Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is it a memory leak if the garbage collector runs abnormally?

I have developed a J2ME web browser application, it is working fine. I am testing its memory consumption. It seems to me that it has a memory leak, because the green curve that represents the consumed memory of the memory monitor (of the wireless toolkit) reaches the maximum allocated memory (which is 687768 bytes) every 7 requests done by the browser, (i.e. when the end user navigates in the web browser from one page to other for 7 pages) after that the garbage collector runs and frees the allocated memory.

My question is:

  • is it a memory leak when the garbage collector runs automatically every 7 page navigation?
  • Do I need to run the garbage collector (System.gc()) manually one time per request to prevent the maximum allocated memory to be reached?

Please guide me, thanks

like image 398
mdawaina Avatar asked Sep 16 '12 11:09

mdawaina


2 Answers

To determine if it is a memory leak, you would need to observe it more.

From your description, i.e. that once the maximum memory is reached, the GC kicks in and is able to free memory for your application to run, it does not sound like there is a leak.

Also you should not call GC yourself since

  1. it is only an indication
  2. could potentially affect the underlying algorithm affecting its performance.

You should instead focus on why your application needs so much memory in such a short period.

like image 180
Cratylus Avatar answered Oct 23 '22 10:10

Cratylus


My question is: is it a memory leak when the garbage collector runs automatically every 7 page navigation?

Not necessarily. It could also be that:

  • your heap is too small for the size of problem you are trying to solve, or

  • your application is generating (collectable) garbage at a high rate.

In fact, given the numbers you have presented, I'm inclined to think that this is primarily a heap size issue. If the interval between GC runs decreased over time, then THAT would be evidence that pointed to a memory leak, but if the rate stays steady on average, then it would suggest that the rate of memory usage and reclamation are in balance; i.e. no leak.

Do I need to run the garbage collector (System.gc()) manually one time per request to prevent the maximum allocated memory to be reached?

No. No. No.

Calling System.gc() won't cure a memory leak. If it is a real memory leak, then calling System.gc() will not reclaim the leaked memory. In fact, all you will do is make your application RUN A LOT SLOWER ... assuming that the JVM doesn't ignore the call entirely.


Direct and indirect evidence that the default behaviour of HotSpot JVMs is to honour System.gc() calls:

  • "For example, the default setting for the DisableExplicitGC option causes JVM to honor Explicit garbage collection requests." - http://pic.dhe.ibm.com/infocenter/wasinfo/v7r0/topic/com.ibm.websphere.express.doc/info/exp/ae/rprf_hotspot_parms.html

  • "When JMX is enabled in this way, some JVMs (such as Sun's) that do distributed garbage collection will periodically invoke System.gc, causing a Full GC." - http://static.springsource.com/projects/tc-server/2.0/getting-started/html/ch11s07.html

  • "It is best to disable explicit GC by using the flag -XX:+DisableExplicitGC." - http://docs.oracle.com/cd/E19396-01/819-0084/pt_tuningjava.html

And from the Java 7 source code:

./openjdk/hotspot/src/share/vm/runtime/globals.hpp

  product(bool, DisableExplicitGC, false,                                   \
          "Tells whether calling System.gc() does a full GC")               \

where the false is the default value for the option. (And note that this is in the OS / M/C independent part of the code tree.)

like image 32
Stephen C Avatar answered Oct 23 '22 09:10

Stephen C