Skip to content

Instantly share code, notes, and snippets.

@turbanoff
Created August 10, 2018 15:33
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save turbanoff/19982d1bc84a5cf7296b426f8f5d32fd to your computer and use it in GitHub Desktop.
Save turbanoff/19982d1bc84a5cf7296b426f8f5d32fd to your computer and use it in GitHub Desktop.
package com.devexperts;
import org.netbeans.lib.profiler.heap.Heap;
import org.netbeans.lib.profiler.heap.HeapFactory;
import org.netbeans.lib.profiler.heap.Instance;
import org.netbeans.lib.profiler.heap.JavaClass;
import org.netbeans.lib.profiler.heap.PrimitiveArrayInstance;
import java.io.File;
import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.util.List;
/**
* @author Andrey Turbanov
* @since 10.08.2018
*/
public class MultiplexorDumpAnalyze {
public static void main(String[] args) throws IOException {
Heap heap = HeapFactory.createHeap(new File("D:\\acm_fail\\multiplexor_live1_java_pid1019835\\java_pid1019835.hprof"));
JavaClass marshalledClass = heap.getJavaClassByName("com.devexperts.io.Marshalled");
List<Instance> marshalledClassInstances = marshalledClass.getInstances();
System.out.println("There are " + marshalledClassInstances.size() + " Marshalled instances in heap");
Path dir = Paths.get("D:\\acm_fail\\multiplexor_live1_java_pid1019835\\marshalled_bytes");
for (int i = 0; i < marshalledClassInstances.size(); i++) {
if (i != 0 && i % 500 == 0) {
System.out.println("Process " + i + " Marshalled instance");
}
Instance marshalledInstance = marshalledClassInstances.get(i);
PrimitiveArrayInstance field = (PrimitiveArrayInstance) marshalledInstance.getValueOfField("bytes");
if (field == null) {
continue;
}
byte[] bytes = convertToArray(field);
if (bytes == null) {
continue;
}
long instanceId = marshalledInstance.getInstanceId();
Path fileToWrite = dir.resolve(Long.toString(instanceId) + ".bin");
Files.write(fileToWrite, bytes);
}
}
private static byte[] convertToArray(PrimitiveArrayInstance byteArray) {
int len = byteArray.getLength();
if (len < 1_000_000) {
return null;
}
byte[] to = new byte[len];
List<Object> values = byteArray.getValues();
for (int i = 0; i < to.length; i++) {
to[i] = Byte.parseByte((String) values.get(i));
}
return to;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment