2

Now if I want to dump a big string or char[] from heap dump.
I firstly get the address from heap analyzer and then dump it to disk using

chars = heap.findObject("0x1111111")
var writer = new java.io.FileWriter("D:/temp/oql/chars.txt");
for (var i = 0; i < chars.length; i++) {
    writer.write(chars[i]);
}
writer.close()

But it's annoying.
I need to use two tool to do one thing and waste both time and memory.

Instead of object address, What the visualVM provide is index:
enter image description here

So If I don't want to open Heap analyzer I will use:

filter(heap.objects('char[]'),
 function(it, index) {
  if (index == 710394) {
      var writer = new java.io.FileWriter("D:/temp/oql/chars.txt");
      for (var i = 0; i < it.length; i++) {
          writer.write(it[i]);
      }
      writer.close();
      return true;
  }
  return false;
})

But it's slow when index is big.

I check the OQL to see if I can use index,but not found.
So Can I get the address directly by VisualVM(like index now),or any other way to simply dump to disk.

fairjm
  • 1,115
  • 12
  • 26

1 Answers1

0

Check my answer here: OQL syntax to refer to an object?

After you identified an object (via objectid(obj) or my objectnum("byte[]#123")) you can save it to file with FileOutputStream.write(byte[]).

If you extract char[] or java.lang.String you can use:

FileOutputStream.write(obj.toString().getBytes())

For byte array you can write each element individually:

var fos = new java.io.FileOutputStream("c:/home/tmp/arr.txt");
for (i = 0; i < o.length; i++) { fos.write(o[i]); }
fos.close();
gavenkoa
  • 45,285
  • 19
  • 251
  • 303