nukie
nukie

Reputation: 681

OOM error and weird SoftReferences

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

Upvotes: 0

Views: 410

Answers (2)

Peter Lawrey
Peter Lawrey

Reputation: 533820

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.

Upvotes: -1

Anand Vaidya
Anand Vaidya

Reputation: 1461

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.

Upvotes: 1

Related Questions