Java Basic Data Type
Int 32bit
Short 16bit
Long 64bit
Byte 8bit Char 16bit
Float 32bit
Double 64bit Boolean 1bit, this data type represents one bit of information, but its "size" isn't something that's precisely defined. (REF)
Java basic data type size
<textarea readonly name="code" class="java">Private Static void calsize () {<br/> system. out. println ("INTEGER:" + integer. size/8); // 4 <br/> system. out. println ("short:" + short. size/8); // 2 <br/> system. out. println ("LONG:" + long. size/8); // 8 <br/> system. out. println ("Byte:" + byte. size/8); // 1 <br/> system. out. println ("character:" + character. size/8); // 2 <br/> system. out. println ("float:" + float. size/8); // 4 <br/> system. out. println ("Double:" + double. size/8); // 8 <br/> // system. out. println ("Boolean:" + Boolean); <br/>}</textarea>
Implementation of sizeof in simulation C in Java Ideas: The heap size difference before and after GC memory recovery in Java is used to obtain the size of each object.
This is Program There is no ready-made sizeof implementation in Java, mainly because the size of the basic data types in Java is fixed, so it seems that there is no need to use the sizeof keyword. The implementation idea is as follows: Java. lang. the runtime class has some simple functions that can involve memory management: Every Java application has a single instance of class runtime that allows the application to interface with the environment in which the application is running. the current runtime can be obtained from the getruntime method.
| Long |
Freememory()
Returns the amount of free memory in the Java Virtual Machine. |
| Void |
GC()
Runs the garbage collector. |
| Static Runtime |
Getruntime()
Returns the runtime object associated with the current Java application. |
| Long |
Maxmemory()
Returns the maximum amount of memory that the Java Virtual Machine will attempt to use. |
| Void |
Runfinalization()
Runs the finalization methods of any objects pending finalization. |
With these simple memory access, we can obtain some memory conditions. By creating a large array of a class, we can check the memory usage and obtain the class size. Source code:
Private Static void calsize2 () {<br/> rungc (); </P> <p> long heap1 = 0; <br/> final int COUNT = 100000; <br/> object [] objs = new object [count]; </P> <p> for (INT I =-1; I <count; I ++) {<br/> Object OBJ = NULL; <br/> OBJ = new object (); // 8 <br/> // OBJ = new INTEGER (I ); // 16 <br/> // OBJ = new short (short) I); // 16 <br/> // OBJ = new long (I ); // 16 <br/> // OBJ = new byte (byte) 0); // 16 <br/> // OBJ = new character (char) I ); // 16 <br/> // OBJ = new float (I); // 16 <br/> // OBJ = new double (I ); // 16 <br/> // OBJ = new Boolean (true); // 16 <br/> // OBJ = new string (); // 40 </P> <p> if (I <0) {<br/> OBJ = NULL; <br/> rungc (); <br/> heap1 = usedmemory (); // before memory size <br/>} else {<br/> objs [I] = OBJ; <br/>}</P> <p> rungc (); <br/> long heap2 = usedmemory (); // After memory size </P> <p> final int size = (INT) math. round (heap2-heap1)/(double) count); <br/> system. out. println ("heap1 =" + heap1 + "; heap2 =" + heap2); <br/> system. out. println ("heap2-heap1 =" + (heap2-heap1) + ";" + objs [0]. getclass (). getsimplename () + "size =" + size); </P> <p> for (INT I = 0; I <count; I ++) {<br/> objs [I] = NULL; <br/>}< br/> objs = NULL; <br/> rungc (); <br/>}</P> <p> Private Static void rungc () {<br/> for (INT I = 0; I <4; I ++) {<br/> long usedmem1 = usedmemory (); <br/> long usedmem2 = long. max_value; </P> <p> for (Int J = 0; (usedmem1 <usedmem2) & (j <500); j ++) {<br/> rtime. runfinalization (); <br/> rtime. GC (); <br/> thread. yield (); </P> <p> usedmem2 = usedmem1; <br/> usedmem1 = usedmemory (); <br/>}</P> <p> Private Static long usedmemory () {<br/> return rtime. totalmemory ()-rtime. freememory (); <br/>} Note: Object [] objects = new object [count]; only array space is allocated and no object space is allocated. The array only contains references. Conclusion: Code When testing the basic object, the result is as follows: Object OBJ = NULL;
OBJ = new object ();// 8
OBJ = new INTEGER (I );// 16
OBJ = new short (short) I );// 16
OBJ = new long (I );// 16
OBJ = new byte (byte) 0 );// 16
OBJ = new character (char) I );// 16
OBJ = new float (I );// 16
OBJ = new double (I );// 16
OBJ = new Boolean (true );// 16
OBJ = new string ();// 40
How can this happen ??? The example is well written, indicating the size of memory occupied by basic encapsulated objects in Java.
1. A simple object occupies 8 bytes of memory, because each instance must contain at least some basic operations, such as: Wait ()/notify (), equals (), hashcode () and so on
2. the use of integer objects occupies 16 bytes, while int occupies 4 bytes. After encapsulation, the memory consumption is increased by 4 times. so long seems to use more space than the integer object, and the space occupied by the result long is also 16 bytes.
The JVM's memory allocation rules for basic encapsulated objects are as follows:
Memory occupied by the object (8 bytes) + Memory occupied by the maximum basic type (long) (8 bytes) = 16 bytes.
JVM enforces 8 bytes as the boundary.
So Memory size occupied by all basic encapsulation objects is 16 bytes . However, there is a difference. For example, although the integer object occupies 16 bytes of memory, it only uses the memory occupied by the object (8 bytes) + int (4 bytes) = 12 bytes. four other bytes are not used at all. hehe analyzed the test source code for one night. Reference recommendations: How many bytes does a Boolean in Java occupy in memory? Primitive data types (Sun official documentation) |