- 浏览: 797401 次
- 性别:
- 来自: 上海
文章分类
- 全部博客 (651)
- Java (39)
- Java 初学者小问题 (66)
- 设计模式 (7)
- 项目管理 (3)
- 数据库 (1)
- 算法 (2)
- Java practices (6)
- Effective Java2读书笔记 (78)
- Linux (2)
- programming ruby 读书笔记 (5)
- Core Java Ninth Edition Volume I 读书笔记 (15)
- Pro Git 读书笔记 (12)
- Git (3)
- Maven in Action 读书笔记 (20)
- Web (12)
- 非技术类书籍 (11)
- 电影 (40)
- Web Cache (1)
- jquery (0)
- 历史 (4)
- Dive Into HTML5 读书笔记 (13)
- 三国演义小学毕业考 (79)
- 高效能人士的7个习惯 读书笔记 (12)
- Java Performance 读书笔记 (3)
- Protocol Buffer 学习笔记 (6)
- Mongo DB 学习笔记 (7)
- Morphia 学习笔记 (7)
- Algorithms -- Princeton 学习笔记 (13)
- String研究 (10)
- Hadoop: The Definitive Guide 读书笔记 (3)
- Java与模式读书笔记 (5)
- Date研究 (3)
- The Roman Empire 听课笔记 (4)
- Algorithms -- Standford 学习笔记 (16)
- Core Java Ninth Edition Volume II 读书笔记 (9)
- Thinking in Java 4th Edition 读书笔记 (21)
- Node : Up and Running 学习笔记 (5)
- Eloquent Javascript (8)
- Smashing Node.js 读书笔记 (1)
- Algorithms II -- Standford 学习笔记 (19)
- Algorithm II -- Princeton 学习笔记 (14)
- 网络安全 (2)
- Javascript (4)
- 正则表达式 (1)
- JAVA 7/8 (15)
- JVM (10)
- NodeJS (1)
- 鸟哥的linux私房菜读书笔记 (14)
- Web Service (1)
- The art of programming (9)
- Introduction to Algorithm 读书笔记 (4)
- Java 源码阅读 (0)
- Spring in Action 读书笔记 (2)
- Java Network Programming 读书笔记 (2)
最新评论
-
心存高远:
谢谢作者分享,刚好看到这里不太明白,现在茅塞顿开。不过runt ...
关于 Maven的传递依赖的理解 -
sxlkk:
851228082 写道甚至在某次技术会议现场遇到《Maven ...
关于 Maven的传递依赖的理解 -
851228082:
851228082 写道a----compile----b-- ...
第五章 坐标和依赖 -
851228082:
a----compile----b-----provided- ...
第五章 坐标和依赖 -
851228082:
甚至在某次技术会议现场遇到《Maven in action》的 ...
关于 Maven的传递依赖的理解
Some time ago I was interviewing candidates for a Senior Java Engineer position. Among the many questions I asked was "What can you tell me about weak references?" I wasn't expecting a detailed technical treatise on the subject. I would probably have been satisfied with "Umm... don't they have something to do with garbage collection?" I was instead surprised to find that out of twenty-odd engineers, all of whom had at least five years of Java experience and good qualifications, only two of them even knew that weak references existed, and only one of those two had actual useful knowledge about them. I even explained a bit about them, to see if I got an "Oh yeah" from anybody -- nope. I'm not sure why this knowledge is (evidently) uncommon, as weak references are a massively useful feature which have been around since Java 1.2 was released, over seven years ago.
Now, I'm not suggesting you need to be a weak reference expert to qualify as a decent Java engineer. But I humbly submit that you should at least know what they are -- otherwise how will you know when you should be using them? Since they seem to be a little-known feature, here is a brief overview of what weak references are, how to use them, and when to use them.
Strong references
First I need to start with a refresher on strong references. A strong reference is an ordinary Java reference, the kind you use every day. For example, the code:
WeakReference<Widget> weakWidget = new WeakReference<Widget>(widget);
creates a new StringBuffer()
and stores a strong reference to it in the variable buffer
. Yes, yes, this is kiddie stuff, but bear with me. The important part about strong references -- the part that makes them "strong" -- is how they interact with the garbage collector. Specifically, if an object is reachable via a chain of strong references (strongly reachable), it is not eligible for garbage collection. As you don't want the garbage collector destroying objects you're working on, this is normally exactly what you want.
When strong references are too strong
It's not uncommon for an application to use classes that it can't reasonably extend. The class might simply be marked final
, or it could be something more complicated, such as an interface returned by a factory method backed by an unknown (and possibly even unknowable) number of concrete implementations. Suppose you have to use a class Widget
and, for whatever reason, it isn't possible or practical to extend Widget
to add new functionality.
What happens when you need to keep track of extra information about the object? In this case, suppose we find ourselves needing to keep track of each Widget's
serial number, but the Widget
class doesn't actually have a serial number property -- and because Widget
isn't extensible, we can't add one. No problem at all, that's what HashMaps
are for:
serialNumberMap.put(widget, widgetSerialNumber);
This might look okay on the surface, but the strong reference to widget
will almost certainly cause problems. We have to know (with 100% certainty) when a particular Widget's
serial number is no longer needed, so we can remove its entry from the map. Otherwise we're going to have a memory leak (if we don't remove Widgets
when we should) or we're going to inexplicably find ourselves missing serial numbers (if we remove Widgets
that we're still using). If these problems sound familiar, they should: they are exactly the problems that users of non-garbage-collected languages face when trying to manage memory, and we're not supposed to have to worry about this in a more civilized language like Java.
Another common problem with strong references is caching, particular with very large structures like images. Suppose you have an application which has to work with user-supplied images, like the web site design tool I work on. Naturally you want to cache these images, because loading them from disk is very expensive and you want to avoid the possibility of having two copies of the (potentially gigantic) image in memory at once.
Because an image cache is supposed to prevent us from reloading images when we don't absolutely need to, you will quickly realize that the cache should always contain a reference to any image which is already in memory. With ordinary strong references, though, that reference itself will force the image to remain in memory, which requires you (just as above) to somehow determine when the image is no longer needed in memory and remove it from the cache, so that it becomes eligible for garbage collection. Once again you are forced to duplicate the behavior of the garbage collector and manually determine whether or not an object should be in memory.
Weak references
A weak reference, simply put, is a reference that isn't strong enough to force an object to remain in memory. Weak references allow you to leverage the garbage collector's ability to determine reachability for you, so you don't have to do it yourself. You create a weak reference like this:
StringBuffer buffer = new StringBuffer();
and then elsewhere in the code you can use weakWidget.get()
to get the actual Widget
object. Of course the weak reference isn't strong enough to prevent garbage collection, so you may find (if there are no strong references to the widget) that weakWidget.get()
suddenly starts returning null
.
To solve the "widget serial number" problem above, the easiest thing to do is use the built-in WeakHashMap
class. WeakHashMap
works exactly like HashMap
, except that the keys (not the values!) are referred to using weak references. If a WeakHashMap
key becomes garbage, its entry is removed automatically. This avoids the pitfalls I described and requires no changes other than the switch from HashMap
to a WeakHashMap
. If you're following the standard convention of referring to your maps via the Map
interface, no other code needs to even be aware of the change.
Reference queues
Once a WeakReference
starts returning null
, the object it pointed to has become garbage and the WeakReference
object is pretty much useless. This generally means that some sort of cleanup is required; WeakHashMap
, for example, has to remove such defunct entries to avoid holding onto an ever-increasing number of dead WeakReferences
.
The ReferenceQueue
class makes it easy to keep track of dead references. If you pass a ReferenceQueue
into a weak reference's constructor, the reference object will be automatically inserted into the reference queue when the object to which it pointed becomes garbage. You can then, at some regular interval, process the ReferenceQueue
and perform whatever cleanup is needed for dead references.
Different degrees of weakness
Up to this point I've just been referring to "weak references", but there are actually four different degrees of reference strength: strong, soft, weak, and phantom, in order from strongest to weakest. We've already discussed strong and weak references, so let's take a look at the other two.
Soft references
A soft reference is exactly like a weak reference, except that it is less eager to throw away the object to which it refers. An object which is only weakly reachable (the strongest references to it are WeakReferences
) will be discarded at the next garbage collection cycle, but an object which is softly reachable will generally stick around for a while.
SoftReferences
aren't required to behave any differently than WeakReferences
, but in practice softly reachable objects are generally retained as long as memory is in plentiful supply. This makes them an excellent foundation for a cache, such as the image cache described above, since you can let the garbage collector worry about both how reachable the objects are (a strongly reachable object will never be removed from the cache) and how badly it needs the memory they are consuming.
Phantom references
A phantom reference is quite different than either SoftReference
or WeakReference
. Its grip on its object is so tenuous that you can't even retrieve the object -- its get()
method always returns null
. The only use for such a reference is keeping track of when it gets enqueued into a ReferenceQueue
, as at that point you know the object to which it pointed is dead. How is that different from WeakReference
, though?
The difference is in exactly when the enqueuing happens. WeakReferences
are enqueued as soon as the object to which they point becomes weakly reachable. This is before finalization or garbage collection has actually happened; in theory the object could even be "resurrected" by an unorthodox finalize()
method, but the WeakReference
would remain dead. PhantomReferences
are enqueued only when the object is physically removed from memory, and the get()
method always returns null
specifically to prevent you from being able to "resurrect" an almost-dead object.
What good are PhantomReferences
? I'm only aware of two serious cases for them: first, they allow you to determine exactly when an object was removed from memory. They are in fact the only way to determine that. This isn't generally that useful, but might come in handy in certain very specific circumstances like manipulating large images: if you know for sure that an image should be garbage collected, you can wait until it actually is before attempting to load the next image, and therefore make the dreaded OutOfMemoryError
less likely.
Second, PhantomReferences
avoid a fundamental problem with finalization: finalize()
methods can "resurrect" objects by creating new strong references to them. So what, you say? Well, the problem is that an object which overrides finalize()
must now be determined to be garbage in at least two separate garbage collection cycles in order to be collected. When the first cycle determines that it is garbage, it becomes eligible for finalization. Because of the (slim, but unfortunately real) possibility that the object was "resurrected" during finalization, the garbage collector has to run again before the object can actually be removed. And because finalization might not have happened in a timely fashion, an arbitrary number of garbage collection cycles might have happened while the object was waiting for finalization. This can mean serious delays in actually cleaning up garbage objects, and is why you can get OutOfMemoryErrors
even when most of the heap is garbage.
With PhantomReference
, this situation is impossible -- when a PhantomReference
is enqueued, there is absolutely no way to get a pointer to the now-dead object (which is good, because it isn't in memory any longer). Because PhantomReference
cannot be used to resurrect an object, the object can be instantly cleaned up during the first garbage collection cycle in which it is found to be phantomly reachable. You can then dispose whatever resources you need to at your convenience.
Arguably, the finalize()
method should never have been provided in the first place. PhantomReferences
are definitely safer and more efficient to use, and eliminating finalize()
would have made parts of the VM considerably simpler. But, they're also more work to implement, so I confess to still using finalize()
most of the time. The good news is that at least you have a choice.
Conclusion
I'm sure some of you are grumbling by now, as I'm talking about an API which is nearly a decade old and haven't said anything which hasn't been said before. While that's certainly true, in my experience many Java programmers really don't know very much (if anything) about weak references, and I felt that a refresher course was needed. Hopefully you at least learned a little something from this review.
This article originates from http://weblogs.java.net/blog/2006/05/04/understanding-weak-references
发表评论
-
Zz The java.lang.LinkageError: loader constraint violation" demystified
2014-05-13 19:24 1520http://frankkieviet.blogspot.c ... -
zz Java NIO 系列教程
2014-05-09 22:38 1165http://www.iteye.com/magazines ... -
Zz Java NIO Tutorial
2014-05-09 22:34 1786http://tutorials.jenkov.com/ja ... -
Zz Spring IOC 好处和劣势
2014-03-28 18:06 1074IoC是什么? Inversion of Contro ... -
Zz ConcurrentHashMap源码解析
2014-03-16 17:29 668ConcurrentHashMap是Java 5中支持高并发 ... -
Zz Java多线程之ConcurrentHashMap深入分析
2014-03-16 14:50 1182一、Map体系 Hashtable是JDK ... -
Zz ConcurrentHashMap原理分析
2014-03-16 14:03 1127集合是编程中最常用的数据结构。而谈到并发,几乎总是离不开集合 ... -
Zz Java并发编程之ConcurrentHashMap
2014-03-16 13:40 2069ConcurrentHashMap Concurrent ... -
关于ConcurrentHashMap 中位移的问题
2014-03-16 14:33 1262拜读了GoldenDoc的大作:《Java并发编程之Con ... -
ConcurrentHashMap 详解
2014-03-15 21:19 01. ConcurrentHashMap 可以做到读取数 ... -
Why java Arrays use two different sort algorithms for different types?
2014-02-25 22:09 1305Java 7 uses Dual-Pivot Quicks ... -
深入探讨 java.lang.ref 包
2014-02-25 19:58 1093回顾了一下三年多前写的文章https://www.i ... -
Zz Java 中使用内存映射文件需要考虑的 10 个问题
2014-02-09 17:18 948java中的内存映射IO和内存映射文件是什么? 内存 ... -
Zz 10 Things to Know about Memory Mapped File in Java
2014-02-09 17:16 632What is Memory Mapped File ... -
Zz Direct vs non-direct ByteBuffer
2014-02-08 12:46 850先解释一下两者的区 ... -
Zz direct buffer VS non-direct buffer
2014-02-08 10:55 869在java NIO中,有两种不同的buffer:direct ... -
Java Generics 小结
2013-03-07 14:12 01. 泛型类型或泛型类 ... -
关于 Java正则表达式中的Possessive数量修饰词的理解
2013-02-26 21:14 1285正则表达式对于数量限定符如 ?, + , *, {n, m ... -
Run Application in debug mode
2013-01-23 13:59 1313Q: I use the Eclipse IDE to de ... -
一个 Dynamic Proxy 的例子
2012-11-22 11:18 1234最近翻出了两年前老吴让我写的一个MockFacotry , ...
相关推荐
VSoft.WeakReference 该单元背后的思想是提供与Delphi中引用计数对象类似的生命周期,就像WeakReference在.NET中一样。 当涉及循环引用时,delphi中引用计数的对象有一些限制,例如TParent引用它的子对象(通过...
在嵌入式系统开发中,特别是使用ARM架构的硬件平台,`__weak`关键字是一个非常重要的概念,它涉及到链接器的符号解析和程序的优化。本文将深入探讨`__weak`关键字的含义、使用场景以及如何在实际项目中应用。 `__...
在编程世界中,`weak`属性是一个非常关键的概念,尤其在C语言和汇编编程中。这个特性主要用于解决引用计数的问题,防止出现循环引用导致的内存泄漏。当我们谈论`weak`属性时,通常是在讨论对象的生命周期管理和内存...
9、Weak Signal Digital GNSS Tracking Algorithms 10、Fundamentals of Global Positioning System Receivers II 11、Composite GNSS Signal Acquisition over Multiple Code Periods 12、GPS接收机电路设计 13...
WEKA 操作入门(数据挖掘) WEKA 是一个开放的数据挖掘工作平台,用于非商业目的的研究行为,集合了大量能承担数据挖掘任务的机器学习算法,包括对数据进行预处理,分类,回归、聚类、关联规则以及在新的交互式界面...
【标题】:“weak应用程序,数据挖掘平台” 这个标题暗示了一个名为“weak”的应用程序,它是一个专为数据挖掘设计的平台。在IT领域,数据挖掘通常指的是从大量数据中通过使用各种算法来发现有价值的信息和模式的...
在iOS开发中,`weak`关键字是一个至关重要的概念,它主要用于解决内存管理中的循环引用问题。循环引用会导致对象无法被正确地释放,因为每个对象都持有对方的强引用。`weak`属性创建了一个非保留的引用,不增加对象...
"Weak-Password.zip" 这个压缩包文件专门收集了常见的弱口令,旨在帮助用户了解并防范这些潜在的安全风险。弱口令通常指的是那些容易被猜测或破解的密码,它们往往包括简单数字组合、连续数字或字母、常见单词、生日...
弱收敛余经验过程是概率统计专业博士生的必修课程,也是经典书籍。
This project implements a Kotlin wrapper for the almighty (and sometimes scary) weak references so that you can use them in a more friendly way. Usage Weak references are often feared but are one of ...
本文介绍了一种新的弱伽辽金(Weak Galerkin,简称WG)有限元方法,该方法针对多面体网格上的二阶椭圆型偏微分方程。这种方法通过使用离散的弱梯度算子作用于有限元划分上不连续的多项式片段来设计WG有限元方法(WG-...
Android Weak Handler Memory safer implementation of android.os.Handler Problem Original implementation of Handler always keeps hard reference to handler in queue of execution. Any object in Message or...
iOS weak 关键字漫谈 iOS 开发中,weak 关键字是基础知识之一,经常在面试中被问到。在 delegate、block、NSTimer 等场景下使用 weak 关键字,以避免循环引用引发的内存泄漏。这是教科书式的用法。然而,weak ...
`shared_ptr`和`weak_ptr`是两种常见的智能指针类型,分别代表了强引用和弱引用的概念。 `shared_ptr`是C++11引入的一种智能指针,它管理一个动态分配的对象,并在不再有引用指向该对象时自动删除它。`shared_ptr`...
Android弱处理程序 ...WeakHandler比android.os.Handler棘手,它将WeakReferences保留到可运行对象和消息中,一旦不再引用WeakHandler实例,GC便可以收集它们。 用法 将JitPack存储库添加到您的build.gradle中: re
ARC的判断准则: 只要没有强指针指向对象,就会释放对象,弱指针不会这样,及时有弱指针指向对象,对象没有强指针指向,也会自动释放掉。一般,无需显式声明为强指针,但是在封装里,定义方法的时候需要写明。...
逆序超平面排列与弱布吕阿序,范久瑜,,对每个排列$w$,我们可以根据$w$的逆序构造一个超平面排列$mathcal{A}_w$,称为$w$对应的逆序超平面排列。$mathcal{A}_w$中区域的个数小于等于在
研究发现:其实与一个人的工作和事业关系最密切的社会关系并不是“强关系”,而常常是“弱关系”。“弱关系”虽然不如“强关系”那样坚固(金字塔),却有着极快的、可能具有低成本和高效能的传播效率。
本书《A Weak Convergence Approach to the Theory of Large Deviations》由Paul Dupuis和Richard S. Ellis撰写,它属于Wiley概率与统计系列,由John Wiley & Sons出版社出版。本书利用弱收敛方法来探讨大型偏差理论...