views:

175

answers:

6

If we have 300 classes in an application, is it possible to monitor how many instances of each class we have at a given time? Is it possible to know how much memory each instance is consuming?

Thanks

A: 

use jvisualvm.exe it is part of the JDK6

Does JVisualVM show instance counts and memory size by class? I know it shows thread activity and overall memory footprint, etc., but I didn't think it had the features the OP was looking for.
Andrzej Doyle
https://visualvm.dev.java.net/features.htmlTake and browse heap dumps. When you need to browse contents of application memory or uncover a memory leak in your application, you'll find the built-in HeapWalker tool really handy. It can read files written in hprof format and is also able to browse heap dumps created by the JVM on an OutOfMemoryException.
A: 

You could use a Java profiler, depending on which web container (if it's a web-app) you're deploying to you can try alot of different profilers: http://java-source.net/open-source/profilers

NickDK
+1  A: 

JDK 1.6 includes a tool called jvisualvm, which allows you to view lots of information about your running Java program, including memory usage, threads, etc. You could also use a profiler to see this kind of information. The profiler in NetBeans looks a lot like JVisualVM.

Jesper
A: 

Most profilers will give you this information. I'm personally familiar with JProfiler, but I expect any worthwhile profiler would let you do this.

For a more low-tech solution, you could even trigger a heap dump from your application and then look through it with an application like jhat. The interface leaves a lot to be desired, though, and profilers would be much more comfortable to use in any non-trivial case.

Edit: here is an example of the memory screen for JProfiler, and you can also investigate the reference chain.

Andrzej Doyle
+1  A: 

I personally like Yourkit. It has a very good UI and comes with a 30 day trial. The details are also pretty extensive.

The online help document in that site should help you on how to set things up for running it.

Aviator
wow ... Yourkit is amazing! Thanks alot
craftsman
A: 

use profiler4j or pmd

personally i like profiler4J for its ease of use and simple graphics :)

Rakesh Juyal