Advantage in giving null in a variable on Android

Asked

Viewed 355 times

2

Assign the value of null in a variable on Android can improve application performance?

As far as I know, in Java we have the Garbage Collector, and in Android, the activity cycle of Activities and Fragments.

Problem:

  • In a class a local variable will be cleared when? If given null in it, can help in the issue of application performance?

  • In a global variable, if applicable null in it, knowing that it will no longer be useful, will help in the application’s performance?

  • In class variables, give null whenever you’re no longer using, you can improve the app’s performance?

Searching, I found this about variables and static methods:

Use static methods

You’ve created a method and it’s totally independent of the class it’s in, which you’re hoping to make your method static?

By making a static method you are telling the compiler, or interpreter, that the content of the method only needs to be in memory during its execution, not while the class is instantiated, that is, you release the memory of the local variables after the execution of the method. In addition to this wonder, you don’t need an instance to run a static method, you invoke the method directly from your class, freeing your memory from having to store class attributes that you won’t use.

Use sparingly, do not switch all your methods to static just to gain performance, otherwise the spell can turn against the wizard. Nonstatic methods have the privilege of sharing attributes, avoiding storing more than once the same information."

2 answers

2


So

In a class, a local variable will be cleared when?

You mean in a method? You don’t have to unless he’s got some serious problems.

In a global variable, if you give null to it, knowing that it will no longer be useful, it will help in the application’s performance?

Yes, it helps, but if possible, change this pattern so you don’t have to do it.

In class variables, giving null whenever no longer use, can improve application performance?

Maybe, but you should also change the pattern.

On Android, following the same practice is also synonymous with performance? Doing this, I wouldn’t be "saving" memory without using?

Being static has nothing to do with performance, at least not directly. It may even have a tiny gain on the call, but it’s not relevant.

Static methods do not store memory without using. Static variables can help do this eventually.

Explanations

Overall the advantage is zero, at least if the code is well done.

In fact annulling a variable can cause a reference to cease to exist and an object that is year heap can be collected by GC at any time by releasing memory.

This is not guaranteed because the object may have other references that still hold it alive in memory. But nullifying the variable is already doing its part.

Nullifying a local variable in a method makes no sense, because all of them are automatically nullified at the end of the method. It would make sense if the method hangs on one thread for some reason and some bad reason you keep holding references you shouldn’t. This is actually programming error, fix the error instead of doing gambiarra trying to pound the reference. No matter if the method is static or not, they themselves are not problematic.

In a class a member can hold a reference when he really shouldn’t. In fact in instances it is less common because at some point the instance will die and all its variables are automatically nullified at that time. What can occur is an object survive longer than expected and this is probably a programming error as well and should be fixed.

But if the object lasts very long, then you should rethink the structure of the object. Maybe he has a variable holding a reference to another object that shouldn’t be there.

This can occur with static members of a class that has durability of the entire application. If a static variable should have its value undone, it should rethink whether the variable should be static or until it exists.

Static members may be problem because of the global state, but they can be useful if you know what you’re doing. Almost whenever you need to override it you don’t know what you’re doing. The fact that the static variable stays there all the time is not a problem because it always has a small value, either it is a type by value or it is a reference. Its problem may be the competition or the fact that it holds a reference for longer than it should. But there are cases for its use. Those who understand how things work and do not follow "good practices" blindly know this. Always study before using something.

Note that canceling a variable is different from changing its value. When we put another reference to a variable the object that before was pointed by this variable loses this note and possibly can be collected.

There are even some rare cases where nullifying the variable may be useful, but it is rare, it almost always has better way to do this and should fix the right problem.

Even if you cancel a variable and the referenced object has no other references, there is no guarantee that the GC will collect the object at some point. Not that the GC is unpredictable, you just don’t know when it’ll run. Only time is not deterministic and the application can end before a collection takes place. But if a collection is fired and the object has no references it will be collected for sure, it is not a crazy thing that nobody knows whether it will occur or not.

You said good practice, I twist my nose. In general the person is reproducing what he read without understanding it or is selling an idea without wanting to instruct the reader.

The quoted text has several errors, some serious and is the opposite of reality, the bold quote in the question for example, would not trust it. Occurs in most internet content.

  • The same goes for static methods? (running a little)

  • I was just editing this.

  • I thought what you wrote was very good and clear. It just wasn’t clear to me the static methods part. So static methods are not so good to write, right? But it’s not about memory. Variables are cleaned as well, so is a static method used? Because in a way, it is more "efficient" not to instantiate a class and call the static method, than to instantiate it and call the method, no?

  • 1

    They are, as long as you know what you’re doing. I thought it was clear when I said I always prefer them. Only that in many cases they do not make sense, methods that need to operate in the instance cannot be static. All methods clear variables when they are finished, this is all in the answer. Clearing the variable is not cleaning the object automatically, this is GC work. It’s certainly more efficient not to instantiate a class, but we can’t always do that.

1

In a nutshell: no, it won’t solve.

In all the experience time I have, the JVM (Dalvik/Art), and the Garbage android’s Ollector are totally independent and you do not control its execution. Changing into kids, they are "crazy dog" (independent and unpredictable)

Passing null to a variable, regardless of whether it is class, global or local, will indicate that it should be cleaned by Garbage Collectork, but does not indicate that it will occur at that exact moment.

In addition to that the system itself will wipe the application resources when it needs memory (being in the background).

One point about static methods is that they are not the best, variable so no talking. To my knowledge, static variables are allocated in memory and are available there until they are explicitly removed, either by the operating system or by running the program.

The best way to optimize an application on android, is to understand the life cycle, and how it stores the information between transfers, and state saving. In addition to reducing costly operations for core threads and using tasks/services in the background for longer operations.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.