Reputation: 1431
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?
Upvotes: 3
Views: 133
Reputation: 131396
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?
You could use a Java agent to instrument your program.
Here is the official documentation : https://docs.oracle.com/javase/7/docs/api/java/lang/instrument/package-summary.html and a question about that in SO.
If you are under OpenJDK, you could use the jol tool : http://openjdk.java.net/projects/code-tools/jol/
The idea would be that you determine which is the size in octets that your map should reach to trigger the trim operation on the map.
Then in your application, when your map reachs a some number of elements, you could perform a computation of the octets size of map to check if the threshold was reached and therefore if you should trim it.
Upvotes: 2
Reputation: 140467
You can have a look into the MemoryMXBean to see if that can give you what you are looking for, as it represents:
The management interface for the memory system of the Java virtual machine.
You can find some further reading on that here.
Upvotes: 2