6

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?

2
  • Do you want to reuse the instances? Commented Jun 3, 2012 at 22:46
  • 1
    Any time/space savings are outstripped by a 10x cost to sanity. Commented Jun 3, 2012 at 23:38

4 Answers 4

4

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.

Sign up to request clarification or add additional context in comments.

Comments

1

I don't think the decision should be made purely on efficiency - instead on which composition better represents the domain you are trying to model. The question to ask is "which is the correct structure for my class that uses View?".

Using an instance variable is not a global - it's contained within the context of the object declaring it. Globals are not supposed to exist in OO, though public static variables come pretty close.

The first thing to decide is where your "View" instance logically belongs relative to the intent of the class/method using it. If the class using it is a factory of some sort, and "myMethod" is a factory method, then yes return a new instance.

If "View" is a logical field of your class, and somehow helps in capturing and enhancing its state and behaviour, then there is no reason to create a new one every time. Simply maintain its state, and work with the existing object.

From your description it seems like you are trying to maintain and update the state of a view of some sort. It makes sense to change it to the required state and re-display it, rather than creating a new object every time. As it seems like functionally both approaches work in your scenario, I would take the second alternative and avoid creating unnecessary objects.

Comments

0

The global variable is more efficient.

Creating new objects have more of a memory footprint, because each time the view is created and replaces the old view, the old one has to get garbage collected. Reusing the same view with a global variable saves the system the task of creating a new object and collecting the old one.

Edit: The global variable is only a reference to the object, not an object itself.

Comments

0

Well, when you have every constructor create a new instance, essentially you are performing work again that isn't absolutely necessary. If I were in your shoes, I would have the global variable option, then then clear the view, and redraw on the same object if at all possible. This prevents the possibility of repeated memory deallocation and allocation with the other method, so unless you think that clearing is particularly expensive-enough to make it worth it to continually allocate new views, then wipe it.

Another incentive to reuse the same object is that java virtual machine implementations differ on their use of garbage collection in that once a variable falls out of scope it can not be accessed-when the memory that corresponds to the JVM is freed is not always the same. You could find that with the alternative, you have instances where many views are sequentially allocated, but not all deallocated until the process sits idle for a while, effectively making the app a memory hog exactly like you DON'T want.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.