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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 14, 20262026-06-14T14:43:43+00:00 2026-06-14T14:43:43+00:00

I have an instance of an object (Object1) whose constructor creates instances of other

  • 0

I have an instance of an object (Object1) whose constructor creates instances of other objects (Object2) and stores them in an ArrayList. Object2 has a static instancecount variable (among other static variables), and instances of Object2 depend on this instancecount. In a test program, I run a for loop where an instance of Object1 are created on each iteration, and then the reference to the instance of Object1 is reassigned a null value. My test program looks something like:

    for (...) {
        Object1 obj = new Object1(...); //which creates several Object2's
        obj.myMethod();
        obj = null;
    }

The problem is that instances of Object2 are not getting garbage collected at the end of each loop iteration, so the static instancecount variable of Object2 just grows and grows (along with memory usage) with every iteration. Is this to be expected? Is there a way to force the cleanup of the Object2 instances so that each loop has a fresh start? Is this a bad program design?

Thanks!
Ryan

  • 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-14T14:43:44+00:00Added an answer on June 14, 2026 at 2:43 pm

    Garbage collection works pretty reliable, so it won’t leak memory or grow indefinitely.

    But GC also only runs when the JVM thinks it needs to, so you cannot force it to run at the end of every iteration of your loop.

    Having code that depends on when GC runs is bad code.

    Also, I am not sure how you are managing the instance count. It is easy to increment it in a constructor, but when do you decrease it again? It certainly won’t happen automatically.

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

Sidebar

Related Questions

I have a seed object which has an instance method seed:fall() which is called
When you have an object instance in C#, you can use the this keyword
If I have an object instance and I know it is actually a boxed
I have a method where I would like to return an object instance of
Possible Duplicate: Java: recommended solution for deep cloning/copying an instance I have an object
I have this object which is an instance of a superclass. I want to
I have some object that is instantiated in code behind, for instance, the XAML
I have to share an instance of a config object in PHP. This object
In C#, suppose you have an object (say, myObject ) that is an instance
I have class MasterData whose instances contain some general information about all possible gadgets.

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.