Is there a way to FORCE weak and/or soft referenced objects to be GC'd in Java?

后端 未结 1 590
悲&欢浪女
悲&欢浪女 2021-02-20 01:10

Here\'s my use case. We are trying to narrow down a potential memory leak in an application, and we are using a memory analysis tool to snapshot the heap so we can look for obj

1条回答
  •  有刺的猬
    2021-02-20 01:31

    Calling GC should always release all weakly-reachable objects (assuming the "request" made by calling System.gc is actually granted). If weak references are not getting cleared by GC, it means the objects are at least softly reachable.

    Clearing soft references is trickier, as this is up to the JVM's discretion. The only way to guarantee clearing of softly-reachable objects is to cause an OutOfMemoryError to be thrown. This trick is demonstrated in this discussion.

    0 讨论(0)
提交回复
热议问题