3

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?

2
  • And you did a lot of memory profiling; thus you are sure that there isn't a bug in your code causing leaks (or maybe the generation of a lot of garbage within short periods of time). In other words: are you sure that your application really needs all that memory? Commented Dec 17, 2016 at 20:22
  • I'd double check / profile your app, or for some estimated values, use Runtime: stackoverflow.com/questions/2015463/… Commented Dec 17, 2016 at 20:25

2 Answers 2

2

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.

Sign up to request clarification or add additional context in comments.

Comments

2

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.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.