【高级java程序员应该知道的小知识】weak reference

来源:互联网 发布:印度人是黄种人吗 知乎 编辑:程序博客网 时间:2024/05/01 15:23

本篇态度: simple & stupid


weak reference

Weak reference objects, which do not prevent their referents from being made finalizable, finalized, and then reclaimed. Weak references are most often used to implement canonicalizing mappings.

弱引用不会阻止其引用的对象变成finalizable、finalized状态并最终被GC回收。

finalizer方法:在GC回收对象前执行此方法,一般此方法用于手工释放其它系统资源。
finalizable对象:jvm的GC最终会执行其finalizer方法。
finalized状态:对象的finalizer方法已被自动执行。

用法:

    WeakReference<Class<?>> ref = map.get(name);     if (ref != null) {       clazz = ref.get();    }

用途:

一般配合WeakHashTable做缓存时使用,不想手工关注某个对象的释放,也不想因为忘记此对象的释放而造成内存泄露的话就用weak reference吧。

【版权所有@foreach_break 转载请注明出处 博客地址http://blog.csdn.net/gsky1986】

1 0