Implementation of sizeof in simulation C in Java

Source: Internet
Author: User

This is a program. There is no ready-made sizeof implementation in Java, because the size of the basic data type 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. runtime class has some simple functions that can involve memory management: Every Java application has a single instance of class RuntimeThat allows the application to interface with the environment in which the application is running. The current runtime can be obtained from getRuntimeMethod.
 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. Original code:/** created on */public class sizeof {public static void main (string [] ARGs) throws exception {// warm up all classes/methods we will use rungc (); usedmemory (); // array to keep strong references to allocated objects final int COUNT = 100000; object [] objects = new object [count]; long heap1 = 0; // allocate count + 1 objects, discard the first one for (INT I =-1; I <count; ++ I) {object Object = NULL;/* instantiate your data here and assign it to object * // object = new object (); // object = new INTEGER (I ); //???? // Object = new long (I );//???? // Object = new string (); // object = new byte (byte) 0 );//???? // Object = new float (0.2f );//???? // Object = new double (0 );//???? // Object = new helloworld (); if (I> = 0) objects [I] = object; else {object = NULL; // discard the warm up object rungc (); heap1 = usedmemory (); // take a before heap snapshot} rungc (); long heap2 = usedmemory (); // take an after heap snapshot: final int size = (INT) math. round (double) (heap2-heap1)/count); system. out. println ("'before' heap:" + heap1 + ", 'after' heap:" + heap2); lead E M. out. println ("heap delta:" + (heap2-heap1) + ", {" + objects [0]. getclass () + "} size =" + size + "bytes"); For (INT I = 0; I <count; ++ I) objects [I] = NULL; objects = NULL;} Private Static void rungc () throws exception {// It helps to call runtime. GC () // using several method CILS: For (INT r = 0; r <4; ++ R) _ rungc ();} Private Static void _ rungc () throws exception {long usedmem1 = us Edmemory (), usedmem2 = long. max_value; For (INT I = 0; (usedmem1 <usedmem2) & (I <500); ++ I) {s_runtime.runfinalization (); s_runtime.gc (); thread. yield (); usedmem2 = usedmem1; usedmem1 = usedmemory () ;}} Private Static long usedmemory () {return s_runtime.totalmemory ()-s_runtime.freemory ();} private Static final runtime s_runtime = runtime. getruntime ();} // end of class note the above sentence: OBJ ECT [] objects = new object [count]; only array space is allocated and no object space is allocated. The array only contains references. Conclusion: the basic object of code test is as follows:
The object class object is 8 bytes.
The integer class object is 16 bytes.
Long class object is 16 bytes
Byte Class Object is 16 bytes
How can this happen ??? Puzzled. View the post on the Internet: explanation: This 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 integer object occupies 16 bytes, And the int occupies 4 bytes. After encapsulation, the memory consumption is 4 times higher.
3. Then 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.
Therefore, the 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. well, after carefully analyzing the results for one night, there are still a lot of GAINS.

 

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.