Analyzing the Heap of GraalVM Polyglot Applications
This post was written by Jiří Sedláček — a tooling expert on the GraalVM team and author of VisualVM.
GraalVM — a high-performance polyglot virtual machine from Oracle Labs — enables programmers to create applications combining multiple programming languages within a single process. While this opens a world of new exciting possibilities, it also brings several challenges from the tooling perspective — traditional tools are not ready to work with polyglot applications. Luckily, GraalVM comes with a bunch of tools supporting multi-language monitoring, debugging and profiling.
Graal VisualVM — the only tool capable of viewing polyglot heap dumps — is bundled with GraalVM. As a start, download GraalVM from the downloads page and extract it to
$GRAALVM_HOME/bin/gu install python
$GRAALVM_HOME/bin/gu install r
$GRAALVM_HOME/bin/gu install ruby
For our article we create a simple Java class printing “Hello World” using various languages. While this is not real polyglot code — the languages don’t interact with each other — it creates some data structures for every guest language, which can be displayed in the heap viewer.
This is the content of the sample
PolyglotTest.java which we’ll use for generating the heap dumps. Feel free to uncomment the additionally installed guest languages that interest you:
The first line of the main method prints “Hello World” from a standard Java code. On the following lines we create a polyglot context for running the guest languages and print “Hello World” from each of these languages. The last two lines pause the execution thread for five minutes, which should be enough time for us to take the heap dump before the process finishes.
Now let’s compile the code and run it just like any other Java application:
Based on which languages were enabled, the process prints the appropriate “Hello World” messages and falls asleep.
Getting the Heap Dump
To create a heap dump from the PolyglotTest process, we use the Graal VisualVM tool. Alternatively you can use any other tool of your choice to dump the heap in the standard Java
.hprof format, like
JConsole etc. Just keep in mind that you still have to open the heap dump in Graal VisualVM to analyze the guest language structures.
To start Graal VisualVM use the following command, which is similar to starting the preceding Java VisualVM tool from JDK 6~8:
The tool starts and immediately shows the running PolyglotTest process in the Applications pane on the left side. Select the process node and invoke the Heap Dump action from its context menu. A heap dump from PolyglotTest is created and opened in the heap viewer.
Note that in case you need to obtain heap dump from a guest language REPL — js, graalpython, R or truffleruby — make sure it runs using the HotSpot JVM and not as a native image process. This can be achieved by passing the
--jvm flag to the REPL launcher:
Analyzing the Heap Dump
Once a heap dump is opened in Graal VisualVM — either automatically after creating it right from the tool, or manually by opening an external file using File | Load… action from the main menu — we’re presented a Java heap viewer with all the common features: reports, histograms, incoming and outgoing references to objects, paths to GC roots and many other. The Java heap viewer also allows us to analyze the dumps using custom queries and scripts in the OQL Console and R Console.
Where appropriate, objects display their logical values for easier identification (gray string next to the object identificator). Each object in the Objects view can be expanded to display its properties (fields, attributes etc. based on the language) and references, where available. Using context menu, the objects and types can be opened in a new tab for further analysis. The scope of displayed data can be easily lowered by switching to a subtree view using Pin buttons in the breadcrumbs stripe.
In this article, we described methods to save heap content of a polyglot application into a
We highly appreciate your feedback on the Graal VisualVM heap viewer, other tools and the GraalVM itself — both positive and negative. Feel free to leave your comments in the responses below this article, or file a feature request or report a bug on GitHub.