4

This is homework, not going to lie. I need to write a program which will generate "java.lang.OutOfMemoryError: PermGen space" error.

Since I wasn't able to attend the lecture, I did hours of research yesterday and this is what I have so far.

At first I created a program and I got constantly this error:

java.lang.OutOfMemoryError: GC overhead limit exceeded

Okay, so I did some more research and understood that I didn't get PermGen error because, although that I created objects(String objects) I didn't use them again, so they were considered Garbage. So I changed my code and got constantly this:

Exception in thread "main" java.lang.OutOfMemoryError: Java heap space

So this is the code I had at that point:

import java.util.ArrayList;
import java.util.List;

public class Test {

public static void main(String[] args) {
    List<String> test = new ArrayList<String>();
    test.add(new String(""));

    for (;;) {
        for (int i = 0; i<test.size(); i++){
            test.add(test.get(i));
        }
    }
}
}

Also under VM arguments I had "-XX:PermSize=2m" (I have tried different values). I was told that my code is wrong because it uses the same String over and over. So I tried to change that but I was still unsuccessful. Then I found this code: (Algorithms that lead to java.lang.OutOfMemoryError: PermGen space error)

Random rnd = new Random();
List<String> interned = new ArrayList<String>();
for (;;) {
    int length = rnd.nextInt(100);
    StringBuilder builder = new StringBuilder();
    String chars = "abcdefghijklmnopqrstuvwxyz";
    for ( int i = 0; i < length; i++ ) {
        builder.append(chars.charAt(rnd.nextInt(chars.length())));
    }
    interned.add(builder.toString().intern());
}

So if I understand correctly this should give me PermGen error? But I still get java heap error.

Also I found this: http://javaeesupportpatterns.blogspot.com/2011/10/java-7-features-permgen-removal.html Again if I understand correctly, then when using java7 maybe the java heap space error is the one I should be getting? That it's not possible to get PermGen error anymore? Okay, so I've tried to change the compiler and project version to java 6. But still got Java heap space error.

I know it's my fault that I didn't attend the lecture, but I could use some help to understand what am I doing wrong here or what am I missing?

Community
  • 1
  • 1
Artur K.
  • 3,263
  • 3
  • 38
  • 54
  • 1
    Oh, regarding your examples - they both works fine for me. Its just you have to specify Max permanent generation size: -XX:MaxPermSize=2M as well (from what I can see you're specifying only initial one: -XX:PermSize=8M) – Petro Semeniuk Oct 14 '12 at 20:59
  • They did work indeed. Thank you! I had already discarded these as not working, but I never tried again - after I had more information and such. But I would still love to know is it possible and how it is possible to create PermGen error for example with -XX:MaxPermSize=32M, XX:PermSize=32M. – Artur K. Oct 14 '12 at 23:34
  • 1st example - with perm gen size of 32M you won't be able to hit PermGen space error (as you mentioned in question somebody helped you and said that strings should unique to fill perm gen space). 2nd example - you could get use size of 32M and get PermGen space error with '-Xms64m -Xmx64m -XX:PermSize=32M -XX:MaxPermSize=32M' options. – Petro Semeniuk Oct 15 '12 at 00:32

3 Answers3

6

First, simple one liner how you can reproduce perm gen error (just recursively call main):

public class PermGenError {
    public static void main(String[] args) {
        main(new String[] { (args[0] + args[0]).intern() });
    }
}

Should be launched with parameters:

whatever -XX:PermSize=8M -XX:MaxPermSize=8M

Second, why your example its not producing perm gen error? You usage of string intern is correct. However in latest JMV specification java virtual machine can move internalized strings out from permanent generation to another one. Real life scenario (happened on production :-) ) is that you could see perm gen 99% full, application extremely slow and jvm not throwing any erros because its constantly reshuffling objects between generations.

Third, the best paper which I've read and constantly referring to for any GC/Memory issue is Tuning Garbage Collection with the 5.0 Java[tm] Virtual Machine

EDIT:

Update to question is:

Example works fine, but still doesn't work when I'm specifying large perm size - getting 'Java heap space'

Update to answer is:

