Running jmap getting Could not open socket file

I had to run jmap to take a bunch of my bunch. but jvm returns:

 Unable to open socket file: target process not responding or HotSpot VM not loaded The -F option can be used when the target process is not responding 

So, I used -F :

 ./jmap -F -dump:format=b,file=heap.bin 10330 Attaching to process ID 10331, please wait... Debugger attached successfully. Server compiler detected. JVM version is 24.51-b03 Dumping heap to heap.bin ... 
  • Is using -F suitable for getting a heap dump?
  • I wait 20 minutes and it’s not finished yet. Any ideas why?
+42
java linux jvm jvm-hotspot
01 Oct '14 at 11:20
source share
3 answers

jmap vs. jmap -F as well as jstack vs. jstack -F uses completely different mechanisms for switching to the target JVM.

jmap / jstack

When launched without -F these tools use the Dynamic Attach Mechanism . This works as follows.

  • Before connecting to Java process 1234, jmap creates the jmap file in the working directory of the target process or in /tmp .

  • jmap then sends the SIGQUIT to the target process. When the JVM captures the signal and finds .attach_pid1234 , it starts the AttachListener stream.

  • AttachListener thread creates a UNIX child domain /tmp/.java_pid1234 to listen for commands from external tools.

  • For security reasons, when the connection (from jmap ) is accepted, the JVM checks that the socket credentials are equal to the euid and egid the JVM process. Therefore, jmap will not work if it is run by different users (even root).

  • jmap connects to the socket and sends the dumpheap command.

  • This command is read and executed by the AttachListener JVM thread. All output is sent back to the socket. Because the heap dump is performed directly by the JVM, the operation is very fast. However, the JVM can only do this in safepoints . If it is not possible to reach the security point (for example, the process freezes, does not respond, or the GC lasts), jmap will time out and crash.

Summarize the advantages and disadvantages of Dynamic Attach.

Pros.

  • Heap dumps and other operations are performed jointly by the JVM at maximum speed.
  • You can use any version of jmap or jstack to connect to any other version of the JVM.

Cons.

  • The tool must be run by the same user ( euid / egid ) as the target JVM.
  • It can only be used for live and healthy JVMs.
  • It does not work if the target JVM is started using -XX:+DisableAttachMechanism .



jmap -F / jstack -F

When launched with -F tools switch to a special mode in which the HotSpot Serviceability Agent is used . In this mode, the target process is frozen; tools read their memory through OS debugging tools, namely ptrace on Linux.

  • jmap -F calls PTRACE_ATTACH on the target JVM. The target process unconditionally pauses in response to a SIGSTOP signal.

  • The tool reads JVM memory using PTRACE_PEEKDATA . ptrace can only read one word at a time, so too many calls are required to read a large heap of the target process. It is very, very slow.

  • The tool restores the internal structures of the JVM based on knowledge of a specific version of the JVM. Since different versions of the JVM have different memory formats, the -F mode only works if the jmap comes from the same JDK as the target Java process.

  • The tool creates the heap heap itself and then resumes the target process.

Pros.

  • Collaboration with the target JVM is not required. It can be used even when hanging.
  • ptrace works when there are enough OS-level privileges. For example. root can reset the processes of all other users.

Cons.

  • Very slow for large heaps.
  • The tool and the target process must be from the same version of the JDK.
  • It cannot be guaranteed that safepoint will not be used if the tool is connected in forced mode. Although jmap tries to handle all special cases, it can sometimes happen that the target JVM is not in a consistent state.

Note

There is a faster way to dump heaps in forced mode. First create a coredump with gcore , then run jmap on the generated kernel file. See related question .

+85
Mar 12 '16 at 20:43
source share

I just discovered that jmap (and presumably jvisualvm when using it to create a heap dump) provides that the user running jmap must be the same user that starts the process trying to reset.

in my jvm case, I want the dump heap to be started by linux user "jboss". so when sudo jmap -dump:file.bin <pid> reported "Could not open socket:", I was able to capture my heaps with:

 sudo -u jboss jmap -dump:file.bin <pid> 
+54
Dec 11
source share

Like ben_wing , you can work with:

 sudo -u jboss-as jmap -dump:file.bin <pid> 

(in my case, the user is jboss-as , but yours may be jboss or some other.)

But that was not enough, because he asked me for a password ( [sudo] password for ec2-user: , although I could run sudo without asking for a password for other commands.

I found a solution here , and I just needed to add another sudo :

 sudo sudo -u jboss-as jmap -dump:file.bin <pid> 

It works with other teams such as jcmd and jinfo .

0
May 31 '17 at 20:40
source share



All Articles