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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 13, 20262026-06-13T15:41:23+00:00 2026-06-13T15:41:23+00:00

Say I want to create a new instance foo. I’ll do it like this:

  • 0

Say I want to create a new instance foo.

I’ll do it like this:

SomeClass foo = new SomeClass();

Now lets say the object has reached its end (eg. like a colliding bullet in a game) and should be disposed and reinitialized. To reset the object I could either do this:

foo = new SomeClass();

or this

foo.reset();

NB: The reset() method would just reset all the variables from that instance.

Which is the better way to go (when trying to avoid GC)?
Would the first option create a new pointer?

  • 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-13T15:41:25+00:00Added an answer on June 13, 2026 at 3:41 pm

    The first option creates a new object and leaves the old one to be collected as garbage (unless there are other live references to it).

    One strategy for reducing GC is to maintain an object pool. This would be just a collection of objects that have no current use and are available for reuse. When you are finished with an object, call a method that returns it to the pool. When you need a fresh object, call a method that checks the pool before creating a new object; if the pool is not empty, it would remove an object from the pool and reuse that rather than creating a new object.

    public class SomeClass {
        private static final int MAX_POOL_SIZE = . . .;
        private static final ArrayList<SomeClass> pool = new ArrayList<SomeClass>();
    
        public static SomeClass getInstance() {
            int poolSize = pool.size();
            if (poolSize > 0) {
                return pool.remove(poolSize-1);
            }
            return new SomeClass();
        }
    
        public void recycle() {
            // reset any fields
            if (pool.size() < MAX_POOL_SIZE) {
                pool.add(this);
            }
        }
        . . .
    }
    

    When you need a new SomeClass, call SomeClass.getInstance(). When you are done with an instance, call it’s recycle() method.

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

Sidebar

Related Questions

I am doing something like this: class Class(object): def __init__(self): self.var=#new instance name string#
Lets say I have a class like this: public class Foo { public Foo
I want to create an ouputStreamWriter object say out, for a file named say
I want to create an object, let's say a Pie. class Pie def initialize(name,
I want to create a city filled with virtual creatures. Say like Sim City,
Let's say I create an instance of a class and want to assign some
Let's say I have created two objects from class foo and now want to
Let's say I want to create a new document, and cycle quickly through a
I'm trying to perform an action periodically. I want to create a new instance
Say I want to create a sort of Pre-processor for existing java code, so

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.