Does using final for variables in Java improve garbage collection?

后端 未结 15 2206
南笙
南笙 2020-11-29 18:01

Today my colleagues and me have a discussion about the usage of the final keyword in Java to improve the garbage collection.

For example, if you write a

15条回答
  •  悲哀的现实
    2020-11-29 18:28

    Some points to clear up:

    • Nulling out reference should not help GC. If it did, it would indicate that your variables are over scoped. One exception is the case of object nepotism.

    • There is no on-stack allocation as of yet in Java.

    • Declaring a variable final means you can't (under normal conditions) assign a new value to that variable. Since final says nothing about scope, it doesn't say anything about it's effect on GC.

提交回复
热议问题