2

I have a try catch and finally block like this.

Client client = new Client();
try {
   ...
}
catch {
   ...
}
finally {
   client = null;
}

I would like to ask if client = null is necessary to wipe out memory use for client object if exception occurs.

7
  • @JoopEggen - Would a problem occur if you did that anyway (i.e. type client = null; ) ? Commented Mar 19, 2012 at 16:01
  • @delnan Who would've explained it? And it is hard to search if you don't know what the relevant keywords are. Commented Mar 19, 2012 at 16:01
  • @biziclop Whoever is teaching people Java ought to explain it at some point, I'd say. And searching for java memory null variable on StackOverflow has 4 relevant question in the first 50 results (just skimmed over the titles, didn't take very long) - I suppose other combinations fare well too. I alone have seen this questions dozens of times, and I'm sure I didn't see them all. Commented Mar 19, 2012 at 16:05
  • 2
    No, you can set client to null anyway. But in fact that probably is the last occurrence of client so it might even postpone the garbage collection up to that statement. Does not matter, but if before finally there is some huge allocation. Commented Mar 19, 2012 at 16:07
  • @delnan Why do you assume anyone is teaching them Java? Many people are self-taught. SO is full of redundant questions anyway, one more won't make an inch of a difference. Commented Mar 19, 2012 at 16:23

4 Answers 4

4

As soon as the client variable is no longer reachable (ie no longer has a GC root) it is eligible for garbage collection.

In your example it appears as if client will be unreachable as soon as the method exits (whether by an exception being thrown or a 'normal' return), so assigning it to null is unnecessary.

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

Comments

3

Whenever an object has no references to it for any reason then it becomes eligible for garbage collection, including if a variable goes out of scope as a result of the program leaving a function or statement block. In other words, no.

1 Comment

+1 for being the closest to explaining the underlying reasoning (rick.okelly speaks of references to a variable, which seems like confusing nonsense). Throw in some discussion on reachability and it'd be even better :)
2

Client object is subject to scope.If Client object has class scope then it will live until class will load, if client has method scope then it will live until control will be inside method.

there are many scopes besides these two.

so need not to wipe out object. You only need to wipe out when some resource are used like File IO or Database connection.

Comments

1

No, Java's garbage collector takes care of this.

I would tattoo this on my forehead, as this is one of Java's big strengths in relation to C/C++.

4 Comments

"Java's biggest selling point" - That's silly; lots of languages have equally or similarly good garbage collection, have more features, and are in general easier to use. C# and Python come to mind.
The revised version will just piss the C++ developers off (at least the decent ones who know to use smart pointers). Just take that whole "GC is the best thing since sliced bread" part out, it contributes nothing.
@delnan - Garbage collection is a strength , I hereby summon all JavaBeans!
I prefer it too, but your subjective opinion on it has no place in an answer. You could try turning it into a list of objective reasons GC is a strength, but that'd be off topic ;)

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.