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

  • Home
  • SEARCH
  • 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 8035709
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 5, 20262026-06-05T02:23:09+00:00 2026-06-05T02:23:09+00:00

I am working on an Android app and a method I am writing might

  • 0

I am working on an Android app and a method I am writing might get called a bunch of times. In this method I am making updates to the user interface. Memory use and performance are important to me. The way I see it, I have 2 options for making the UI changes.

The first is to makes new objects every time. That is to say something like:

public void myMethod(){
new View().makeVisible();
}

The second is to declare the object as a variable globally and reference it in the method. This might look like:

View myView = new View();

public void myMethod(){
myView.makeVisible();
}

Obviously the if this method only called a few times any difference is going to be small. However if I am potentially calling this many times, and there are many variables being call/or created this way, does the second way increase performance?

  • 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-05T02:23:10+00:00Added an answer on June 5, 2026 at 2:23 am

    As the other answers have indicated, reusing the same object instead of repeatedly instantiating a new one for every method call will reduce your memory footprint and improve performance with respect to garbage collection.

    But I would actually think about maintainability first. Is reusing the same object going to make your code much more complicated (and potentially introduce bugs)? It’s good to keep efficiency in mind as you program, but avoid premature optimization that complicates your project and slows development.

    If performance and memory usage are a concern, then yes it will benefit you to reuse the same View:

    final View myView = new View(); //made final because it shouldn't be reassigned
    

    If you really want to get resource minded, you could even lazy-load the object – that is, create it only as soon as it’s needed. However I would recommend using Guava’s Suppliers.memoize(Supplier) to take care of this instead of hand-coding that behavior:

    final Supplier<View> myViewSupplier = Suppliers.memoize(new Supplier<View>() {
        @Override
        public View get() {
            return new View();
        }
    });
    
    ...
    
    public void myMethod() {
        View myView = myViewSupplier.get(); //lazy-loads when first called
        myView.makeVisible();
    }
    

    That’s probably extreme for this particular situation though.

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

Sidebar

Related Questions

I am currently working on an Android app and I am trying to get
i'm working on a android app that will display Strings to the user, and
I am working on an Android app that has multiple screens the user will
I have been trying to get this android service working but I can not
I'm working an Android AudioRecord app. If I do not call the release() method
Working on an Android app with FB 4.6, I would like to have the
I am working on an Android app that utilizes the Google Maps API MapView,
I'm working on an Android app in which I would like to use multi-touch.
I am working on an android app that has a database in which one
I'm working on an Android app native written in java and I'm getting a

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.