1

I am working on a Swing based java application. And my application will holds lots and lots of object in the memory and which leads to "Out Of Memory" error. I knew that my code broken somewhere and not handled the memory leaks.

When i was profiling my application, for a study, i have written a thread which will call System.gc() in a specific interval (example: for every 3 minutes) which was reducing the JVM used memory dramatically. I do understand that, the code was not written properly to clear the references of unused objects which giving this behavior.

I am looking into such memory leaks in my code, but still i am having two questions,

  1. can i write such a thread which request for garbage collection? will it harm my application? i do accept, the performance will be reduced but its not worse than getting out of memory error.

  2. Instead of creating a thread, is there any start-up command-line argument which will request for garbage collection for the given specific time interval.

Thanks in advance !!!

  • You shouldn't need to call `System.gc()` manually; the JVM should already be running the collector periodically for you. – user2357112 Jun 11 '14 at 08:42
  • 2
    It probably won't help. The JVM *guarantees* that the Garbage Collector will run **before an `OutOfMemoryError` is thrown**, so that you're seeing one indicates that the GC *has* run, and that it wasn't able to free up enough memory for execution to continue. Calling it more frequently won't fix that. – JonK Jun 11 '14 at 08:42
  • 1
    Run a profiler. Find out what holds on to memory. Fix your code. – NilsH Jun 11 '14 at 08:46
  • 1
    See [this answer](http://stackoverflow.com/a/2414120/758831). Note also that I have seen first hand code that tried to be clever using `System#gc` and it caused a serious problem that dramatically slowed the app. This sort of thing is best left to the JVM and gc config (notify the jvm how the app is to run (i.e. "stop the world"). To be continued... – wmorrison365 Jun 11 '14 at 08:46
  • [cont...] You should use a proper profiler to profile your app and find the issues or your app will forever be creaking. I would personally recommend [YourKit](http://www.yourkit.com/java/profiler). This enables you to manually gc to "spot" long-lived objects. Its generations are also very useful in memory profiling and there are good video tutorials to get you going. In summary, 1) sort your memory leaks (as far as you can), (2) set a realistic -Xmx, (3) then consider gc-tuning (and read [Java Performance] by Charlie Hunt (links too long to post) – wmorrison365 Jun 11 '14 at 08:57
  • Hi Thank you for the reply. I am using JProfiler to profile my application. I am very much new to JProfiler and i am not able to find which part of my code creating and holding too much memory even when my application is idle. Most of the high instance count are char[], java.util.* and such. Do you have any suggestion for me how to find the memory leaks? – user3729101 Jun 11 '14 at 11:37
  • Start up your application with the `-XX:-HeapDumpOnOutOfMemoryError` argument set and let it run out of memory, then you can analyse the heap dump it creates to see what's taking up all of the heap space. – JonK Jun 11 '14 at 13:05

3 Answers3

1

I do understand that, the code was not written properly to clear the references of unused objects which giving this behavior.

Actually, this is not a proper explanation of the behaviour you see.

Either your code releases references, or it does not.

As a result, the garbarge collector will be able to either reclaim memory, or it will not be able to (not matter how hard it tries or how often you run it).

That garbage collection happens at lengthly intervals during which you build up a lot of "used" memory is perfectly normal. Java will try very hard to reclaim memory before it will complain about OutOfMemory errors. Conversely, it will not try very hard if there is still free memory for you to use.

So unless you are seeing actual performance problems that result from GC taking too long, I would not worry about trying to tune it.

If what you are seeing are OutOfMemory errors, then GC tuning won't help you.

can i write such a thread which request for garbage collection? will it harm my application? i do accept, the performance will be reduced but its not worse than getting out of memory error.

Again, that tradeoff does not exist. You cannot avoid OOM by running GC more often.

And yes, unnecessary GC (which is what you would be doing here) is detrimental to performance.

Thilo
  • 257,207
  • 101
  • 511
  • 656
  • the memory has been reclaimed in GC is my understanding. Say, I am not doing such System.gc() explicitly, then those memory will not be released and the used memory will continue to grow which may cause out of memory error rite? – user3729101 Jun 11 '14 at 09:06
  • No. Before you get an OOM error, the JVM will try very hard to release as much memory as possible. You don't have to call it explicitly. And even if you call it, it won't do any good. OOM is OOM. The garbage collector won't take away memory that your program is still using. – Thilo Jun 11 '14 at 09:12
0

First of all, Use on System#gc is not considered as a best practice.

I do understand that, the code was not written properly to clear the references of unused objects >which giving this behavior

Instead of running GC, try to spot the memory leaks and change your code accordingly. Calling System#gc will hamper your performance severely. I will advise you to revisit your application code.

edited: follow basic Java Programming practice to close resources and assign null to references which are no longer used, and many more.. you can easily find on google.

Mahendra
  • 323
  • 1
  • 7
  • Hi Thank you for the reply. I am using JProfiler to profile my application. I am very much new to JProfiler and i am not able to find which part of my code creating and holding too much memory even when my application is idle. Most of the high instance count are char[], java.util.* and such. Do you have any suggestion for me how to find the memory leaks? – user3729101 Jun 11 '14 at 09:34
  • You can use VisualVM which comes bundled with JDK. and exclude java written classes. – Mahendra Jun 11 '14 at 12:52
0

You can always download VisualVM profiler. It has an option "Perfom GC".

Szarpul
  • 1,531
  • 11
  • 21