Size of the specific object's subtree in the JVM memory

荒凉一梦 提交于 2019-12-08 07:39:18

问题


Is it possible to check programatically how much memory takes some object (with the whole subtree in the JVM memory). I would like to say (from the java code)

'tell me how much memory in the current JVM takes the JPanel with the whole reference subtree when we assume that mentioned JPanel is the root of this tree'.

I wonder if I could this way compare how much memory take two JPanels (or JFrame or whatever), and which takes more - without analyzing the dump. And I wonder if the answer is 'yes' how precise would be this value.


回答1:


as stated in the comments to your qustion, the sizeOf problem in java isnt easy to solve, not only because the object youre trying to size isnt really the root of a memory graph, but also because there are issues with counting the size of static fields etc (they belong to the whole class, not any specific instance).

however, there are ways to get some meaningful data.

the 1st approach is to use a java agent attached to the jvm which in turn calls a size estimation function that sun/oracle have added starting with java 6. see this page for instructions

the 2nd approach is to estimate the size of an object tree based on theoretical calculations. there's a library that does this for you here




回答2:


You can check JAMM, which is a java agent for measuring object size. You can find a tutorial here how to use it.



来源:https://stackoverflow.com/questions/18403742/size-of-the-specific-objects-subtree-in-the-jvm-memory

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!