Reputation: 188
We have a Java batch program that process large amounts of data.
After processing this data why doesn't JVM clear the garbage immediately instead of waiting for next execution ?
Based on what i have observed, data from the previous run remains as it is, until i manually shutdown the JVM or next scheduled run starts.
I want the JVM to clear the data immediately so that, on the next scheduled run, it doesn't have to clear the garbage in parallel during then run, hopefully running faster.
Appreciate your help.
Really appreciate your quick response.
I don't really see considerable performance impact on my process because of this garbage existence in the memory. But, it is eating away system RAM this whole time and impacting memory availability to other programs outside this JVM.
Another problem i have is, during development and debugging this program, while the program runs i let it run but once done, to analyze the results, i have to open a few other applications which obviously need a bit of RAM but the JVM doesn't relinquish RAM space unless i Stop it, affecting other programs usability and performance.
Upvotes: 1
Views: 3107
Reputation: 4877
Its not a good idea to allocate large memory to JVM if you think it can affect other applications. GC options can be tuned based on the jvm version and the gc options being used. The jvm may be running the default serial collector which is not a good choice for bigger heap sizes. Take a look at the concurrent mark sweep collector which performs garbage collection concurrently. http://docs.oracle.com/javase/8/docs/technotes/guides/vm/cms-6.html
Take a look at g1 garbage collector if using 1.7
Foremost thing is to ensure that unwanted objects are being freed up in the code and there is no memory leak.
Upvotes: 0
Reputation: 543
JVM runs garbage collector by using gc policy. Almost all GC occurs at allocation Failure. You can call System.gc(); but this way not recommended.
Upvotes: 0
Reputation: 26882
JVM's GC is very efficient as is and it's not easy to make it "more efficient". Unless you are having serious performance issues, I wouldn't try to do anything.
If you have serious performance issue, you should look into other possibilities, primarily on your code. It's almost never GC.
Having said that, my practical suggestion is to see if something is retaining a reference to the data. If the reference is only cleared by the virtue of the next run, it'd explain the symptom. Whether you have threads has really nothing to do with GC eligibility. All that counts is whether you have a live reference to the data.
Upvotes: 2
Reputation: 2223
You cannot force the Garbage collector to run. You can barely suggest the JVM to run it with System.gc()
. Moreover, recent JVM are already quite optimized for memory management, so do not expect any large improvement after calling System.gc()
by yourself.
Upvotes: 1