Are WeakHashMap cleared during a full GC?

前端 未结 3 1334
遥遥无期
遥遥无期 2021-02-14 06:12

I encountered some troubles with WeakHashMap.

Consider this sample code:

List list = new ArrayList();

Map

        
3条回答
  •  醉话见心
    2021-02-14 06:44

    The just-in-time compiler analyzes the code, sees that anObject and anOtherObject are not used after the loop, and removes them from the local variable table or sets them to null, while the loop is still running. This is called OSR compilation.

    Later the GC collects the strings because no strong references to them remain.

    If you used anObject after the loop you'd still get an OutOfMemoryError.

    Update: You'll find a more detailed discussion about OSR compilation in my blog.

提交回复
热议问题