0

I have an application deployed to production which sometimes throws OutOfMemory exception due to some memory leak. It is running on a headless ubuntu box on which I would prefer not to connect visualvm, jconsole etc remotely. Is there a way to make the jvm do gc (like in visualvm where you just click a button to do it).

I would like to run jmap -histo:live <pid> and this gc commands alternatively to find out which objects are surviving a gc. Which object numbers are growing etc. Right now I can see some unexpected object counts but it is happening across a number of my domain objects so I am not sure if it is a delayed gc or a memory leak.

So in short, I am looking for the linux command to be run against a jvm pid to cause it to do gc. Not system.gc.

Abe
  • 8,623
  • 10
  • 50
  • 74
  • 2
    possible duplicate of [How do you Force Garbage Collection from the Shell?](http://stackoverflow.com/questions/3523837/how-do-you-force-garbage-collection-from-the-shell) – NPE Jun 12 '12 at 16:26
  • 2
    If you run out of memory due to a memory leak, running gc will not help you. A memory leak occurs exactly when gc cannot remove "dead" objects, because there's a hidden reference to them. – Jochen Jun 12 '12 at 16:26
  • Yes looks like a duplicate. Thanks for the link. I did not see it show up when I searched/typed the q. – Abe Jun 12 '12 at 16:28
  • OOM means you have no more memory (or at least not enough contiguous space after GC has done is it's stuff. It's already executed, doing another pass howvere initiated isn't going to resolve anything. You could detect teh app is not running and start it again as a temporary measure, but what you need to do is sort the code out. Something(s) that should be out of scope aren't. – Tony Hopkinson Jun 12 '12 at 16:39

2 Answers2

2

The GC will aggressively try to clean up unreferenced objects as the heap gets full. So its not a "delayed gc". I think you are on the right track, use jmap and get a heap dump. Then analyze it to see what application objects are surviving that should not be. You may need to get a couple heap dumps and compare them against each other.

kevin_c
  • 91
  • 2
0

It's pretty hard to get a real memory leak in Java. If you're getting an out of memory error, then it most likely means that you're actually running out of memory. So to fix this, you need to find references to unused objects and clean them up manually. Because otherwise, the garbage collector can't free up the wasted memory.

When the JVM can't allocate any more memory, the garbage collector should automatically run.

Community
  • 1
  • 1
tskuzzy
  • 35,812
  • 14
  • 73
  • 140
  • It's been a while since I've worked in Java, and this question is pretty old, but this information is not accurate. At least up to Java 5, it was definitely easy to get circular dependencies among your objects. This can happen when you work with object pools, complex data structures, or as interactions between frameworks. Learn to use JVM inspection tools. They will benefit you greatly and you will definitely encounter JVM memory issues in production. – Patrick Farrell May 07 '13 at 22:46