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

  • SEARCH
  • Home
  • 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 728769
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 14, 20262026-05-14T06:43:11+00:00 2026-05-14T06:43:11+00:00

I am getting OutOfMemoryError: java heap snippets of the method: { // step 1:

  • 0

I am getting OutOfMemoryError: java heap

snippets of the method:

{
// step 1: I am creating a 2 dim array
  int totalCombination = (int) Math.pow(2.0, (double) vowelCount);
// here vowelCount > 10

// step2: initializing my array
// step3: and using that array
}

My Question:

each time this method is called, that array is getting created.
Is it possible that the array is not getting released .

In windows taskmanager i can see memory used by java is purely incremental.
So it is not that at a point heap size is less, but memory is repetitively used and not released somehow.

Please let me know if you need more detal.

Please help to debug the error.

Anuj

The part of the code which might be causing the error:

int totalCombination = (int) Math.pow(2.0, (double) vowelCount);

    int lookupArray[][] = new int[totalCombination][vowelCount];

    // initialize lookupArray

    for (int i = 0; i < totalCombination; i++) {
        for (int j = 0; j < vowelCount; j++) {
            lookupArray[i][j] = 0;
        }
    }

    // populate lookupArray
    //vowelCount : number of vowels in a word
    // if count is 2, then array will contain 00,01,10,11

    for (int i = 1; i < totalCombination; i++) {
        for (int c = 0; c < vowelCount; c++) {
            lookupArray[i][c] = lookupArray[i - 1][c];
        }
        boolean flag = true;
        for (int j = vowelCount - 1; j >= 0 && flag; j--) {
            if (lookupArray[i - 1][j] == 1) {
                lookupArray[i][j] = 0;
            } else if (lookupArray[i - 1][j] == 0) {
                lookupArray[i][j] = 1;
                flag = false;
            }
        }
    }


  // this part total combination of a word having different combination of vowels in it.


    for (int i = 0; i < totalCombination; i++) {
        int vcount = vowelCount - 1;
        StringBuffer stringBuffer = new StringBuffer();

        for (int j = 0; j < word.length(); j++) {
            if (wordArr[j] == 'a' || wordArr[j] == 'e' || wordArr[j] == 'i'
                    || wordArr[j] == 'o' || wordArr[j] == 'u') {
                if (lookupArray[i][vcount] == 1) {
                    stringBuffer.append(wordArr[j]);
                }
                vcount--;
            } else {
                stringBuffer.append(wordArr[j]);
            }
        }
  • 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-05-14T06:43:11+00:00Added an answer on May 14, 2026 at 6:43 am

    Powers of two grows exponentially. If vowelCount is high, one array alone could easily cause OutOfMemoryError (2^32 = 4GB).

    You can try to tweak your VM maximum memory requirement (e.g. -Xmx512m), but do realize that your algorithm is requiring A LOT OF MEMORY. You may want to find a better algorithm if at all possible.


    See also

    • Wikipedia: exponential growth
      • Wheat and chessboard problem — famous anecdote to show just how surprisingly quick powers of two can grow
    • java application launcher command line options
      • “-Xmxn : Specify the maximum size of the memory allocation pool.”

    After edit: just as I expected, you’re generating a huge array filled with all binary possibilities. You rarely need to actually store this whole array in memory. You can just generate each possible combination “on-the-fly” and feed it to whoever needs the 0s and 1s “just-in-time”.

    Do keep in mind that this is still exponential growth, so even though you’ve taken care of your memory requirement from O(2^N) to just O(N), your time complexity is still O(2^N).

    each time this method is called, that array is getting created. Is it possible that the array is not getting released .

    Yes, that is very possible, if the reference to the array is ever leaked, and then something somewhere holds on to this reference. The garbage collector doesn’t really care what you think is/isn’t garbage; as long as an object is referred to by something (and it’s not a weak reference etc), it’s NOT garbage.


    After figuring out what you’re trying to do, here’s my solution. Note that it doesn’t generate an array of bits at all.

    static void generate(String prefix, String suffix) {
        int i = suffix.replaceAll("[aeiou].*", "").length();
        if (i == suffix.length()) {
            System.out.println(prefix + suffix);
        } else {
            generate(prefix + suffix.substring(0, i), suffix.substring(i + 1));
            generate(prefix + suffix.substring(0, i+1), suffix.substring(i + 1));
        }
    }
    
    // generate("", "apple");
    

    It uses regex to find where the next vowel is. You can use a regular for-loop instead and the general algorithm will still work. You can optimize it to use StringBuilder instead (I’m mostly going for conciseness and hopefully clarity in this snippet).


    Here’s an alternative solution that uses split to pre-chop the input string into pieces (O(N) space), then uses a StringBuilder to generate all the other strings (O(N) space).

    static void generate(StringBuilder sb, String[] parts, int i) {
        if (i == parts.length) {
            System.out.println(sb.toString());
        } else {
            if ("aeiou".contains(parts[i])) {
                generate(sb, parts, i + 1);
            }
            sb.append(parts[i]);
            generate(sb, parts, i + 1);
            sb.setLength(sb.length() - parts[i].length());
        }
    }
    static void generate(String s) {
        generate(
            new StringBuilder(),
            s.split("(?<=[aeiou])|(?=(?!^)[aeiou])"),
            0
        );
    }
    
    // generate("apple");
    

    The regex splits "apple" into [ "a", "ppl", "e" ]. It splits everywhere after a vowel, or (if it’s not the beginning of the string) everywhere before a vowel.

    It should be obvious now that the space requirement is O(N), so unless your string is ridiculously long, this should not cause OutOfMemoryError.

    Of course, if you’re storing the generated strings — all O(2^N) of them — in memory then of course you’ll get OutOfMemoryError. I hope this fact is obvious.

    The entire idea is to not store in memory anything that you don’t need to generate this HUGE OUTPUT. If you then store all of this HUGE OUTPUT in memory (instead of, say, printing them to stdout or a file) then it defeats the whole purpose and you’ll get an OutOfMemoryError as expected.

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

Sidebar

Ask A Question

Stats

  • Questions 368k
  • Answers 368k
  • Best Answers 0
  • User 1
  • Popular
  • Answers
  • Editorial Team

    How to approach applying for a job at a company ...

    • 7 Answers
  • Editorial Team

    What is a programmer’s life like?

    • 5 Answers
  • Editorial Team

    How to handle personal stress caused by utterly incompetent and ...

    • 5 Answers
  • Editorial Team
    Editorial Team added an answer The Entity Framework designer is terrible - I've had the… May 14, 2026 at 5:11 pm
  • Editorial Team
    Editorial Team added an answer If by "hijack" you meant sniff the packets then what… May 14, 2026 at 5:11 pm
  • Editorial Team
    Editorial Team added an answer If you want two actions to be atomic, embed them… May 14, 2026 at 5:11 pm

Trending Tags

analytics british company computer developers django employee employer english facebook french google interview javascript language life php programmer programs salary

Top Members

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.