Lazy Diary @ Hatena Blog

PowerShell / Java / miscellaneous things about software development, Tips & Gochas. CC BY-SA 4.0/Apache License 2.0

Output of jcmd GC.heap_dump is placed in the root directory of the application

Problem

When you get a heap dump of a Java application with jcmd like:

PS C:\home\satob> jcmd 18228 GC.heap_dump ./demo.hprof
18228:
Heap dump file created

You will not be able to find the heap dump on the current directory like:

PS C:\home\satob> Get-ChildItem -Name demo.hprof
Get-ChildItem : パス 'C:\home\satob\demo.hprof' が存在しないため検出できません。
発生場所 行:1 文字:1
+ Get-ChildItem -Name demo.hprof
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    + CategoryInfo          : ObjectNotFound: (C:\home\satob\demo.hprof:String) [Get-ChildItem], ItemNotFoundException
    + FullyQualifiedErrorId : PathNotFound,Microsoft.PowerShell.Commands.GetChildItemCommand

Cause

When you specify a path of a heap dump file to jcmd, the base directory of the path will be the root directory of the target application (not the current directory of the command). In this example, demo.hprof is placed on the root directory of the Eclipse project of process 18228.

Solution

You have to specify the absolute path like:

PS C:\home\satob> jcmd 18228 GC.heap_dump C:/home/satob/demo.hprof
18228:
Heap dump file created
PS C:\home\satob> Get-ChildItem -Name demo.hprof
demo.hprof

or you can use Get-Location in PowerShell, or pwd in sh like:

PS C:\home\satob> jcmd 18228 GC.heap_dump "$(Get-Location)/demo.hprof"
18228:
Heap dump file created
PS C:\home\satob> Get-ChildItem -Name "$(Get-Location)/demo.hprof"
demo.hprof
PS C:\home\satob>