0

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?

Sam
  • 244
  • 2
  • 5
  • 20
  • I think the static map is holding a lot of strings over a period of time. As it is static map those objects won't be garbage collected. If there are duplicate strings in map then try to intern them before putting in map `new String("value1").intern();` – the Hutt Mar 01 '21 at 03:45
  • Are you reloading the bundle `MyClassA` is in, and the old bundles are not being garbage collected? I'm not sure why you're expecting `MyClassA` to be freed. – tgdavies Mar 01 '21 at 03:49
  • @tgdavies, I think bundle of MyClassA is not GCed because as per the heap dump analysis the classloader of that bundle is showing the Path to GC pointing to MyClassA and has a big Retained size. I am expecting MyClassA to be freed because, the classloader is not GCed as shown in MAT analyzer. – Sam Mar 01 '21 at 08:26
  • @onkarruikar, The static map has only 3 keys/values and they are not duplicate. – Sam Mar 01 '21 at 08:28
  • This may help https://stackoverflow.com/questions/2433261/when-and-how-are-classes-garbage-collected-in-java -- I still don't understand why you are expecting the class to be garbage collected. – tgdavies Mar 01 '21 at 08:33

0 Answers0