0

I have application running on HotSpot jvm 1.8.0_45 with well packed 8GB heap. Application tries to allocate memory for new objects fails with heap space OOM error. I have looked at heap dump and found that most part of space are occupied by charBufferCaches of T4CConnection instances. This caches hold SoftReferences for char arrays. I was surprised that SoftReferences was not released before OOM. I double checked if there is a hard references for this arrays but didn't found one.

Why do I have heap space OOM when application hold 3GB of char arrays by SoftReferences? Why this SoftReferences not released when application need new memory?

Part of T4CConnection object showing charBufferCache:

part of T4CConnection object showing charBufferCache

Incoming references for char array holded at T4CConnection charBufferCache:

incoming references for char array holded at T4CConnection charBufferCache

nukie
  • 691
  • 7
  • 14
  • when was the dump captured? it's possible that the array was held alive by something on the stack and that's not reflected in the dump after the stack was unwound due to the exception. also, the specific type of OOM matters, e.g. an overhead exceeded one might happen even if the allocation request could have been fulfilled after freeing soft references. – the8472 Mar 04 '16 at 23:28
  • dump was created on with +HeapDumpOnOutOfMemoryError JVM option. I thought snapshots produced by this option should contain all objects and references at the moment of OOM error appearance. Is there any chance for some references to be absent in snapshot? Also, my application not consuming memory by big frames - there is no chance it tries to allocate 2GB object at once. – nukie Mar 06 '16 at 09:07
  • Yes, i think that option does include stack roots. but the exact type of OOME still might make a difference. – the8472 Mar 06 '16 at 15:12
  • there was `java.lang.OutOfMemoryError: Java heap space` error – nukie Mar 07 '16 at 12:53

2 Answers2

1

Ideally soft-references are supposed to be cleared before throwing OOM. I am suspecting there is some memory leak in the program..

You may want to take a look at here - How to cause soft references to be cleared in Java?

try -XX:SoftRefLRUPolicyMSPerMB=<value> parameter to limit the size of soft-references and check if it helps. This may provide a direction to next step.

Community
  • 1
  • 1
Anand Vaidya
  • 1,374
  • 11
  • 26
-1

A SoftReference or a WeakReference will only be cleared if the object is cleaned up. It it will not prevent an object being collected, but if that object has a strong reference somewhere, it will be retained after a GC.

e.g.

Double d = new Double(123456);
WeakReference<Double> ref = new WeakReference<>(d);
System.gc();
System.out.println(ref.get() + " == " + d); // both not null.
Peter Lawrey
  • 525,659
  • 79
  • 751
  • 1,130
  • thank you, @Peter Lawrey! I understand, that weak/soft magic won't work if application holds strong references. I have searched for incoming references for each suspicious SoftReferenece referent object at my heap dump. I didn't found any reference for thous char arrays beside soft reference containers... This is the main point of my problem: I have live soft references for objects and no strong references for this arrays... – nukie Mar 04 '16 at 09:35