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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 28, 20262026-05-28T17:21:18+00:00 2026-05-28T17:21:18+00:00

If I have a loop and create a new object inside it for (

  • 0

If I have a loop and create a new object inside it

for ( int i ; i < 10 ; i++)
{
  MyObject obj = new MuObject();
   obj.use();
}

Do I need to say obj = null, inside the loop at the beginning or end to release memory used by that object , or by using “new” that object will be send to GC ? and can I see this in terms of memory usage ?

update : so in case I have big object and long loop , should I assign the object to null or no ?

  • 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-28T17:21:19+00:00Added an answer on May 28, 2026 at 5:21 pm

    Check this: http://javarevisited.blogspot.com/2011/04/garbage-collection-in-java.html

    “An Object becomes eligible for Garbage collection or GC if its not reachable from any live threads or any static references”. After the loop ends, the objects that you created inside the loop do not have any external references pointing to them and are eligible for garbage collection.

    EDIT:
    If you want to see memory usage, you can profile your application using an IDE that has such a feature. For example, NetBeans has a nice interface that shows live memory usage for object allocation.

    EDIT 2:
    “so in case I have big object and long loop , should I assign the object to null or no ?”
    No, you do not need to do this. Once one iteration of the loop is complete, there are no active references to any objects created in that iteration so it does not matter that you have a long or short loop.

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

Sidebar

Related Questions

Let's say I have the following code... StringBuilder sb = new StringBuilder(); for (int
I have created some python code which creates an object in a loop, and
I need to suck data from stdin and create a object. The incoming data
I have loop to create a column of button and now i wish to
How do I loop over a string of item and create an object based
I have a list of Nodes which I loop through: for (int i =
// Member Variable private static readonly object _syncLock = new object(); // Now inside
I have an object which contains 7 items. $obj.gettype().name Object[] $obj.length 7 I want
I want to create a new object and assign some properties for each array
I have the following code in Javascript: jQuery(document).ready(function(){ var actions = new Object(); var

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.