1

I have an Android application where I allocate large arrays, fill them up, and throw them into buffers. After doing this, the arrays are lying around, doing nothing. Would setting them to null theoretically improve the performance of my app, or is the effect negligible/nonexistent?

1
  • 7
    Best to simply make the array variables as local as possible, so that when the method that they were declared in ends, the arrays go out of scope and no longer exist. Commented Jun 8, 2016 at 14:43

1 Answer 1

9

Generally no, setting the array to null will not improve performance or memory use in any way.

The garbage collector will free up the memory when it decides that the object is no longer accessible. Explicitly setting a reference to null is a good signal to the garbage collector that the object is free-able, but if those arrays are already no longer being referenced anyway (because, say, their containing object is no longer referenced) then explicitly setting it to null won't make the garbage collection happen any more quickly.

Sign up to request clarification or add additional context in comments.

1 Comment

Just adding to this already good answer; just because you set the array itself to null doesn't mean the contents of the array just disappear. They are still in memory and still have to be GC'ed just like when they were part of the array. They just aren't referenced by the array any longer, but since the array is longer being referenced it is redundant.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.