There is no reason to think that SoftReference
or WeakReference
don't work for any Java (tm) platform or for Android. There is an Android bug report that talks about difference between Java (tm) and Android behavior: Android clears soft references more "eagerly" than Java (tm). However, the analysis states that the differences are:
- by design, and
- within the "semantic envelop" of the specification; i.e. the Java(tm) javadocs.
However, what I don't understand is how you propose to use Reference objects to implement return of objects (to the pool). If the code that has been allocated a buffer drops its (strong) reference to the object, then the weak or soft reference will be cleared before the reference is enqueued. That means that the buffer will be GC'd before the buffer pool's ReferenceQueue gets to hear about it.
On the other hand, if you are merely using the weak / soft reference so that the pool isn't a memory leak ... that's OK. SoftReference
is the right choice.
SoftReference is not useful for explicit pool sizing. Soft references only respond to memory pressure, and you have little control over that.
Finally, References and reference queues incur appreciable GC overheads. While they are unbroken, the GC must mark them when it encounters them. When the GC breaks them, there is an appreciable overhead in enqueing them, and in processing the queued Reference.