如何計算java中的物件object大小size
阿新 • • 發佈:2019-02-05
Measuring Object Size
While the Java platform doesn't provide a way to measure the size of an object, it is possible to measure the approximate size indirectly. Section 5.1.1 discusses theRuntime
methods that provide information about the size of the JVM's object heap.
You can also use these methods to determine the approximate size of an object by:
Although this general approach works, it has some flaws:
Runtime.freeSpace
method might not be accurate down to the byte level.
Fortunately, you can get around most of these by:
Creating a primer instance of the class before your measure the heap the first time. This eliminates the impact of one-time cache costs.
Measuring several instances of the object and calculating the average. This helps offset the unpredictability inherent in the GC behavior and the heap space measurement methods.
zz from oracle