0

Background: I have a method that does a bunch of fiddling with copies of a giant Lists and I am occasionally running our of memory. All of this in a single method scope.

Question: Luckily I only need to original giant list at the beginning of the method. In my method, would setting my list to null allow garbage collecting it? Or does the JIT or compiler automatically dereference a variable it sees will no longer be used? I'm thinking this could be a minor optimization to allow earlier garbage collection.

Ztyx
  • 14,100
  • 15
  • 78
  • 114
  • Also duplicate of http://stackoverflow.com/q/850878/13 – C. K. Young Jun 24 '14 at 21:19
  • Clearly the start of the method does something different to the rest of the method. I would extract the portion of the method which needs the giant list and have it fall out of scope in the extracted method. – Peter Lawrey Jun 24 '14 at 21:22
  • The first of those two isn't strictly related to this as even in the olden days, returning from a function caused all the local variables in said function to be garbage collectable. – Powerlord Jun 24 '14 at 21:22

1 Answers1

2

There is no need to nullify a variable. An article on garbage collection (that is well worth a read) can be found here. You are looking specifically for the section labeled "Explicit Nulling"

Mike Elofson
  • 2,017
  • 1
  • 10
  • 16