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