Picture the server behind a bank's mobile app on a busy Thursday evening. Every second it creates thousands of small objects: a Transaction here, a Receipt there, a String for every line of a statement. Nowhere in the Java source does anybody call free, and yet the server runs for months without choking. Who clears away the things nobody needs any more?
Now picture one of those transactions going wrong halfway. The network drops in the middle of a transfer, a customer types an account number that doesn't exist, a file the program expects is not there. The program can crash with an ugly message, or it can notice, tidy up, tell the customer politely and carry on. And all the while two tellers are working on the same account at the same moment, each sure that the balance she just read is still true.
This chapter is about those three things, in that order: how Java cleans up after you, how it lets you deal with trouble, and how it lets two things happen at once. The last chapter ended with objects living on the heap, born by new, shared through references, compared with equals. Here we follow them to the end of their lives.
By the end you'll be able to say exactly when an object becomes garbage, why System.gc() is a request and not a command, and why nobody writes finalize() any more. You'll read a stack trace, draw the family tree of exceptions, write try, several catch blocks and finally in the right order, and say when finally does not run, with proof from a real run. You'll tell throw from throws, write an exception class of your own, close resources with try-with-resources and explain what a suppressed exception is. And you'll start a thread in both ways, say what start() does that run() does not, draw the life cycle, make a race condition happen on your own screen and fix it with synchronized.