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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 24, 20262026-05-24T06:04:33+00:00 2026-05-24T06:04:33+00:00

This question bothers me quite sometime and i cant seem to find any answer

  • 0

This question bothers me quite sometime and i cant seem to find any answer from oracle java resource. does anyone have any clue on this matter:

I understand that a string is actually an array of char. However, I wonder how is it different when comes to how the data is store in physical memory for the following two cases:

Case 1) total of 10 Strings put inside a hashmap. Each string start with 0 length. and each second, each string is append with 1000 bytes. until it reaches 1MB each

Case 2) total of 10 Strings put inside a hashmap. Each string start with 1M length (with space). and each second, each string is replace with 1000 bytes. until it replaced all the 1MB each.

For case1 will it causes more reference to be made in physical memory because the string length keeps growing and new allocation needs to be made? or does it “push” the data behind so it can allocate the next available memory?

For case2 does it means less reference require (or practically no reference required) because the string was initiate with 1MB length in the first place?

Lastly, i wonder does these two cases has any impact to Garbage Collector or memory allocation performance?

  • 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-24T06:04:34+00:00Added an answer on May 24, 2026 at 6:04 am

    Actually they’re both about the same when dealing with Strings.

    Simply put, Strings are immutable. So, if you have a String of 1000 chars, and append 1000 chars to it, you then have a single String of 2000 chars, and the previous one is available for garbage collection.

    If you have a 1M String and change it, you have a new 1M String, and the old one is available for garbage collection. Since Strings are immutable, there’s no gimmickry of splitting the old string, removing what you want, adding the new and appending the old and new together. Rather, it simply copies it wholesale with the new version.

    There are other structures that behave better, but still have similar issues.

    For example, if you have a StringBuilder, it will behave almost exactly like a normal String in the 1000 + 1000 case. However, if you know this is going to happen, you can pre-allocate it to, say, 10,000 and then it will simply copy in to the pre-allocated space rather than throwing the entire old kit away.

    Another feature of String immutability is that Strings can be shared.

    A simple example is this:

    String a = "abc123";
    String b = a.substring(1, 3);
    

    This will have a single array of 6 chars (“abc123”), but BOTH String will point to this array, the ‘b’ String will point to the offset within the array from the original.

    The downside of this is:

    String a = ".../pinky 1 MILLION characters...";
    String b = a.substr(1, 3);
    a = null;
    

    ‘b’ now points to the original buffer that ‘a’ used, even though it only “sees” 3 characters. So, your ‘b’ String is actually holding on to 1M chars of memory.

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

Sidebar

Related Questions

This question is related to another question I wrote: Trouble using DOTNET from PHP.
This question may seem blindingly obvious and I realise I am putting myself up
Thanks to this answer to a previous question of mine and this article I
this question bothers me this few days. I want to read a string for
This isn't a big deal of a question but it bothers me, so I
There is one thing that bothers me, C++ function pointers. I'm asking this question
Maybe my question is quite strange, but it bothers me for some time and
Now that it's answered: Don't bother reading this question, it's a bit lengthy and
Problably an easy questions but this calculation bothers me.. why is double test =
This question is directly related to this SO question I posed about 15 minutes

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.