I think this is good for garbage collection
Nope, it just makes the code messier.
What's going to hold a reference to the instance of MyClass
? If you genuinely need the instance of MyClass
to stay alive, but not the BigObject
, then - and only then - should you bother setting bo
to null. But at the same time, you should also consider your design. Usually, all of the state of a class is important to it for its whole lifetime. There are exceptions to that rule, but they're relatively rare.
When the instance of MyClass
is eligible for garbage collection, the value of its bo
variable will be irrelevant to whether the instance of BigObject
is eligible for garbage collection, so it doesn't matter whether or not it's been set to null... but it does make a big difference in terms of the amount of code you end up not having to write trying to manually clean up after yourself.
You should clean up resources such as streams, native handles etc - but generally you can let the garbage collector handle memory.