views:

29

answers:

2

hi,

  1. i sampling the memory usage and count number of instances for POJO. if when i press 'garbage collecting' in visual vm and i can see the instances down , does that mean it's memory leak free ?

  2. how to force jvm do garbage collecting everyday at midgnight ? (just like automatically press garbage collect on visualvm) ? i see visualvm 's cpu usage, gc is always 0%. i set -xmx -xms 1024m, but normally memory usage around 200mb. is this because GC is only done when necessary? that's why always 0% for gc cpu time

  3. how to check the time of last time doing 'full GC' ?

+2  A: 
  1. No, not necessarily. All that really means is that some of your objects can be collected. You could still have a garbage leak if, for example, the instance count always went down after a GC but not quite to the same level as before; if this "baseline" increased over time you'd run out of memory occasionally. Also, it's very hard to prove a negative, so it could be that your application does have a memory leak bug, but this specific situation isn't exercising it.
  2. Let's clarify one thing - you can never force the JVM to run garbage collection. The best you can do is call System.gc(), which is a hint to the JVM that it might want to run GC now. It doesn't have to do anything, and a no-op implementation of that method would be perfectly valid. Basically, garbage collection "just happens" and the less you place specific expectations on it the better. So basically, yes, it will typically only run when needed.
  3. Again, this is typically internal knowledge and the details will depend on which garbage collector implementation you're using. For Sun's JVM, however, you can use the command-line argument -verbose:gc to get verbose garbage collection details output to the console. If you want to inspect the details programmatically or visually, this information might be exposed via JMX too (i.e. set up your process to use JMX remoting, and connect to it with JConsole). For me, using Sun's 1.5.0_06 JVM, I see an MBean in java.lang.GarbageCollector that exposes some information including the time of the last full GC.
Andrzej Doyle
if you have 3 applications in tomcat. and one application doing System.gc(). that will automatically gc all 3 applications because sharing same jvm right?
cometta
That's correct - the `gc()` call is JVM-wide.
Andrzej Doyle
+1  A: 
  1. No. There is no really concrete way to determine if your application is memory leak free. The best is to run a soak test over an extended period of time and ensure that the size of the live heap is stable.

  2. You can use the System.gc() method call to run the garbage collector.

  3. There doesn't seem to be an API for getting the last time a GC has run, however the GarbageCollectorMXBean can give some statistics about the GC.

Michael Barker