Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to request JVM garbage collection (not from code) when run from Windows command-line

how could I request Java garbage collection externally, starting the program from JAR (Windows BAT used)?

  • From the Java code I can do it with System.gc()
  • When running a JNLP distribution, I get this "Java console" turned on from Control Panel / Java / ... and this Java console provides manual garbage collection.

But... When I'm running the jar from command-line / bat the java console doesn't seem to open. Couldn't find help with a brief googling, maybe somebody here?

like image 559
Touko Avatar asked Apr 15 '09 07:04

Touko


People also ask

Which code is written to manually request JVM to garbage collection?

System class has a static method gc(), which is used to request JVM to call garbage collector.

How can you force an object to be garbage collected?

Finalizing Objects Before an object is garbage collected, the Java runtime system gives the object a chance to clean up after itself. This step is known as finalization and is achieved through a call to the object's finalize method. You can force object finalization to occur by calling System 's runFinalization method.

What triggers JVM garbage collection?

When a JVM runs out of space in the storage heap and is unable to allocate any more objects (an allocation failure), a garbage collection is triggered. The Garbage Collector cleans up objects in the storage heap that are no longer being referenced by applications and frees some of the space.


1 Answers

For a purely command-line approach, you should be able to use jcmd. I believe jcmd has been part of the standard JDK install since at least Java 1.7. The command would be something like this:

jcmd <process-id> GC.run

You can get a list of available diagnostic commands that jcmd provides for a particular process like this:

jcmd <process-id> help
like image 117
Ogre Psalm33 Avatar answered Oct 02 '22 00:10

Ogre Psalm33