JVM - Are WeakReferences collected in minor GC?

前端 未结 6 1175
情歌与酒
情歌与酒 2021-01-05 11:06

I was wondering about that since that would make them less useful. If so, is there a way to make memory weakly referenced only \"garbage\" on major GC?

6条回答
  •  無奈伤痛
    2021-01-05 11:31

    Why do you want that? Your program shouldn't care much about major vs minor GC cycles, and in fact that sort of differentiation won't even exist in all JVMs / GC configurations.

    WeakReferences are collectable as soon as there are no strong references to the object. This could include minor GCs.

    If you want the object to stick around for a while until there is actual memory pressure, try SoftReference instead.

提交回复
热议问题