I am testing a game solver that tracks solved positions in a Map<Long, Integer> solvedPositions where Long is positionID and Integer holds smallest known ply count to reach this position. Solver handles smaller boards, but causes java.lang.OutOfMemoryError: GC overhead limit exceeded on larger boards. Increasing memory size is impractical because a large enough board would have more positions than can fit in memory on a given computer.
I would like to do this:
...
boolean isTimeToTrimRecords = willResizedMapExceedMemLimit();
if(isTimeToTrimRecords){
    int maxDepthOfRecordedPlies = getMaxDepth();
    removeLastPly(solvedPositions, maxDepthOfRecordedPlies);
    setMaxDepthOfRecordedPlies(maxDepthOfRecordedPlies-1);
    }
...
public void removeLastPly(Map<Long, Integer> solvedPositions, int maxDepthOfRecordedPlies){
    for (Map.Entry<Long, Integer> position : solvedPositions.entrySet()) {
                    Long positionID = position.getKey();
                    Integer value = position.getValue();
                    if(value==maxDepthOfRecordedPlies){
                        solvedPositions.remove(positionID);                 
                        }
                }
    }
I can check if Map size exceeds certain value as a trigger for trim, but is there a native way to check if JVM is close to memory limit?
 
     
     
    