I implemented a Java garbage collector once, so whatever I was able to accomplish is a (weak :) lower bound on what is possible.
In my implementation, there is a small constant amount of additional overhead for each weak reference when it is visited during garbage collection.
So the upshot is: I wouldn't worry about it, it's not a big problem unless you are using zillions of weak references.
Most importantly, the cost is proportional to the number of weak references in existence, not the size of the overall heap.
However, that's not to say that a garbage collector that supports weak references will be as fast as one that does not. The presumed question here is, given that Java supports weak references, what is the incremental cost of using them?
Mine was a simple "stop the world" mark/sweep garbage collector. During garbage collection, it makes a determination for every object whether that object is live or not and sets a LIVE
bit in the object header. Then it goes through and frees all the non-live objects.
To handle weak references you just add the following:
- Ignore weak references when setting
LIVE
bits (i.e., they don't cause the LIVE
bit on the referenced object to be set).
- During the sweep step, add a special check as follows: if the object you're visiting is
LIVE
, and it's a WeakReference
, then check the object that it weakly references, and if that object is not LIVE
, clear the reference.
Small variations of this logic work for soft and phantom references.
Implementation is here if you're really curious.