A study of Java footprint Memory

Source: Internet
Author: User
Tags float double

Recently on the memory of the program to do some optimization, achieved good results, summed up some experience, briefly said, I believe that we will write a good quality program to help

The following discussion is for 32-bit systems that are not applicable to 64-bit systems,

Often you write a program, a test, the function is no problem, a look at the memory footprint is not much, not to consider other things. However, it is possible that the program uses a data structure that, when the size of the data becomes larger, memory footprint surges.

The basic && key question is, how much memory does the various dongdong in Java occupy?????????

For the primitive type, there are 8

byte short int long float double Char boolean their lengths are respectively

1 2 4 8 4 8 2 1

This is not wordy, for example.

long[] data=new long[1000];

Consuming memory 8*1000 bytes

In addition, the data itself is an object that also takes up memory for a number of subsequent reclassifications, of course it's for 8*1000, ignoring

Again, the use of object, before saying this, first talk about the reference, a customary argument is

No pointers in Java, only references, references are safe

This is true, but in principle, references are pointers, but the JVM has a lot of checks and restrictions on the use of pointers, and this reference/pointer becomes very secure

Direct conclusion: A reference to 4byte, on the 32-bit system

Object obj=null; //4byte
Object[] objs=new Object[1000]; //至少4*1000byte

Do you think I've defined an obj, or null, to take up 4byte

Defines a objs,1000 element, but it's all null, and each takes up 4byte.

Yes!!!!!

Though Obj==null, it is already a reference, or a pointer to a

The pointer also takes up a place.!!!! Ah,!!!!. Ah,!!!!.

Next, directly to another conclusion: object for 8byte, note that the pure object

Object Obj=new object (); How much????

8byte?? Wrong!! 12byte, forgot to have a reference, 8byte is the content of the object

Remember the object Obj=new object (); Account for 12byte

Object[] objs=new Object[1000];
for(int i=0;i<1000;i++) {
objs[i]=new Object();
}

At least occupy 12*1000 bytes

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.