3

I am willing to write the value of a large StringBuilder with index 827 to disk. I am trying to do that using the following OQL:

map(heap.objects('java.lang.StringBuilder'),
 function(it, index) {
  if (index == 827.0) {
      var writer = new java.io.FileWriter("/Users/username/output/sb_0x" + it.id.toString(16) + ".txt");
      var chars = it.value;
      for (var i = 0; i < chars.length; i++) {
          writer.write(chars[i]);
      }
      writer.close();
  }
  return index;
})

However, nothing gets written. I now that the builder exists, since I have inspected it:

All StringBuilder objects

It seems that the result gets truncated after the builder with index 99 (i.e. it works for 99, but doesn't work for 100):

Truncated after 100

Any suggestions how can I get the value of the StringBuilder with id 827?

trincot
  • 317,000
  • 35
  • 244
  • 286

1 Answers1

2

You can use the following query:

filter(heap.objects('java.lang.StringBuilder'),
 function(it, index) {
  if (index == 827.0) {
      var writer = new java.io.FileWriter("/Users/username/output/sb_0x" + it.id.toString(16) + ".txt");
      var chars = it.value;
      for (var i = 0; i < chars.length; i++) {
          writer.write(chars[i]);
      }
      writer.close();
      return true;
  }
  return false;
})
Tomas Hurka
  • 6,723
  • 29
  • 38
  • 1
    It would be even more helpful if you explained what the difference is between the original code, and why it makes a difference. – Kayaman Jan 23 '18 at 16:09