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 8844185
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 14, 20262026-06-14T11:25:21+00:00 2026-06-14T11:25:21+00:00

I am having heap space errors with the below code. Anybody have any idea

  • 0

I am having heap space errors with the below code.

Anybody have any idea how to optimize this code.

This happens for large files [180MB]. The method parameter has around 50 metatag key-values corresponding to each locale. The error shows up after handling 4500 pages.

Note: I tried changing foreach to iterator to use iterator.remove() for freeing up space.

public static String myChildPropsToString(final UnicodeProperties myLayoutProps) {       
    final StringBuilder sb = new StringBuilder(myLayoutProps.size());

    final String[] matchTarget = new String[] { StringPool.RETURN_NEW_LINE, StringPool.NEW_LINE, StringPool.RETURN };
    final String[] replaceTargetBy = new String[] { "_SAFE_NEWLINE_CHARACTER_", "_SAFE_NEWLINE_CHARACTER_",
            "_SAFE_NEWLINE_CHARACTER_" };

     //COMMENTED TO TRY OUT ITERATOR.REMOVE
    //
    // for (final Map.Entry<String, String> entry : myLayoutProps.entrySet()) {
    // final String value = entry.getValue();
    //
    // if (Validator.isNotNull(value)) {
    // StringUtil.replace(value, matchTarget, replaceTargetBy);
    //
    // sb.append(entry.getKey());
    // sb.append(StringPool.EQUAL);
    // sb.append(value);
    // sb.append(StringPool.NEW_LINE);
    // }
    // }

    final Iterator<Entry<String, String>> propsIterator = myLayoutProps.entrySet().iterator();
    while (propsIterator.hasNext()) {
        final Entry<String, String> entry = propsIterator.next();

        if (Validator.isNotNull(entry.getValue())) {
            StringUtil.replace(entry.getValue(), matchTarget, replaceTargetBy);

            sb.append(entry.getKey());
            sb.append(StringPool.EQUAL);
            sb.append(entry.getValue());
            sb.append(StringPool.NEW_LINE);
        }
    }
    propsIterator.remove();

    return sb.toString();
}

From my code I am setting this to a parent properties obj as follows :

UnicodeProperties myParentProps = new UnicodeProperties();
//Set some values to parent
UnicodeProperties myLayoutProps = new UnicodeProperties();
//Set some values to child
....
myParentProps.setProperty("childProp",myChildPropsToString(myLayoutProps));

Any help would be deeply appreciated !

  • 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-14T11:25:22+00:00Added an answer on June 14, 2026 at 11:25 am

    Try putting the propsIterator.remove(); inside the while.

    final Iterator<Entry<String, String>> propsIterator = myLayoutProps.entrySet().iterator();
        while (propsIterator.hasNext()) {
            final Entry<String, String> entry = propsIterator.next();
    
            if (Validator.isNotNull(entry.getValue())) {
                StringUtil.replace(entry.getValue(), matchTarget, replaceTargetBy);
    
                sb.append(entry.getKey());
                sb.append(StringPool.EQUAL);
                sb.append(entry.getValue());
                sb.append(StringPool.NEW_LINE);
            }
            propsIterator.remove();
        }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Having recently attempted to increase the heap space and ratio of new to old
what are the consequences (if any) of having big objects stored on the heap
I've been having this memory leak issue for days and I think I have
Heap - Sort Algorithm The problem I am having is this, this algorithms n
I'm having issues when I take screenshots. This is my test code. import java.awt.Robot;
I am having a heap space problem. My program is simple. There are two
Having trouble with each function... Will try to explain by example... In my code,
Having used storyboards for a while now I have found them extremely useful however,
Having trouble accessing javascript code in a mixed html/js ajax response. jQuery ajax doc
I have really large file with approximately 15 million entries. Each line in the

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.