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

前端 未结 14 776
感动是毒
感动是毒 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:26

    There was a class of memory leak bugs that happened regardless of whether I set the reference to null - if the library I was using was written in a language like C without memory management, then simply setting the object to null would not necessarily free the memory. We had to call the object's close() method to release the memory (which, of course, we couldn't do after setting it to null.)

    It thus seems to me that the de facto method of memory management in java is to rely on the garbage collector unless the object/library you're using has a close() method (or something similar.)

提交回复
热议问题