Failed to create PermGen error

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?

+4
source share
3 answers

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

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

Should start with parameters:

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

Second , why does your example not cause perm gen error? You are using the intern intern string correctly. However, in the latest JMV specification, a Java virtual machine can move internalized strings from a constant generation to another. The real-life scenario (happened in production :-)) is that you can see that perm gen is 99% full, the application is extremely slow and jvm does not throw any erros, because it constantly shuffles objects between generations.

Third , the best work I read and constantly referring to any problem with GC / memory is Tuning Garbage Collection with Java 5.0 virtual machine [tm]

EDIT :

Update for the question:

The example works fine, but still doesn't work when I specify a large size area - getting a "Java heap heap"

Update for answer:

There are two systems you need to know when specifying a larger size:

  • All objects first pass into one of the younger generations. Therefore, if your size of the permanent size is larger than the size of the first heap of the first generation Java heap, it will be thrown first. In this particular example, I use one large line that is larger than the perm gen size to reproduce the error. If you want to play "PermGen space", you also need to specify the heap size. For instance:

    • -Xms2048m -Xmx2048m -XX: PermSize = 512M -XX: MaxPermSize = 512M - will result in a "PermGen space" (large heap, small gen variable)
    • -Xms512m -Xmx512m -XX: PermSize = 2048M -XX: MaxPermSize = 2048M - will result in a "Java heap heap" (small heap, large gen variable)
  • The second aspect is that the heap is divided into generations, and the memory in the heap itself is fragmented. Therefore, when you specify a 512M heap size, you cannot insert a half gigabyte string into memory (because the string is internally implemented as an array and must be stored as one consecutive fragment in memory). The biggest thing you could fit is probably about half that size (it depends on how much memory is fragmented).

+6
source

PermGen Space is a memory area where Java stores class definitions and internal data (which is not the data that your application creates and uses). So basically you donโ€™t need a big and / or smart application. This blog page briefly describes how Java memory is structured and what are the responsibilities of various areas.

In theory, you get an exception pretty quickly by defining a low perm gen space (as you already did with -XX:PermSize=2m ). Then you add several libraries to your application class path (e.g. Apache, possibly HTMLUnit or the like). Download a couple of cans, and you will get this error as soon as the application starts and the class loader tries to save the class definitions of your cans in the PermGen space.

If this does not work, try using Tomcat, expand several libraries and restart the application again and again through the web application manager.

If you need an algorithm that causes this problem, you can debug the running application with the JVM -XX:+PrintGCDetails . This will show you the size of the PermGen space used each time you use the GC. This will help you understand whether you are going in the right direction and what area of โ€‹โ€‹memory you are filling. PermGen space stores arrays containing method references, so this may also solve your problem.

+3
source

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

java.lang.OutOfMemoryError: GC upper limit exceeded

Yeah. What happens is that you click on the JVM mechanism, which is designed to stop the program, which is likely to run out of memory, spending too much time on the process.

See SSL protocol limit exceeded

You can disable this by using the -XX: -UseGCOverheadLimit switch. I expect you to then get OOME, which says you are from the memory of permgens.


If you are getting this now:

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

then the problem is that your regular heap ends out of space before you finish the permenz space:

  • Increase the size of the regular heap.
  • Make sure your application maintains links to all of these interned strings. (If they become inaccessible, they will collect garbage, and you will not leave the space of the Permians.)
+1
source

Source: https://habr.com/ru/post/1439638/


All Articles