Wednesday, November 19, 2008

Ah.. Java

I've been using Java for a few years now. And one thing that i've come across is.. memory leaks.

"Memory leaks?" you say. Yup. "In Java? That's unpossible!" you exclaim.

No really. It's true. The garbage collector makes a fine effort to clean up most unused objects, but every so often you'll get an object being stored that can't be collected because it's still referenced somewhere. Usually these are caused by putting a object into a static Map, or Array somewhere. Mostly it's the programmers fault, though the JRE/JDK itself may have some. This one for example is caused when you use a JPopup. A reference to the JComponent it is displayed on is kept internally by the standard Java libraries. If you only ever have one JPopup in your entire program, say in a startup page, and the rest of the program is all console based, then the reference to your window will be kept until the program exits. If your program is a daemon designed to run on a server, that could be a very very long time.

So.. how to find them? Well, luckily it's only a few step process.

Finding memory leaks using the Standard JDK 5.0 tools.


1. Run the program to check for leaks.
2. Try to do something that will expose the memory leak
3. Find the PID of the the java program by running jps.exe
4. Dump the memory of the program with this command
jmap -dump:format=b,file=heap.bin
5. Run jhat, it will start a webserver on the computer it's run on at port 7000
jhat -J-mx512m heap.bin
6. Load http://localhost:7000/ for general data
7. Load http://localhost:7000/histo/count and find the class you think is leaking, click on the link
8. Scroll down till you find the section called "Instances" and click "Include subclasses"
9. Find the instance that leaked and click it
10. Scroll down till you find the link "Exclude weak refs" and click it
11. A list of references will appear. Ignore any that contain a class called sun.awt.AppContext, or AppClassLoader as these classes are Java internal classes
12. The rest of the page will show which classes are still holding references to the object. Find out why it's holding it. Maybe you forgot to do a .remove() when the item was no longer needed.

And for my first trick!

A few phrases from a Markov text generator i wrote which can be surprisingly lucid at times. It uses text from the Gutenberg project, as well as a couple of Star trek scripts(Encounter at Farpoint)

"Seeming impossibly large, yet in some ways as delicate as a beardless youth, with hands of one"

"The Monkey King spread his wings and flowing drapery, which is evidently meant to reflect strength and power."

"From the time upon which he sowed as he feared a spark might get into the temple stood a vessel bound from Crete to the quick; and he was overcome with sorrow and indignation of his cheerless and unproductive portion of the day"

"Father," she said, "Follow me and end my wicked deeds"

"Priest of Unreason, and of Styx, the presiding nymph of Larissa, served as the Wicked Witch"

"Look out--here I go!" With skill will set the human intelligence. 42"

"Who's doing this? PICARD Get off my head, at last, he walked close to an energy beam, then back to Kansas"

"Noble Knights of Hunger."

"Men Erginus was so remote as {139} to be immolated on his hitherto luxurious couch, this also was painted green"

"Obeying his belly so that you can put your brains in your eagerness to please us."

"YOU AGREE THAT THE FOUNDATION, THE TRADEMARK OWNER, AND ANY DISTRIBUTOR UNDER THIS AGREEMENT WILL NOT BE LIABLE TO YOU FOR ACTUAL, DIRECT, INDIRECT, CONSEQUENTIAL, PUNITIVE OR INCIDENTAL DAMAGES EVEN IF YOU GIVE NOTICE OF THE TURBOLIFT ENTRANCE"

"Besides, you have overlooked something--the mortality of the cow suddenly gave a distinct divinity, holding indisputable dominion over the entire bolt."

"Ready to beam up to Heaven's gate."