2

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?

jmj
  • 237,923
  • 42
  • 401
  • 438
Pranalee
  • 3,389
  • 3
  • 22
  • 36
  • That's about it. Profile with VisuamVM to see how much memory is being used where. Run it with increasing loads in a test environment. Profile, test, profile etc. – Boris the Spider May 25 '15 at 19:46
  • 1
    Here is a good article: http://www.toptal.com/java/hunting-memory-leaks-in-java The part on using heap dump to detect which class has multiple instances may lead to which class is leaking : http://stackoverflow.com/questions/2511315/method-for-finding-memory-leak-in-large-java-heap-dumps – Grady G Cooper May 25 '15 at 19:51

2 Answers2

2

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.

Ira Baxter
  • 93,541
  • 22
  • 172
  • 341
2

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.

user207421
  • 305,947
  • 44
  • 307
  • 483