• Post Reply Bookmark Topic Watch Topic
  • New Topic

plugging in Weak ,Soft and Phantom references  RSS feed

 
Rick Charles
Greenhorn
Posts: 25
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Guyz.

I have been trying to know where these things are being implemented.
I know these things are used to help in the process of GC In which scenario we will go for the following

1.Weak References
2.Soft References
3.Phantom References

Thanks in advance
Rick !
 
M Easter
Ranch Hand
Posts: 133
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
To understand these, it helps to understand the various degrees of "reachable".

From the 1.4.x javadocs: http://tinyurl.com/2x5rpy

Here's my stab at it. Check out this code:



If you create a new object in Java, you have a strong reference. e.g. Strong Ref above. GC will never reclaim this object because you have a strong reference to it.

For weak and soft references, the GC may reclaim the object under certain conditions. A "soft reference" is an object that has a pointer: the pointer may or may not be null.

The difference between soft and weak is subtle. Soft references are easier to understand. The GC will reclaim objects that are "soft reachable" when it gets low on memory. A good example is a memory-sensitive cache where you want to keep some stuff, but it's ok if the GC reclaims it. In the past, I have written a map that uses SoftReferences. (There may also be something similar in Jakarta Commons.) The idea is:



Note that in the above, the SoftReference will stay in the map, but the object pointed to by the soft reference may "disappear" due to GC.

Weak references are somewhat similar but I'll leave it to the docs to explain "weak" versus "soft". Phantom references are intended for the internal workings of the GC and are not appropriate for application development.

[ May 13, 2007: Message edited by: M Easter ]

[ May 13, 2007: Message edited by: M Easter ]
[ May 13, 2007: Message edited by: M Easter ]
 
It is sorta covered in the JavaRanch Style Guide.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!