I recently discovered that, under certain circumstances, synchronisation mechanisms offered by java.util.concurrent leak memory. When making a timed wait against, say, CountDownLatch, the latch will allocate a small object that is not garbage collected until the latch itself is garbage collected.
Our application has several CountDownLatches which last the lifetime of the application, and, in certain cirucmstances, the application uses a timed acquire against them. Unfortunately those certain circumstances appeared in production, and persisted for much longer than they do in test. Eventually the application ran out of memory, but not before starting to run very slowly, which increased lock contention, causing truly bizarre behaviour well before the application started to throw OutOfMemoryErrors.
The root of the problem is a bug in AbstractQueuedSynchronizer, and since AbstractQueuedSynchronizer is used to implement other synchronizer mechanisms provided by java.util.concurrent, ReentrantLock, Semaphore, and CountDownLatch, amongst others, all exhibit the bug.
Sun have known about this problem since March 2005, and they have made a decision not to fix it. I can understand this decision – any bug introduced by the fix would have wide-ranging consequences. I don’t agree with it, but I understand it. However, would it have killed Sun to stick a little note about the bug into the Javadoc?
At this point, I am put in mind of The Hitchhiker’s Guide:
PROSSER: But Mr. Dent, the plans have been available in the local planning office for the last nine months!
ARTHUR: Yes, well, as soon as I heard, I went straight round to see them. You hadn’t gone out of your way to call attention to them, had you? I mean, like actually telling anybody.
PROSSER: The plans were on display -
ARTHUR: On display? I had to go down to the cellar to find them!
PROSSER: That’s the display department!
ARTHUR: With a flashlight.
PROSSER: The lights had probably gone out.
ARTHUR: So had the stairs.
PROSSER: But you found the notice, didn’t you?
ARTHUR: Yes, I did. It was “on display” in the bottom of a locked filing cabinet stuck in a disused lavatory with a sign on the door saying, “Beware of the Leopard.”
Hmmm…. looks like Arthur has a good point, but then
Sun Prosser says:
PROSSER: Mr. Dent, have you any idea how much damage that bulldozer would suffer if I just let it roll straight over you?
ARTHUR: How much?
PROSSER: None at all.