My approach was to create hundred thousand local collections and populate them with random strings, something like this:
SecureRandom random = new SecureRandom();
for(int i = 0 ; i < 100000 ; i++){
HashMap<String, String> map = new HashMap<String, String>();
for(int j = 0 ; j < 30 ; j++){
map.put(new BigInteger(130, random).toString(32), new BigInteger(130, random).toString(32));
}
}
I have provided -XX:+UseGCOverheadLimit jvm parameter too, but can not get the error. Is there any easy and reliable way/hack to get this error?
I think this should do the trick ... if you run it long enough:
What I'm doing is slowly building up the amount of the non-garbage, while creating a significant amount of garbage at the same time. If this is run until the non-garbage fills nearly all of the heap, the GC will get to a point where the % of time spent garbage collecting exceeds the threshold.
Since you haven't accepted any answer, I'll assume that none of them have worked for you. Here's one that will. But first, a review of the conditions that trigger this error:
So, you have to consume almost all of the heap, keep it allocated, and then allocate lots of garbage. Putting lots of stuff into a
Map
isn't going to do this for you.The
consumeAvailableMemory()
method fills up the heap with relatively small chunks of memory. "Relatively small" is important because the JVM will put "large" objects (512k bytes in my experience) directly into the tenured generation, leaving the young generation empty.After I've consumed most of the heap, I just allocate and discard. The smaller block size in this phase is important: I know that I'll have enough memory for at least one allocation, but probably not more than two. This will keep the GC active.
Running this produces the desired error in under a second:
And, for completeness, here's the JVM that I'm using:
And now my question for you: why in the world would you want to do this?
This:
is scoped within the loop and there are no external (long-term) references to the map created as the loop iterates. Hence each map will be eligible for garbage collection at the end of each loop iteration.
You need to create a collection of objects outside the loop, and use the loop to populate that collection.