Profiling Grails Applications With VisualVM

Describe how to profile a Grails application using the built-in Java VisualVM application.

Object Partners

Lately, I have been finding the need to get into the bowels of Grails in order to increase the performance of large batch type features of any application. Most of the time the issue appears to be with the Hibernate session becoming over-saturated with references. Performance degrades significantly even when limiting the batch size to ~100 records. Luckily, Oracle ships a pretty decent profiler with Java, VisualVM. The trick is getting it to work correctly with a Grails application.

Here are the tricks, I’ve found

  1. Add the command ‘-Xverify:none’ to your GRAILS_OPTS environment variable before launching your application. VisualVM relies on class replacement to instrument your code for profiling, and this switch allows that to work correctly.
  2. Disable the auto reloading agent in Grails by passing -Dauto.recompile.disable=true on the command line when you start your Grails application. This mostly a safety measure. Since VisualVM is replacing classes, you don’t want Grails replacing over the top of them. Also, there is a significant performance hit when using the Reloading Agent once it recompiles any class since it it basically proxying every class reference.
  3. Launch VisualVM with a bunch of memory. Since Grails needs a good chunk of memory, the profiler is going to need a lot. You can increase memory to VisualVM by passing the commands -J-Xms and -J-Xmx on the command line when launching VisualVM (i.e. jvisualvm -J-Xms1G -J-Xmx1G).
  4. While your there, you should add your Grails application classes to VisualVM’s classpath so you can make better sense of the profiling results. You can prepend/append classes to VisualVM’s classpath by using the —cp:p and —cp:a arguments, respectively (i.e. jvisualvm -J-Xms1G -J-Xmx1G —cp:a /target/classes

Share this Post

Related Blog Posts

JVM

DIY Graphing Calculator: Google Visualization Grails Plugin

January 22nd, 2013

Make your own graphing calculator in Grails with Google Chart Tools, the Google Visualization Plugin, and exp4j

Object Partners
JVM

Gradle Quick Start

January 18th, 2013

Getting started with Gradle, with examples. No installation necessary, get started quickly and painlessly with the included sample project.

Phil True
JVM

Customizing Grails Scaffolding and Adding Accessibility with the Fields Plugin

January 16th, 2013

Customize the way Grails generates user interfaces to get a look much closer to what you need with the fields plugin. Finally, Grails scaffolding can be useful.

Eric Foster-Johnson

About the author