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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 15, 20262026-05-15T06:59:56+00:00 2026-05-15T06:59:56+00:00

I am running the following code every two minutes via a Timer: object =

  • 0

I am running the following code every two minutes via a Timer:

object = new CustomObject(this);

Potentially, this is a lot of objects being created and a lot of objects being overwritten. Do the overwritten objects get garbage collected, even with a reference to itself being used in the newly created object?

I am using JDK 1.6.0_13.

Thanks for the help.

  • 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-15T06:59:57+00:00Added an answer on May 15, 2026 at 6:59 am

    We’d need to know what was going on inside the constructor to answer the question fully.

    But generally, as long as nothing retains a reference to the old object when the new one is created, it will be available for garbage collection, even if the old object is used in the process of creating the new one.

    For example:

    class Foo {
        private String name;
    
        Foo() {
            this.name = null;
        }
    
        Foo(Foo f) {
            this.name = f.name;
        }
    
        public void setName(String n) {
            this.name = n;
        }
    
        public Foo spawn() {
            return new Foo(this);
        }
    }
    

    This won’t retain references to the old Foo when the new Foo is constructed, so the old Foo can be GC’d:

    Foo f;
    
    f = new Foo(); // The first Foo
    f.setName("Bar");
    
    while (/* some condition */) {
        // Periodically create new ones
        f = f.spawn();
    }
    

    Whereas if Foo looked like this instead:

    class Foo {
        private Foo parent;
    
        Foo() {
            this.parent = null;
        }
    
        Foo(Foo f) {
            this.parent = f;
        }
    
        public Foo spawn() {
            return new Foo(this);
        }
    }
    

    …then each newly spawned Foo would have a reference to the previous Foo and none of them would be available for GC.

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

Sidebar

Related Questions

Similar to this question , after running the following code the browser dialog does
Following the directions at this question , I have some code running to extract
When running the following code it leaves out one row. When I do a
I am running following PHP code to interact with a MS Access database. $odbc_con
I am currently running the following code based on Chapter 12.5 of the Python
I understand that using the === compares type, so running the following code results
When running the following Java code, I get very accurate and consistent results in
The following code compiles correctly under VC++ 8 on XPSP3, but running it causes
let's say I have the following code: for a in object.a_really_huge_function(): print a In
When I am running the following statement: @filtered = map {s/ //g} @outdata; it is

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.