Our application running on OSGI container and experiencing OutOfMemory - Metaspace error. While analyzing the heap dump in MAT, I found the following classes has big amount of Retained size.
Class A:
public class MyClassA{
.......
private static final Gson GSON = new GsonBuilder().excludeFieldsWithoutExposeAnnotation()
.serializeNulls()
.create();
......
private static final Map<String, String> map = new HashMap<>();
static{
map.put("key1","value1");
}
}
As per my understanding, a class cannot be garbage collected if it has a reference. In this case, static object GSON
has a reference to MyClassA and the classloader of MyClassA also cannot be garbage collected.
Another reason I am thinking is that, map
object also has a static reference which also preventing classloader.
Can you please let me know is my understanding correct?