How to reproduce Java OutOfMemoryError - GC overhead limit exceeded

前端 未结 3 1121
天涯浪人
天涯浪人 2020-12-29 07:30

My approach was to create hundred thousand local collections and populate them with random strings, something like this:

    SecureRandom random = new Secure         


        
3条回答
  •  遥遥无期
    2020-12-29 07:43

    I think this should do the trick ... if you run it long enough:

    HashMap map = new HashMap();
    for (long i = 0; true; i++) {
        for (int j = 0; j < 100; j++) {
            String s = "" + j;
            map.put(i, s);
        }
    }
    

    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.

提交回复
热议问题