The size of computing objects in Java and the size of computing objects in Java

Source: Internet
Author: User

The size of computing objects in Java and the size of computing objects in Java

I. method for calculating object size

How to calculate the object size in Java has been found in four methods:

1. getObjectSize method of java. lang. instrument. Instrumentation;

2. sizeof method of BTraceUtils;

3. http://yueyemaitian.iteye.com/blog/2033046;

4. https://github.com/mingbozhang/memory-measurer.pdf tool package;

 

Essentially, the use of java. lang. instrument. Instrumentation is the basis of the other three methods. However, the getObjectSize method in this class only calculates the object itself. JDK annotation description:

Returns an implementation-specific approximation of the amount of storage consumed
The specified object. The result may include some or all of the object's overhead,
And thus is useful for comparison within an implementation but not between implementations.

 

The 2nd method uses the BTrace method to detect programs in the production environment. However, the sizeof Implementation of BTraceUtils directly calls java. lang. instrument. the getObjectSize method of Instrumentation, so there are still 1st methods. The method in 3rd can calculate the size of the array, but cannot calculate objects such as HashMap. 4th methods support calculation of the size of common objects such as HashMap.

 

You can find a lot of information on the Internet in 1st methods. The content of the method link blog in 3rd has been very detailed. Here are the steps for using methods in 2nd and 4th methods.

 

Ii. Calculation object size in BTrace

The BTrace I used is the plug-in version of Java VisualVM. In the visual VM mode, enter jvisualvm as the command line,

 

Install plug-in BTrace. If the installation is successful, check the Plugin window.

I will not talk about installing the plug-in here. The following shows how to use the plug-in.

Start tomcat, select the started tomcat on the left side of VisualVM, and select Trace application in the context menu.

The BTrace window will be opened on the right:

The Code on the right above is BTrace used to check the size of the return value of the com. test. data. TestBtrace class after the search method is executed.

import com.sun.btrace.annotations.*;import static com.sun.btrace.BTraceUtils.*;@BTrace public class Sizeof {    @OnMethod(        clazz="com.test.data.TestBtrace",        method = "search", location = @Location(Kind.RETURN)    )     public static void onnew(@Return Object obj) {        println(Strings.concat("object of: ", Reflective.name(Reflective.classOf(obj))));        println(Strings.concat("size: ", Strings.str(sizeof(obj))));    }}

 

 

Click the start button and the execution is displayed as follows (in the red box), indicating that the monitoring status has entered:

 

Execute the search method in com. test. data. TestBtrace on tomcat. The BTrace console prints the type and size of the returned result object, which is 48 bytes each time.

BTrace script description and examples can refer to: https://github.com/mingbozhang/btrace

Here, docs has help documentation and samples has examples.

3. Use MemoryMeasurer to calculate the object size

To begin.

1. Add the detection code to the target code.

The method used here is not like the low intrusion of BTrace. You need to add the detection code to the Code:

long memory = MemoryMeasurer.measureBytes(objectToTest);

The memory variable in the above Code is obtained by outputting logs or other methods.

2. start TOMCAT by adding the following content to JAVA_OPTS:

-Javaagent: <actual directory>/memory-measurer-0.1.0-SNAPSHOT.jar

3. Start TOMCAT, operate application, and view the size of the objects to be checked.

 

The size of the search object returned by the search Method in com. test. data. TestBtrace tested by this method is 349264 bytes.

Note: Because this project depends on the guava package, if the code to be tested is started directly using the main method to use the memory-measurer-0.1.0-SNAPSHOT.jar generated by the target, if it is to detect the application on tomcat, you need to use the memory-measurer-0.1.0-SNAPSHOT-jar-with-dependencies.jar generated by the target.

 

Iv. Thinking

The Methods 1st and 2 found are relatively official methods. 3 and 4 are personal code or tools. If you need to find the actual size of container objects, why is there no official version of the object's real size computing tool?

 

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.