0
votes

Can I force a running Java application to Garbage Collect from console ?

Just to preempt some replies:

  • I know I can do it with JVisualVM but I only have console access not X-Windows access
  • I know System.gc() only requests garbage collection it doesnt force it, but thats seem to 3. work, so Im looking for a way of calling that externaly.
  • I know you shouldn't garbage collect a working application, I'm doing this as part of a process of checking for a potential memory leak.'
  • The application is runinng in production I cannot modify its envrionment, jvm ecetera
5
To check a potential memory leak, you also have to see how much memory is used after a collection. For this VisualVM is the easiest in any case, so use that.Peter Lawrey
Duplicate has details on how to do this.Stephen Connolly
Yes i did Zagorulkin which is why I added my listed of prempt points to avoid going over the same groundPaul Taylor
@PeterLawrey yes I would like to use JVisualVM but we could not get it to work, isnt there a not a console based alternative to do the garbage collection, then I can use jstat aftwerwards to check the affect. Bear in my I dont have direct access to the machine so everything has to be done via a system administratorPaul Taylor

5 Answers

2
votes

JVisualVM can connect to remote processes if

  • relevant ports are open
  • java process was started with correct parameters to allow remote connections

So you don't need to be in an xWindows environment on the same machine to make the magic happen...

Example or parameters to pass to java

-Dcom.sun.management.jmxremote.port=9005\   
-Dcom.sun.management.jmxremote.authenticate=false\ 
-Dcom.sun.management.jmxremote.ssl=false
2
votes

Create a simple Java agent which does only System.gc(). Attach to the target JVM by using its pid and run it. Start from here: http://docs.oracle.com/javase/6/docs/jdk/api/attach/spec/com/sun/tools/attach/VirtualMachine.html

2
votes

Actually the jmap tool from the JDK will trigger full GC. Try jmap histo

1
votes

No.. You can't force the garbage collector to perform garbage collection. Using System.gc() you can only request the garbage collector to perform garbage collection.

-1
votes

Consider running your program with the Oracle JRockit JVM. There, you can use JRCMD and issue JVM-level management commands / monitoring, including forcing a GC. It's right, that (under JRockit and elsewhere), System.gc() does not necessarily do anything!