Search |
||
Understanding Weak ReferencesPosted by enicholas on May 4, 2006 at 5:06 PM PDT
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:
creates a new 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 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
This might look okay on the surface, but the strong reference to 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:
and then elsewhere in the code you can use To solve the "widget serial number" problem above, the easiest thing to do is use the built-in Reference queues Once a The 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
Phantom references A phantom reference is quite different than either The difference is in exactly when the enqueuing happens. What good are Second, With Arguably, the 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. »
Related Topics >>
Java Desktop Comments
Comments are listed in date ascending order (oldest first)
Great Post
Submitted by kramer2718 on Tue, 2010-01-26 13:37.
Well explained, great topic, and (I am embarrassed to say) one I was not previously familiar with.
thanks
Submitted by ustad1024 on Wed, 2010-01-27 19:14.
This article was quite helpful - thanks. It would be even better if you could provide a few coding examples of Weak Reference use.
gamesliga-gamesliga
Submitted by bizimoglan on Tue, 2010-02-02 13:45.
gamesliga-gamesliga üyelik-gamesliga referans-v-pills gold-v-pills-film izle-film seyret-sinema izle-bedava film izle-online film izle-film indir-full film izle-indirmeden film izle-football pictures-arda turan-arda turan resimleri-makine mühendisi-makina mühendisi-tatil-tatil köyü-tatil sepeti-ucuz tatil-bayram tatil-balayı tatili-balayı otelleri-araba-arabalar-araba resimleri-modifiyeli arabalar-audi resimleri-bmw resimleri-ferrari resimleri-mercedes resimleri-peugeot resimleri-pejo resimleri-porsche resimleri-porşe resimleri-uçak-uçaklar-uçak resimleri-araba resimleri-world cup 2010-2010 dünya kupası-arda turan-arda turan resimleri-araba kiralama-oto kiralama-rent a car-car rental-otomobil kiralama-araç kiralama-miami rent a car-miami car rental-miami cheap car rental-miami cheap rent a car-miami hire a car-lazer epilasyon-tatil otelleri-tatil köyü-tatil köyleri-tatil-ucuz tatil-cephe kaplama-dış cephe kaplama-dış cephe kaplaması-siding-silikon cephe kaplaması-güneş kırıcı-terracotta kaplama-kapaklı dış cephe-kompozit-pvc kaplama-fiber cement kaplama-film izle-film seyret-bedava film izle-online film izle-sinema izle-indirmeden film izle-film indir-online sinema-ilaçlama-halı yıkama-ilaçlama-bebek-bebekler-bebek resimleri-termal-termal otel-thermal-termal oteller-şarkı sözü-şarkı sözleri-film izle-indirmeden film izle-sohbet-chat-okey oyna-flaş oyun oyna-flaş oyunlar-video izle-video seyret-youtube-gelinlik-gelinlikler-gelinlik modelleri-2009 gelinlikleri-2010 gelinlikleri-tesettürlü gelinlikler-tesettürlü gelinlik modelleri-örgü-dantel-çiçek-çiçek türleri--çiçekler
|
||
|
Thanks