This is homework, not a lie. I need to write a program that will generate the error "java.lang.OutOfMemoryError: PermGen space".
Since I could not attend the lecture, I did some research yesterday, and this is what I still have.
First I created a program, and I constantly got this error:
java.lang.OutOfMemoryError: GC overhead limit exceeded
Ok, so I did some more research and realized that I did not get a PermGen error, because although I created the objects (String objects), I did not use them again, so they were considered Garbage. So I changed my code and constantly changed this:
Exception in thread "main" java.lang.OutOfMemoryError: Java heap space
So this is the code that I had at that moment:
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 in the VM arguments I had "-XX: PermSize = 2m" (I tried different values). I was told that my code is wrong because it uses the same line again and again. Therefore, I tried to change this, but I still did not succeed. 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, should this give me a PermGen error? But I still get java heap error.
I also found the following: http://javaeesupportpatterns.blogspot.com/2011/10/java-7-features-permgen-removal.html Again, if I understand correctly, when using java7 it is possible that an obvious error in the java heap is the one i should get? What is no longer possible to get PermGen error? Ok, so I tried changing the compiler and project version to java 6. But I still got a Java heap error.
I know that it is my fault that I did not attend the lecture, but could I use some help to understand what I am doing wrong here or what I am missing?