Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Java: Do global variables save memory and/or time?

Tags:

java

android

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?

like image 817
Flynn Avatar asked Jun 03 '12 22:06

Flynn


2 Answers

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.

like image 157
Paul Bellora Avatar answered Oct 07 '22 22:10

Paul Bellora


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.

like image 35
filip-fku Avatar answered Oct 07 '22 22:10

filip-fku