If you are attempting to use jmap or another Java memory analysis tool to connect to a running JVM to generate a heap dump, even when running jmap as the same user as that of the running process, and encounter the following error:
Attaching to process ID 2712, please wait... Error attaching to process: sun.jvm.hotspot.debugger.DebuggerException: Can't attach to the process
Following is the (likely) solution to your problem.
It is likely that the ptrace_scope setting for your system is set to a restrictive mode which will not allow another process access to the memory space of the other process. See this link for more details.
The fix is to update the setting in /proc/sys/kernel/yama/ptrace_scope.
First, verify the current setting:
1, indicates a restricted setting. To update it so that another process with the same UID can attach to that processes memory space do the following (as root)
echo 0 | tee /proc/sys/kernel/yama/ptrace_scope
Now, checking that value should return 0
At this point you should now be able to do a heapdump with jmap on your process with a command similar to the following:
jmap -J-d64 -heap 2712