Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8685195
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 12, 20262026-06-12T22:30:14+00:00 2026-06-12T22:30:14+00:00

This is homework, not going to lie. I need to write a program which

  • 0

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?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-12T22:30:16+00:00Added an answer on June 12, 2026 at 10:30 pm

    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).

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

This is not homework, I need this for my program :) I ask this
This is a homework question. Frankly, I'm not sure how a C program delivers
Preface: This is not a homework question. I'm going through an algos book in
This is not a homework problem. I am only going over a freely available
This is not homework. I am a beginner (novice) java programmer, trying to read
This is not homework . I am interested in setting up a simulation of
Please note that this is not homework and i did search before starting this
well I'm doing this homework but I'm still not viewing the fault... When I
To clarify before I begin, this is NOT homework but rather I am studying
this one is not homework, it just the fact that i've been out of

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.