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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 16, 20262026-06-16T04:06:33+00:00 2026-06-16T04:06:33+00:00

I am currently trying to avoid GC_CONCURRENT calls, so i’m running through my main

  • 0

I am currently trying to avoid GC_CONCURRENT calls, so i’m running through my main loop.
I’ve noticed that i often create a complex object to do calculations.

So my question is would declaring that object as a field of the class opposed to declaring it in the methods that use it help performance?

Or because my english has probably hurt you brain, here’s the code example as field

class myclass{
    private MyObject myObject;
    ...
    public void myLoopedMethod(...){
        myObject = new MyObject(...);
        myObject.dostuff;
    }

Example in method

class myclass{
    ...
    public void myLoopedMethod(...){
        MyObject myObject = new MyObject(...);
        myObject.dostuff;
    }

The right scope would be the method, but my doubt is that by making it a field, the memory is always freed and allocated in the same spot. Is this true and does this help avoiding GC calls?

Also, i should probably do something like this, but I’m interested if the above logic makes sense.

class myclass{
    private MyObject myObject;
    ...
    public void myMethod(...){
        myObject.setNewValues(...);
        myObject.dostuff;
    }
}
  • 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-16T04:06:34+00:00Added an answer on June 16, 2026 at 4:06 am

    but my doubt is that by making it a field, the memory is always freed
    and allocated in the same spot. Is this true and does this help
    avoiding GC calls?

    There is no guarantee that memory allocated in the same spot. It is implementation detail.

    In your example case, if instance variable, all objects referenced by this instance variable will be eligible for GC except last object which still has a reference from the instance variable (last one will become eligible for GC when it has no reachable references).

    In case of defining inside method, all objects referenced by this reference will become eligible for GC as soon as loop done.

    So, better go with defining inside method unless you need reference to the object identified in loop.

    Coming to question avoiding GC calls, I think both approaches will have almost same amount of GC activity. Unless you have real issue with memory I would suggest don’t worry about memory allocation and GC, VMs are intelligent enough to take care that stuff.

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

Sidebar

Related Questions

I'm currently trying create a service that will return results of a OLAP cube
Some sample, straight forward gtk2 within-main-loop I'm currently trying to add code at key-press-event
I'm currently trying to write some Javascript to loop through the elements in a
Currently trying at add ajax to a site, after much reading I discovered that
I am currently trying to write some code that will accept some FTP details,
I'm currently trying to view an image being loaded through a Res://imagename at the
I am currently trying to make a test site that allows users to record
I have a possibly long running program that currently has 4 processes, but could
I am currently trying to build a small system that read's in a bunch
I'm currently trying to manually create a simple daemon process, I don't want to

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.