• Post Reply Bookmark Topic Watch Topic
  • New Topic

scope?: setting method-local variables to null at the end of the method?  RSS feed

 
Riffle Chris
Greenhorn
Posts: 27
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Maybe a silly question.

Is it not true that variables(primitives and references to objects) that are declared inside of a method, only consume memory during the "lifetime of that method", with the understanding that the garbage collectors will reclaim the memory within a reasonable amount of time without the programmer needing to explicitly suggest to run the gc?

I joined a project that has the following type of code everywhere:





Our project members claim that the Sun JVM garbage collectors don't behave as documented, based on our project members' profiling-research when debugging memory leaks in our the project. So this type of code is everywhere in an attempt to aid the garbage collectors in knowing that these method-local references to objects are null. Is it not true that the Sun java compilers already set these method-local object references to null when the source code is compiled!? Even if it wasn't done at compile time,... Would explicitly setting the references to null have any advantage in helping the garbage collectors to consume those objects sooner?

Thanks!!


 
Henry Wong
author
Sheriff
Posts: 23295
125
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Riffle Chris wrote:
Our project members claim that the Sun JVM garbage collectors don't behave as documented, based on our project members' profiling-research when debugging memory leaks in our the project. So this type of code is everywhere in an attempt to aid the garbage collectors in knowing that these method-local references to objects are null.


That's pretty hubris there isn't it? Your project members claim to know of a defect, that hasn't been fixed for over 15 years? That hasn't been found by anyone else?

Henry
 
Henry Wong
author
Sheriff
Posts: 23295
125
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Riffle Chris wrote:
Is it not true that the Sun java compilers already set these method-local object references to null when the source code is compiled!? Even if it wasn't done at compile time,... Would explicitly setting the references to null have any advantage in helping the garbage collectors to consume those objects sooner?



Actually, I don't think so -- meaning I don't think the compiler automatically null out local variables upon return. When the method returns, the stack should be popped, and hence, the local variables should go out of scope. This means that they should no longer be reachable, and doesn't need to be set to null.

Henry
 
Riffle Chris
Greenhorn
Posts: 27
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Thanks Henry
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!