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.
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
You can check JAMM, which is a java agent for measuring object size. You can find a tutorial here how to use it.