Sonarlint x Java GC, cancel or not object at the end of the execution?

Asked

Viewed 75 times

3

I am passing Sonarlint in a somewhat old application and fixing several "problems", but I came across a code snippet similar to:

public void fazerAlgo(final String param) {
    MeuObjeto m = new MeuObjeto();
    m.setVar(param.toLowerCase()); 

    myBusiness.atualizarAlgumaCoisa(m);

    m = null; // <- Para liberar memória e ajudar o GC
}

And Sonarlint charged the error below for the line m = null:

Remove this useless assignment to local variable
(Remova essa atribuição inútil à variável local)

The sonar point of view is that this object "m" is not used after the assignment, so the set becomes useless, it makes sense and at other code points where it happened I removed the assignment.

However, at the time of Java 5, Java 6 (that is, today in many companies), it was common for more advanced developers to request things like "put final in everything", "always start a array with a size" and "cancels objects before exiting the method", the latter being to save memory and do the Garbage Collector Java clean this object faster.

Which of the two is more right? Should I keep the m = null, because where this code is happening the objects are very large, or I should remove this assignment because it does not influence anything in the performance of the application and Sonarlint is always right?

1 answer

5


This type of utility does not understand the context of the application. If you use this and you don’t understand what you’re doing, you’ll start screwing up the application as much as you will.

In this case he is justified, who made this null doesn’t understand how the code works. It is not necessary to cancel anything in the code, except in static variable, which at some point no longer needs to have a stored content. But if this is the case, the error is probably that it made this variable static. The statement of the question is a non-sense. No matter how big he is.

If you consider these developers more advanced, I regret having had bad influences. In fact the vast majority of programmers are bad and do not understand what they are doing, even worse when these people influence others. So always look for reliable sources, verifiable by multiple qualified people.

The collection will occur whenever necessary in all objects that do not have reference to it. What you are doing there is this:

public void fazerAlgo(final String param) {
    int x = 1;
    System.out.println(x);
    x = 0;
}

I put in the Github for future reference.

Do you think this is necessary? It has the same effect.

The best part of this question is that I can tell you to give up good practice. Most people don’t understand it and use it as cake recipes. If you don’t understand why they’re telling you to do it in depth, don’t use it! And I say more, most of these good practices are simply wrong because they were created by those who do not understand what they are doing. And even in cases where they make sense, but only in a specific context. Their context may be different. I have been giving lectures showing how the lack of context has caused atrocities in organizations.

Browser other questions tagged

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