Pranalee
Pranalee

Reputation: 3409

How is memory leak defined in JVM?

When I get OOM error, how do I decide weather I should increase heap size or there is memory leak problem with my code?

Also, how do I decide with inital heap size of my application? In my current application, we had started with 512MB but now have increased to 4GB. But this was done by trial and error method. Is there any systematic way to decide required heap size?

If my question is sounding too basic, can anyone please share some references which can help to increase understanding of this?

Upvotes: 2

Views: 214

Answers (2)

user207421
user207421

Reputation: 310884

It seems to me that 'leak' is a poor term in the GC context. Leaks are exactly what will be garbage-collected.

What won't be GC'd, and what causes OutOfMemoryErrors, is memory that isn't leaked when it should be, i.e. references to objects held beyond their true useful lifetime, typically a reference that is a member variable that should be method-local.

Upvotes: 2

Ira Baxter
Ira Baxter

Reputation: 95334

I don't think Java or the JVM actually define "leak". But your programs are clearly affected by them. I'm sure they define "out of memory".

You have a memory leak, if there is an object that will never be examined again by the application, held by a reference in a scope that is never exited, or held by another object that is examined occasionally. Such a leak may not seriously impact the long term stability of your program.

You have a bad leak if you generate a new object with these properties when some bit of code in the application is run, and that bit of code may run an indefinite number of times. Eventually a program with a bad leak runs out of memory.

Upvotes: 2

Related Questions