Advantage of nulling a variable in Android

2

Assigning the value of null to a variable in Android can improve application performance?

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

Problem:

  • In a class a local variable will be cleaned when? If you give null to it, can it help with the issue of application performance?

  • In a global variable, if you give null to it, knowing that it will be no longer useful, will it help with application performance?

  • In class variables, give null whenever it's no longer used, can it improve application performance?

Searching, I found this about variables and static methods:

  

Use static methods

     

You have created a method and it is totally independent of the class where it is, what are you waiting for to make your method static?

     

Making a static method you are saying to the compiler, or interpreter, that the method's content only needs to be in memory during its execution, not while the class is instantiated, that is, you release the memory of the variables the method . In addition to this marvel, you do not need an instance to run a static method, you invoke the direct method of your class, freeing your memory from having to store attributes of the class you will not use.

     

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

  • On Android, following the same practice is synonymous with performance? By doing this, would not I be "saving" unused memory?

  • Source: link

asked by anonymous 27.07.2017 / 14:05

2 answers

2

Then

  

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

Do you mean in a method? No need unless he has serious problems.

  

In a global variable, if you get null in it, knowing that it will be no longer useful, will it help with application performance?

Yes, help, but if possible, change this pattern so you do not have to do this

  

In class variables, give null whenever it's no longer in use, can it improve application performance?

Maybe, but it should also change the pattern.

  

On android, is the same practice synonymous with performance? In doing so, would not I be "saving" unused memory?

Being static has nothing to do with performance, but not directly. It may even have a tiny gain in call, but it's not relevant.

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

Explanations

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

In fact undoing a variable can cause a reference to cease to exist and an object that is 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 overriding the variable is already doing its part.

Bypassing a local variable in a method does not make sense because all of them are automatically undone at the end of the method. It would make sense if the method hangs in a thread for some reason and for a bad reason holds back references it should not. This is actually a programming error, fix the bug instead of doing a trick trying to get rid of the reference. It does not matter whether the method is static or not, they themselves are not problematic.

In a class a member can hold a reference when in fact it should not. Actually in instances it is less common because at any given moment the instance will die and all its variables are automatically overridden at this point. What can occur is an object surviving more than expected and this is probably a programming error as well and should be solved.

But if the object lasts too long then it should rethink the structure of the object. Maybe it has a variable holding a reference to another object that should not be there.

This can occur with static members of a class that have all the durability of the application. If a static variable should have its value aborted, it should rethink whether the variable should be static or even exist.

Static members may be global status problem , but they can be useful if you know what you are making. Almost always that you need to cancel it, you do not know what you're doing. The fact that the static variable stays there all the time is not a problem because it has a small value every time, or is a value type, or is a reference. Her problem may be competition or holding a reference for longer than it should. But there are cases for its use. Whoever understands the workings of things and does not follow "good practices" blindly knows this.

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

There are even a few rare cases where voiding the variable can be useful, but it's rare, it almost always has a better way of doing this and should fix the right problem.

Even if you override 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 do not know when it will run. Only time is not deterministic and the application may terminate before a collection occurs. 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 no one knows whether it will occur or not.

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

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

    
27.07.2017 / 14:20
1

In a nutshell: no, it will not solve.

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

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

In addition to the system itself will clean up the features of the application when it needs memory (being in the background).

One point about static methods is that they are not the best, so variables are not even talked about. To the best of my knowledge, static variables are allocated in memory and remain available until explicitly removed, either by operating system or program execution.

The best way to optimize an application on android is to understand the lifecycle, and how it stores the information between intent transfers, and state saving. In addition to decreasing costly operations for core threads and use of background tasks / services for more time-consuming operations.

    
27.07.2017 / 14:20