There are two aspets you need to be aware when specifying larger perm gen size:

  1. All objects first go to one of younger generations. Therefore if your perm gen size is bigger than size of first generation Java heap space will be thrown first. In this particular example I'm using one large string which exceeds perm gen size in order to reproduce error. If you want to reproduce 'PermGen space' then you need to specify heap size as well. For example:

    • -Xms2048m -Xmx2048m -XX:PermSize=512M -XX:MaxPermSize=512M - will result in 'PermGen space' (large heap, small perm gen)
    • -Xms512m -Xmx512m -XX:PermSize=2048M -XX:MaxPermSize=2048M - will result in 'Java heap space' (small heap, large perm gen)
  2. Second aspect is that the heap is divided into generations and memory in heap itself is fragmented. So when you specifying heap size of 512M you cant fit string of half a gigabyte long into memory (because string internally implemented as array and need to be stored as one sequential chunk in memory). The largest you could fit is probably around half that size(it depends on how badly memory is fragmented).

Petro Semeniuk
  • 6,970
  • 10
  • 42
  • 65
  • First of all thank you for taking time to answer. I feel that this code is purely genius, simple, logical and I didn't even know that you can call main class like that again in code. Definitely will remember this example. So i've tried this code and managed to get PermGen space error when using whatever -XX:PermSize=2M -XX:MaxPermSize=2M What I don't understand is, why it doesn't work with bigger perm gen space. I read some parts of that paper and tried to use -XX:-UseGCOverheadLimit. But I still got Java heap space exception. Although I would have expected to get PermGen space error. – Artur K. Oct 14 '12 at 17:01
  • doesn't work in Java 7, because string pool was relocated to the heap – divideByZero Jun 01 '15 at 16:46
3

The PermGen Space is the memory area, where Java stores class definitions and internal data (which is not the data your application creates and uses). So basically you don't really need a big and/or clever application. This blog page documents briefly, how the Java memory is structured and what are the responsibilities of the different areas.

In theory you get the exception pretty quickly by defining a low perm gen space (as you already did with -XX:PermSize=2m). Then you add a couple of libraries to your applications classpath (Apache commons, maybe HTMLUnit or stuff like that). Download a couple of jars and you'll get this error as soon as the application starts and the classloader is trying to store the class definitions of your jars in the PermGen space.

If this doesn't work try using Tomcat, deploy some libraries and reload the application over and over via the web-app manager.

If you need an algorithm, which causes this problem, you can debug your running application with the JVM paramter -XX:+PrintGCDetails. This will show you the size of the PermGen space used every time the GC was used. This helps you to see whether you go into the right direction and which memory area you fill. The PermGen space stores arrays having references to methods as well, so this could solve your problem as well.

Peter Ilfrich
  • 3,727
  • 3
  • 31
  • 35
1

So if I understand correctly this should give me PermGen error? But I still get java heap error.

java.lang.OutOfMemoryError: GC overhead limit exceeded

Yup. What is going on is that you are hitting a JVM mechanism that is designed to stop the a program that is likely to run out of memory from spending too much time in the process.

See GC overhead limit exceeded

You can disable this using the -XX:-UseGCOverheadLimit switch. I expect you will then get OOME's that say that you are out of permgen memory.


If you are now getting this:

Exception in thread "main" java.lang.OutOfMemoryError: Java heap space

then the problem is that your regular heap is running out of space before you run out of permgen space:

  • Increase the regular heap size.
  • Make sure that your application keeps references to all of those intern'ed Strings. (If they become unreachable. they will be garbage collected and you won't run out of permgen space.)
Community
  • 1
  • 1
Stephen C
  • 698,415
  • 94
  • 811
  • 1,216
  • First of all thank you for taking time to answer. I Checked out the reference you gave me among some other pages. Although after using -XX:-UseGCOverheadLimit switch I still wasn't able to get permgen memory error. Although I would have expected that also. It worked when I set the XX:MaxPermSize=2M, but the switch wasn't needed with such low maximum. But when trying to get this error with bigger XX:MaxPermSize(4M) then using OverHeadLimit switch didn't do the trick. I think I'm still not understanding something fully. – Artur K. Oct 14 '12 at 17:19
  • So what message are you getting in the OOME now? – Stephen C Oct 15 '12 at 01:48
  • `Exception in thread "main" java.lang.OutOfMemoryError: Java heap space` – Artur K. Oct 15 '12 at 12:07