Monday, September 1, 2014

Detect file leaks in Java - the quick way

Sometimes you just can't be bothered to install a JVM profiler only to trace the files that have been opened by the piece of code you're debugging. As it turns out, there's a really nifty agent library available for tracing where, when and by which thread your JVM file descriptors have been opened - the File Leak Detector by Kohsuke Kawaguchi.
I've found it to be easiest to use the built-in HTTP server option:
java -javaagent:path/to/file-leak-detector.jar=http=19999 ...
... and then pointing your browser to 
http://[the machine's hostname or IP]:19999
You get a report that shows the file name, the thread responsible for opening the file (accompanied by the stack trace of the open call) and the timestamp of when the file was opened. Just lovely. 

No comments:

Post a Comment