result:
Let us use this tool to see if the results are the same as we expected.
Note: The following results are based on the JDK1.3.1 version of the Windows platform and cannot guarantee that all platforms or JDK versions are available.
l java.lang.object
The base class of all objects is our first example. We will get: for java.lang.object.
'Before' Heap: 510696,'After 'Heap: 1310696
Heap Delta: 800000, {Class Java.lang.Object} size = 8 BYTES
So, a simple object object takes up 8 bytes of memory space, of course, we can't want the space it occupies 0 because each instance must contain some of the most basic operations, such as equals (), hashcode. WAIT () / notify (), etc.
l java.lang.integer
I often encapsulate local int to INTEGER in the instance of local int to INTEGER, so that we can use them in a collection of objects, how much memory is to be consumed?
'Before' Heap: 510696, 'After' Heap: 2110696
Heap Delta: 1600000, {Class Java.lang.integer} size = 16 BYTES
This 16-byte result is bad than we expected, because an int value happens to be 4 bytes, but after INTEGER, 3 times the space is used.
l java.lang.long
Long looks more space than Integer, but the truth is not the case:
'Before' Heap: 510696, 'After' Heap: 2110696
HEAP Delta: 1600000, {Class Java.lang.long} Size = 16 BYtes
Obviously, because a special JVM implementation must conform to a specific CPU type, the factual object size must be aligned with the low-level memory boundary in the stack. It seems a long is an 8-byte size Object object plus 8 bytes to save the LONG value. In contrast, Integer has 4 bytes where there is no space. So, it should be a JVM forced object to use 8 bytes as the boundary of the word.
l arrays
Next, there are some basic types of arrays, more guiding significance, can partially discover some hidden information and prove that other popular tricks: use a size-1 array package basic type as an object. Use a loop to increase the length of the array by modifying SizeOf.main (). The INT array can then be obtained:
Length: 0, {Class [i} size = 16 bytes
Length: 1, {class [i} size = 16 BYTES
Length: 2, {class [i} size = 24 BYTES
Length: 3, {class [i} size = 24 BYTES
Length: 4, {Class [i} size = 32 bytes
Length: 5, {Class [i} size = 32 bytes
Length: 6, {class [i} size = 40 BYTES
Length: 7, {class [i} size = 40 BYTES
Length: 8, {class [i} size = 48 BYTES
Length: 9, {class [i} size = 48 byteslength: 10, {class [i} size = 56 bytes
There are also some Char arrays:
Length: 0, {class [C} size = 16 BYTES
Length: 1, {class [C} size = 16 bytes
Length: 2, {class [C} size = 16 BYTES
Length: 3, {class [C} size = 24 bytes
Length: 4, {class [C} size = 24 BYTES
Length: 5, {class [C} size = 24 bytes
Length: 6, {class [C} size = 24 bytes
Length: 7, {class [C} size = 32 bytes
Length: 8, {class [C} size = 32 bytes
Length: 9, {class [C} size = 32 bytes
Length: 10, {class [C} size = 32 bytes
As can be seen from the above, the border of 8 bytes is obviously manifested. At the same time, it is definitely containing the inevitable 8 bytes of Object headers, and then the array of basic data types takes up for other 8 bytes. It does not provide any memory usage than INT [1] and Integer, in addition to the variable version of the same data.
l multi-dimensional array
The multidimensional array has another amazing thing. Developers generally use a constructor such as int [DIM1] [DIM2] for numbers or scientific calculations. In an INT [DIM1] [DIM2], each nested int [DIM2] is an object, and each object is plus a 16-byte array object header. When I don't need a triangular or rough array, the one represents a pure head. When the dimension increases, the effect is increased. For example, an instance of an int [128] [2] takes up 3600 bytes, uses 246% of the head. In special examples Byte [256] [1], this head factor is already 19! Compared to C / C solutions, the same syntax does not increase so much memory consumption.
l java.lang.string
Let's test a space, now constructed a new string ():
'Before' Heap: 510696, 'After' Heap: 4510696
Heap Delta: 4000000, {Class Java.lang.String} Size = 40 BYTES
The result provides a quite bad phenomenon, that is, an empty String is to take up the size of 40 bytes, enough to save 20 characters.
Before we use the string containing the content, we use a help group method to create a string. However, use the following text to create:
Object = "String with 20 chars";
Will not work because all such object operations will end on the same string instance. The language specification clearly shows that such behavior (Java.lang.Intern ()), so use the following:
Public Static String CreateString (Final Int Length)
{
Char [] result = new char [length];
For (int i = 0; i } After this creation function, you get this result: Length: 0, {class java.lang.string} size = 40 BYTES Length: 1, {class java.lang.string} size = 40 BYTES Length: 2, {class java.lang.string} size = 40 BYTES Length: 3, {class java.lang.string} size = 48 BYTES Length: 4, {class java.lang.string} size = 48 BYTES Length: 5, {class java.lang.string} size = 48 bytes Length: 6, {class java.lang.string} size = 48 BYTES Length: 7, {class java.lang.string} size = 56 bytes Length: 8, {class java.lang.string} size = 56 bytes Length: 9, {Class Java.lang.String} size = 56 bytes Length: 10, {class java.lang.string} size = 56 bytes The results showed that the memory increased trajectory of the string. But the string should add a 24-byte head. For non-empty strings, if the characters are less than 10 or less, this increased head will consume relative to the effective load (2 bytes for each character, plus 4 as the length) at 100 % To 400% change.