1

Is it possible to find out programmatically how many objects are eligible for garbage collection and print out their quantity?

2 Answers 2

8

No, because if you have a reference to the object, it's not eligible for garbage collection.

Sign up to request clarification or add additional context in comments.

2 Comments

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.
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 :)
1

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

Runtime rt = Runtime.getRuntime(); rt.totalMemory();

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.