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)
– Guilherme Ramos
I was just editing this.
– Maniero
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?
– Guilherme Ramos
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.
– Maniero