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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 24, 20262026-05-24T17:10:01+00:00 2026-05-24T17:10:01+00:00

I am having a hard time trying to understand what is going on with

  • 0

I am having a hard time trying to understand what is going on with Strings and memory management in Android (Java).

To simplify, have a look at this simple piece of code:

public void createArray(){
        String s = "";
        String foo = "foo";
        String lorem = "Lorem ipsum ad his scripta blandit partiendo, eum 
                 fastidii accumsan euripidis in, eum liber hendrerit an.";
        ArrayList<Item> array = new ArrayList<Item>();

        for( int i=0; i<20000; i++ ){
            s = foo.replace("foo", lorem);
            array.add( new Item(s) );
        }
        System.gc();
    }

private class Item{
    String name;

    public Item(String name){
        this.name = name;
    }
}

If executed, createArray will allocate more than 5Mb in memory. Even with the System.gc() instruction, memory won’t be deallocated after the loop.

Now, if we replace s = foo.replace("foo", lorem); with s = lorem;, allocated memory will only increase by 0.5Mb.

I need to understand what is going on to improve my application’s performance.

Can anybody explain how should I replace Strings in a case like this? And why is System.gc() not deallocating memory?

Thanks.

UPDATE:

Thanks for the answers, now I understand that System.gc() is only a hint.

To clarify the other question (the important one):

How can I dynamically generate 20000 Strings (“foo1”, “foo2″… “foo20000”) add them to the ArrayList and do not run out of memory? If this 20000 strings were static they wouldn’t allocate more than 0.5Mb in memory. Besides, if s = foo.replace("foo", lorem) creates a brand new String, why my function allocates 5Mb which is 10 times more memory? Shouldn’t be around 1Mb?

(I am already thinking in a workaround but I want to be sure there is no way to generate the strings dynamically without using this huge amount of memory)

  • 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-24T17:10:03+00:00Added an answer on May 24, 2026 at 5:10 pm

    replace is generating a brand new String object every time it is called, since strings in Java are immutable and so modifications cannot be made “ìn-place”. Also, you’re adding that String to an ArrayList which will hold a reference to the new object, preventing it from being collected.

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

Sidebar

Related Questions

I'm having a real hard time trying to figure out how to fix this
I am having one heck of a hard time trying to figure this out.
i'm having hard time trying to understand how interrupts work. the code below initialize
I've been having a hard time trying to understand PyPy's translation. It looks like
I'm having a hard time trying to understand a problem in my iphone app.
I'm having a hard time trying to make this work. I'm trying to execute
I'm trying to better understand c, and I'm having a hard time understanding where
I am having a really hard time trying to get this to work. All
I'm having a hard time trying to solve this. Here is my sample code:
I'm having a hard time trying to create the query. This is the file:

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.