Class HeapDump
java.lang.Object
org.graalvm.tools.insight.heap.HeapDump
Support for generating
.hprof files in
Java Profiler Heap Dump Format.
Builder builder = HeapDump.newHeapBuilder(new FileOutputStream(hprof));
builder.dumpHeap((heap) -> {
final ClassInstance classActor = heap.newClass("cartoons.Actor").
field("name", String.class).
field("friend", Object.class).
field("age", int.class).
dumpClass();
final ObjectInstance jerry = heap.newInstance(classActor).
put("name", heap.dumpString("Jerry")).
putInt("age", 47).
// field 'friend' remains null
dumpInstance();
final ObjectInstance tom = heap.newInstance(classActor).
put("name", heap.dumpString("Tom")).
put("friend", jerry).
putInt("age", 32).
dumpInstance();
final ClassInstance classMain = heap.newClass("cartoons.Main").
field("tom", classActor).
field("jerry", classActor).
field("thread", java.lang.Thread.class).
dumpClass();
HeapDump.InstanceBuilder mainBuilder = heap.newInstance(classMain);
final ObjectInstance main = mainBuilder.id();
mainBuilder.put("tom", tom).put("jerry", jerry);
ObjectInstance cathingThread = heap.newThread("Catching Jerry").
addStackFrame(classActor, "tom", "Actor.java", -1, jerry, tom, main).
addStackFrame(classMain, "main", "Main.java", -1, main).
dumpThread();
mainBuilder.put("thread", cathingThread).dumpInstance();
});
- Since:
- 21.1
-
Nested Class Summary
Nested ClassesModifier and TypeClassDescriptionfinal classFills data for new array to put into theHeapDump.final classBuilder to construct content of the heap dump file.final classBuilds structure of a new class for theHeapDump.final classRepresents a class in theHeapDump.final classFills data for new object instance to put into theHeapDump.final classRepresents an object instance in theHeapDump.final classAllows one to describe a state of a thread with local variables and record it in the generatedHeapDump. -
Method Summary
Modifier and TypeMethodDescriptiondumpPrimitive(Object obj) dumpString(String text) Builds new string instance in theHeapDump.newArray(int len) Starts building an object array.Starts building new class for theHeapDump.static HeapDump.BuilderStarts generating new.hproffile.Starts building an instance of given class.Starts building new thread/event with a stacktrace and local variables.
-
Method Details
-
newHeapBuilder
Starts generating new.hproffile. Follow withHeapDump.Builder.dumpHeap(Consumer)call.Builder builder = HeapDump.newHeapBuilder(new FileOutputStream(hprof)); builder.dumpHeap((heap) -> { final ClassInstance classActor = heap.newClass("cartoons.Actor"). field("name", String.class). field("friend", Object.class). field("age", int.class). dumpClass(); final ObjectInstance jerry = heap.newInstance(classActor). put("name", heap.dumpString("Jerry")). putInt("age", 47). // field 'friend' remains null dumpInstance(); final ObjectInstance tom = heap.newInstance(classActor). put("name", heap.dumpString("Tom")). put("friend", jerry). putInt("age", 32). dumpInstance(); final ClassInstance classMain = heap.newClass("cartoons.Main"). field("tom", classActor). field("jerry", classActor). field("thread", java.lang.Thread.class). dumpClass(); HeapDump.InstanceBuilder mainBuilder = heap.newInstance(classMain); final ObjectInstance main = mainBuilder.id(); mainBuilder.put("tom", tom).put("jerry", jerry); ObjectInstance cathingThread = heap.newThread("Catching Jerry"). addStackFrame(classActor, "tom", "Actor.java", -1, jerry, tom, main). addStackFrame(classMain, "main", "Main.java", -1, main). dumpThread(); mainBuilder.put("thread", cathingThread).dumpInstance(); });- Parameters:
os- output stream to write data to- Returns:
- new builder
-
newClass
Starts building new class for theHeapDump.- Parameters:
name- the name of the class- Returns:
- builder to specify field names and types
- Throws:
UncheckedIOException- when an I/O error occurs- Since:
- 21.1
-
newThread
Starts building new thread/event with a stacktrace and local variables.- Parameters:
name- name of the thread- Returns:
- new thread builder
- Since:
- 21.1
- See Also:
-
newInstance
Starts building an instance of given class.- Parameters:
clazz- class with defined fields and their types- Returns:
- new instance builder
- Since:
- 21.1
-
newArray
Starts building an object array.- Parameters:
len- the size of the array- Returns:
- new array builder
- Since:
- 21.3.2
-
dumpString
Builds new string instance in theHeapDump. Encodes the value as an instance ofjava.lang.Stringwithvaluefield holding thechar[]of the providedtext.- Parameters:
text- the text of the string- Returns:
- instance representing the string in the heap.
- Throws:
UncheckedIOException- when an I/O error occurs- Since:
- 21.1
-
dumpPrimitive
Dumps a primitive value (int,long,float,byte,char& other) into theHeapDump. Encodes the value as appropriate boxed object (Integer,Long,Float,Byte,Character& co.).- Parameters:
obj- primitive value- Returns:
- object instance representing the value in the heap
- Throws:
UncheckedIOException- when an I/O error occurs- Since:
- 21.1
-