How to fix pmd violation “NullAssignment”?

前端 未结 3 1842
囚心锁ツ
囚心锁ツ 2021-01-23 19:42

PMD report NullAssignment of the following code, what is the best practice to fix it?

Assigning an Object to null is a code smell. Consider refactoring.

3条回答
  •  终归单人心
    2021-01-23 19:59

    This code is written in the false believe that a reference that is set to null is garbage collected faster.

    Therefore the message from PMD is that this false believe was coded.

    This is a wrong assumption as the garbage collector runs when the memory is exhausted and it collects all objects that have no reference left on them.

    Even calling System.gc() will not cause the garbage collector to run. The call is merely a hint to the garbage collector but when the garbage collector detemines that enough free memory is avaible it will not run.

提交回复
热议问题