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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 14, 20262026-05-14T19:08:10+00:00 2026-05-14T19:08:10+00:00

I just wanted to know what happens behind my program when I declare and

  • 0

I just wanted to know what happens behind my program when I declare and initialize a variable and later initialize it again with other values, e.g. an ArrayList or something similar.

What happens in my RAM, when I say e.g. this:

ArrayList<String> al = new ArrayList<String>();
...add values, work with it and so on....

al = new ArrayList<String>();

So is my first ArrayList held in RAM or will the second ArrayList be stored on the same position where the first one has been before? Or will it just change the reference of “al”?

If it is not replaced…is there a way to manually free the RAM which was occupied by the first arraylist? (without waiting for the garbage collector)
Would it help to set it first =null?

Nice greetings,
poeschlorn

  • 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-14T19:08:11+00:00Added an answer on May 14, 2026 at 7:08 pm

    The code you post will allocate a new ArrayList instance. If you want to reuse the same one, you can do this:

    ArrayList<String> al = new ArrayList<String>();
    ...add values, work with it and so on....
    
    al.clear();
    // now you can use a1
    

    But do this with caution – if you pass the initial instance of a1 to other code that will be using it for a longer period, then clearing it will cause problems and you will need a separate instance.

    But also note that the savings you get from recycling object arrays and ArrayLists aren’t that great. If you were storing 10 x 4096 byte strings in the ArrayList, the array list itself only occupies space proportional to the size of the references, e.g. circa 4 bytes x 10 = 40 bytes. This is a simplification, but the principle is correct. So, even if you reuse the same array list, you are only saving yourself the memory used to store the object references, not the objects themselves. With that in mind, and the risks of causing bugs by modifying a collection unintentionally, I would guess most people don’t bother recycling lists.

    The memory management in a modern VM is really very good, and you should only start introducing memory “optimizations” when you see that there is a need for it. In fact, using objects for longer than their natural lifetime can have a negative effect on garbage collection performance.

    My advice is, code it clearly first, profile, and only focus on optimizing memory use when you see there is a problem and have identified the cause.

    Good luck!

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

Sidebar

Related Questions

Just wanted to know something about signed vs unsigned interpretation. Am I right if
Just wanted to know how i would replace sitename.com with sitename2.com whenever a user
Just wanted to know. Is it possible to highlight text in ElasticSearch on an
Just wanted to know if anyone is really using Objects and Collections in Oracle
Just wanted to know if there is a big chance to inject SQL While
Just wanted to know if overriding UITabBarController would get my app rejected? Is it
Just wanted to know if is possible to use mod_rewrite on a single(or more
I just wanted to know if I need to place a check for null
I just wanted to know how to get the logged in apple id in
I just wanted to know how to configure FCKEditor to upload files and images

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.