One, the method of calculating the object size
How to calculate the size of an object in Java, 4 ways are found:
Getobjectsize method of 1.java.lang.instrument.instrumentation;
2.BTraceUtils sizeof method;
The code calculation provided in 3.http://yueyemaitian.iteye.com/blog/2033046;
The toolkit provided by 4.https://github.com/mingbozhang/memory-measurer;
The use of java.lang.instrument.Instrumentation is essentially the basis of the other three methods, but the method in the class Getobjectsize just calculates the object itself, the JDK comment description:
Returns an implementation-specific approximation of the amount in storage consumed by
The specified object. The result may include some or all of the object ' s overhead,
And thus is useful for comparison within a implementation but not between implementations.
The 2nd is to use the Btrace method, you can test the production environment of the program, However, the btraceutils sizeof implementation on the direct call or Java.lang.instrument.Instrumentation Getobjectsize method, so there is still a 1th method of the problem. The 3rd method calculates the size of the array, but cannot calculate objects such as HashMap. The 4th method supports the calculation of the size of common objects such as HashMap.
The 1th method online can find a lot of information can be consulted, the 3rd Method link Blog content is very detailed. Here are the steps to use for the 2nd and 4th methods.
Calculating object Size in Btrace
The version I'm using is the plugin version in Java VISUALVM. VISUALVM open mode for command line input JVISUALVM,
Install plugin btrace, install successfully, view plugin window
Install plug-in this part of the work is not said here, the following shows how to use.
Start Tomcat, select the Tomcat you just launched in the left of VISUALVM, and select Trace application in the right-click Bullet box
The Btrace window will open on the right:
The code written below is btrace used to detect the size of the return value of the Com.test.data.TestBtrace class after the search method is executed.
Click the Start button to perform the successful display as follows, indicating that the monitoring status has been entered:
Executing the search method in Com.test.data.TestBtrace above Tomcat, the Btrace console prints out the type of the returned result object and the object size, each time it is bytes.
specific Btrace script descriptions and examples can be consulted:https://github.com/mingbozhang/btrace
where docs has help documentation, samples has examples.
Third, calculate object size using Memorymeasurer
To https://github.com/mingbozhang/memory-measurer above to download the source code locally, the compilation requires local pre-installation java,maven.
1. Add the detection code to the target code
The use of this method is not as low-intrusion as btrace, it is necessary to include the detection code in the code:
long memory = memorymeasurer.measurebytes (objecttotest);
The variable memory content in the above code is obtained by outputting logs or by other means.
2. Starting Tomcat is included in the java_opts:
-javaagent:< actual directory >/memory-measurer-0.1.0-snapshot.jar
3. Start Tomcat, manipulate the application, and view the size information of the printed object to be detected.
Use this method to test out the Com.test.data.TestBtrace in the search method to return the result object size to: 349264 bytes
Note: Because this project relies on the guava package, If the code to be tested is directly starting with the Memory-measurer-0.1.0-snapshot.jar generated using target, if you are detecting application on Tomcat you will need to use target generated memory-measurer-0.1 .0-snapshot-jar-with-dependencies.jar.
Iv. thinking
The methods found in the 1th and 2 are some of the more official methods. 3, 4 is a personal writing code or tools. If the need to find the real size of the container object is an urgent need, why not see the official version of the object real size calculation tool? Does it make sense?
Calculating the size of objects in Java