http://www.codeinstructions.com/2008/12/java-objects-memory-structure.html
This scheme allows for a good optimization of memory usage. For example, imagine you declared the following class:
class MyClass {
byte a;
int c;
boolean d;
long e;
Object f;
}
If the JVM didn't reorder the attributes, the object memory layout would be like this:
[HEADER: 8 bytes] 8
[a: 1 byte ] 9
[padding: 3 bytes] 12
[c: 4 bytes] 16
[d: 1 byte ] 17
[padding: 7 bytes] 24
[e: 8 bytes] 32
[f: 4 bytes] 36
[padding: 4 bytes] 40
Notice that 14 bytes would have been wasted with padding and the object would use 40 bytes of memory. By reordering the objects using the rules above, the in memory structure of the object becomes:
HEADER: 8 bytes] 8
[e: 8 bytes] 16
[c: 4 bytes] 20
[a: 1 byte ] 21
[d: 1 byte ] 22
[padding: 2 bytes] 24
[f: 4 bytes] 28
[padding: 4 bytes] 32
This time, only 6 bytes are used for padding and the object uses only 32 bytes of memory.
通读上面的链接原文得知: java中新建一个无属性对象 花费 8byte。有属性时,object reference 占4byte.
所以大致上可以认为java中新建一个全string对象占用的内存是 (8 + string属性*4 + string属性本来占用空间)byte
This scheme allows for a good optimization of memory usage. For example, imagine you declared the following class:
class MyClass {
byte a;
int c;
boolean d;
long e;
Object f;
}
If the JVM didn't reorder the attributes, the object memory layout would be like this:
[HEADER: 8 bytes] 8
[a: 1 byte ] 9
[padding: 3 bytes] 12
[c: 4 bytes] 16
[d: 1 byte ] 17
[padding: 7 bytes] 24
[e: 8 bytes] 32
[f: 4 bytes] 36
[padding: 4 bytes] 40
Notice that 14 bytes would have been wasted with padding and the object would use 40 bytes of memory. By reordering the objects using the rules above, the in memory structure of the object becomes:
HEADER: 8 bytes] 8
[e: 8 bytes] 16
[c: 4 bytes] 20
[a: 1 byte ] 21
[d: 1 byte ] 22
[padding: 2 bytes] 24
[f: 4 bytes] 28
[padding: 4 bytes] 32
This time, only 6 bytes are used for padding and the object uses only 32 bytes of memory.
通读上面的链接原文得知: java中新建一个无属性对象 花费 8byte。有属性时,object reference 占4byte.
所以大致上可以认为java中新建一个全string对象占用的内存是 (8 + string属性*4 + string属性本来占用空间)byte