Is there a way in Java to do something just before running out of memory. For example, keeping a list of previous document states (for undo) and only removing very old states when memory is about to be exhausted?
For example, keeping a list of previous document states (for undo) and only removing very old states when memory is about to be exhausted?
You may be able to do something with SoftReference
. Note also the general documentation for the java.lang.ref package. If the objects are only reachable via a SoftReference
, they'll be garbage collected before an out of memory error occurs. Note however that VMs aren't required to keep those references prior to such an event or make any guarantees about which order they might be cleared in. You might be best combining them with some form of persistence.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With