How can I predict when my memory runs out?

We have a swing based application that performs complex data processing. One of the prerequisites for our software is that any column cannot have too many unique values. If the number is numeric, the user will need to sample the data before they can use our tool.

Unfortunately, the algorithms we use are combinatorially expensive in memory, depending on the number of unique values ​​for each column. Right now, with the wrong data set, the application will quickly run out of memory. Before performing one of these operations that would end in memory, we should be able to calculate approximately how much memory the operation will require. It would be nice if we could check how much memory is currently being used in the application, evaluate whether the application will run out of memory, and display an error message rather than run out of memory. Using java.lang.Runtime, we can find free memory, shared memory and maximum memory, but is it really useful? Even if it turns out, we don’t have enough space for a heap, maybe if we wait 30 milliseconds,The garbage collector will start, and we will have enough heap space to start our operation. Is there anyway to really predict if we run out of memory?

+5
source share
3 answers

I did something similar for a database application where the number of rows that were loaded cannot be estimated. Therefore, in the loop that processes the result set, I call the "MemorWatcher" method, which checks the free memory.

If the available memory is under a certain threshold, the observer will force the collection and reinstallation of garbage. If there is still not enough memory, the watcher method signals this to the caller with an exception. The caller can gracefully recover from this exception - unlike the OutOfMemoryException exception, which sometimes leaves Swing completely unstable.

+2
source

, , -, ASM, , ..

0

, ( -Xms), , - , , .

Soft/WeakReferences , / ?

0

All Articles