David NĂ©grier CTO

When facing problems regarding memory consumption of a Java application, the JDK contains a number of useful tools to help you understand what is going on. You can get a list of the tools on this page.
One of these tools is "jmap". Jmap will let you now (among other things) know what objects are using your precious memory.

Cool!
...

except that when you try to use jmap on Ubuntu 11.04 with a Sun JVM 6, you will get this error message:

Attaching to process ID 12450, please wait...
Error attaching to process: sun.jvm.hotspot.debugger.DebuggerException: Can't attach to the process

Damn!

The error is non obvious, but comes from a configuration of ptrace in Ubuntu.
To solve these issues, you just need to use this line of code before using jmap:

echo 0 | sudo tee /proc/sys/kernel/yama/ptrace_scope

Then you can use jmap normally to get your memory dump:

jmap -histo \[PID\]

You can learn more about this problem in this bug report.

About the author

David is CTO and co-founder of TheCodingMachine. He is the co-editor of PSR-11, the standard that provides interoperability between dependency injection containers. David is the lead developer of Packanalyst, a website that references all PHP classes/interfaces ever stored on Packagist. He is also the lead developper of Mouf, the only graphical dependency injection framework and currently working on another PSR, regarding standardizing service providers (more containers goodness!).