views:

89

answers:

2

The scheduler that runs as a daemon in JVM to garbage collect objects, can it be monitored with JMX.Do we have some way of telling that these are the objects it might garbage collect now.That way we can figure out that if we are creating specific objects of our classes and the instances are held up in memory of when they can be garbage collected and how much memory they take up. Also is there a way of telling how much memory in average an instance of my class takes.

I know this question might be stupid but even though we cannot force garbage collection, can we find out at run time which objects are not being connected by other objects (in other word not being used at all).Can we find weak references at runtime, is there a way to depict that.

+4  A: 

Use a profiler for this - take a look at Visual VM.

Nick Holt
+4  A: 

What you are asking about is called garbage collection tuning, and it has many resources in the web, such as this.

Specifically, there are application which you can use to monitor the behaviour of the memory of the java application such as VisualVM. You can also make the VM to print all the garbage collections to a file by adding the following flags to the commnad line:

 -verbose:gc
 -XX:+PrintGCTimeStamps
 -XX:+PrintGCDetails

And then use analysis application such as gcviewer ot pmat to analyse them.

David Rabinowitz