0

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
Paul Taylor
  • 13,411
  • 42
  • 184
  • 351
  • Did you saw this? http://stackoverflow.com/questions/1481178/forcing-garbage-collection-in-java – Dmitry Zagorulkin Jan 23 '13 at 09:00
  • 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 Jan 23 '13 at 09:02
  • 1
    [Duplicate](http://stackoverflow.com/questions/3523837/how-do-you-force-garbage-collection-from-the-shell) has details on how to do this. – Stephen Connolly Jan 23 '13 at 09:05
  • Yes i did Zagorulkin which is why I added my listed of prempt points to avoid going over the same ground – Paul Taylor Jan 23 '13 at 09:43
  • @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 administrator – Paul Taylor Jan 23 '13 at 09:44
  • In that case I would wait for the process to GC itself. If you have a memory leak this should be often enough that you don't need to trigger it manually. If you have a very slow leak (over many days) it can be hard to find in a short period unless you artificially increase the load. This requires a test environment which you should have and you should have enough access to test to do your job. – Peter Lawrey Jan 23 '13 at 10:02
  • We have a test environment but not seeing a problem on there, the problem is only ocurring on production, and I have additional issue educating the administrator on how java works as they are confusing heap usage with memotry used by memory mapped file sin this lucene. based application. Anyway you say to wait for it to gc itself, but how do I know when it does this ? – Paul Taylor Jan 23 '13 at 10:21

5 Answers5

2

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
demaniak
  • 3,716
  • 1
  • 29
  • 34
  • Just note that the above parameters opens a gaping security hole, so don't run this in production! – demaniak Jan 23 '13 at 09:08
  • The application is already running isnt there a simple console based alternative ? – Paul Taylor Jan 23 '13 at 09:41
  • Maybe have a look at [jmxterm](http://wiki.cyclopsgroup.org/jmxterm) - essentially you are looking for a console based JMX client – demaniak Jan 23 '13 at 10:39
  • This stackoverflow post might be usefull also - http://stackoverflow.com/questions/1751130/calling-jmx-mbean-method-from-a-shell-script – demaniak Jan 23 '13 at 10:41
2

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

jdb
  • 4,419
  • 21
  • 21
  • Thankyou for actually attempting to answer my question, however is there an existing such program availble in the jdk rather than me having to write my own ? – Paul Taylor Jan 23 '13 at 09:49
2

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

jdb
  • 4,419
  • 21
  • 21
1

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.

Xavier DSouza
  • 2,861
  • 7
  • 29
  • 40
-1

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!

mgaert
  • 2,338
  • 21
  • 27
  • Hi, "-1", care to explain why you feel this is not an answer? – mgaert Jan 23 '13 at 09:47
  • This is an aplication in production I cannot start modifying the environment. – Paul Taylor Jan 23 '13 at 09:47
  • Then please make sure your question is accurate instead of downvoting. For starters, explain what "console" means. "JConsole", presumably, but we better make no assumptions here, right? – mgaert Jan 23 '13 at 09:49
  • No console means a computer without a windowing environment i.e http://en.wikipedia.org/wiki/System_console – Paul Taylor Jan 23 '13 at 09:51