how could I request Java garbage collection externally, starting the program from JAR (Windows BAT used)?
System.gc()
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?
System class has a static method gc(), which is used to request JVM to call garbage collector.
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.
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.
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
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With