Helper to hold a memory object based on the lifetime of another object. The intended use is to assoziate a ByteBuffer with its backing Memory object. The ByteBuffer is held by a WeakReference and a ReferenceQueue is used to track GC of the ByteBuffer. The references to the memory objects are rele
| 38 | * The references to the memory objects are released on access of WeakMemoryHolder. |
| 39 | */ |
| 40 | public class WeakMemoryHolder { |
| 41 | ReferenceQueue<Object> referenceQueue = new ReferenceQueue<>(); |
| 42 | IdentityHashMap<Reference<Object>, Memory> backingMap = new IdentityHashMap<>(); |
| 43 | |
| 44 | public synchronized void put(Object o, Memory m) { |
| 45 | clean(); |
| 46 | Reference<Object> reference = new WeakReference<>(o, referenceQueue); |
| 47 | backingMap.put(reference, m); |
| 48 | } |
| 49 | |
| 50 | public synchronized void clean() { |
| 51 | for(Reference ref = referenceQueue.poll(); ref != null; ref = referenceQueue.poll()) { |
| 52 | backingMap.remove(ref); |
| 53 | } |
| 54 | } |
| 55 | } |
nothing calls this directly
no outgoing calls
no test coverage detected
searching dependent graphs…