Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Do something just before running out of memory

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?

like image 404
Bart van Heukelom Avatar asked Oct 25 '22 11:10

Bart van Heukelom


1 Answers

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.

like image 57
McDowell Avatar answered Nov 08 '22 09:11

McDowell