Assigning “null” to objects in every application after their use

前端 未结 14 779
感动是毒
感动是毒 2021-01-18 12:05
  • Do you always assign null to an object after its scope has been reached?

  • Or do you rely on the JVM for garbage collection?

14条回答
  •  醉酒成梦
    2021-01-18 12:48

    I declare almost all of my variables as "final". I also make my methods small and declare most variables local to methods.

    Since they are final I cannot assign them null after use... but that is fine since the methods are small the objects are eligible for garbage collection once they return. Since most of the variables are local there is less chance of accidentally holding onto a reference for longer than needed (memory leak).

提交回复
热议问题