Is it possible to find out programmatically how many objects are eligible for garbage collection and print out their quantity?
2 Answers
No, because if you have a reference to the object, it's not eligible for garbage collection.
2 Comments
Louis Wasserman
Also, and perhaps more to the point: if the GC has gone to the trouble of finding out which objects are eligible for garbage collection, it's probably already collected them.
Alex Pilugin
yes, you are right, of course. And in fact, i guess, it does matter for a real life. I just wanted to find out some opportunity to check my SCJP 6 answers :) it's not for a real-life programming :)
You cannot do this using conventional APIs. You can however estimate the memory that these objects occupy. To do this you can use utility named jmap that gives you objects that are not eligable for garbage collection. Then you can subtract memory that all these objects occupy from total heap. To estimate total heap check resident memory used by your application and subtract resident memory occupied by minimal applcation (HelloWord style).
1 Comment
Alex Pilugin
Runtime rt = Runtime.getRuntime(); rt.totalMemory();