Running OQL queries from command line

OQL is a specialized query language for memory profiling of Java heap dumps. It is implemented by tools such as jvisualvm and jhat, which ship with current versions of JDK. Unfortunately, neither of these tools provides a simple command-line interface out of the box to integrate them into your own development environment and support real-life scripting. Rather, each of them gives you a kludgy and insufficient GUI.

However, it is easy simply to feed queries to the web server provided by jhat. You can download the OQL command line Perl script to do just that (see documentation).

A few notes from my playing around with OQL:

  • The ability to define your own Javascript functions is very useful, go for it!
  • Unfortunately, some of the handy built-in functions provided by jvisualvm are absent from jhat (need to define your own).
  • To obtain object IDs that can be passed to heap.findObject you must use the built-in function objectid(objectReference). The identifiers displayed by jvisualvm that look like #nnn are not objectids (that would be too easy, eh? I don't see any way to obtain them using jvisualvm GUI, have to use OQL).
  • To display the entire content of a char[] array (which jvisualvm cuts off in a useless way), you must use an OQL expression like objectReference.toString().
  • If OQL scares you, consider buying a commercial profiler.

No comments:

Post a Comment