public class MemoryUsageTracker
extends java.lang.Object
WeakReference
s are used internally, so that shortly after a track(Object, long)
ed object is GC'd,
the getBytesUsed()
value decrements appropriately.
Modifier and Type | Field and Description |
---|---|
static MemoryUsageTracker |
SOFT_REFERENCES
Shared instance for use for tracking memory used by
SoftReference . |
Constructor and Description |
---|
MemoryUsageTracker() |
Modifier and Type | Method and Description |
---|---|
long |
getBytesUsed() |
void |
track(java.lang.Object instance,
long bytesUsedByInstance) |
public static final MemoryUsageTracker SOFT_REFERENCES
SoftReference
.
Callers should only use this field to track(Object, long)
objects which have (or will soon have)
given up their strong references, so that only soft or weak references remain.
Provided size estimates are accurate, getBytesUsed()
will report
the amount of used memory which is reclaimable by collecting soft references.
This is particularly handy for tracking SoftReference
s, because otherwise you can quickly get to a state
where Runtime.freeMemory()
looks very low.