File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
The moose likes Groovy and the fly likes High CPU and Memory Usage in Groovy Application Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login

Win a copy of REST with Spring (video course) this week in the Spring forum!
JavaRanch » Java Forums » Languages » Groovy
Bookmark "High CPU and Memory Usage in Groovy Application" Watch "High CPU and Memory Usage in Groovy Application" New topic

High CPU and Memory Usage in Groovy Application

xpratik Patel

Joined: Mar 03, 2011
Posts: 1

We are using groovy to automate our test executions. We have developed java library that contains business logic and is used by groovy.

The groovy script runs in a loop and performs following items
- Waits for an input. The input is provided by external system at a regular interval say 10 seconds
- Upon receiving an input, it performs some operations(which includes calls to some clouser)
- Prepares and sends the resposne
- Waits for an input (step 1)

Over the time, CPU and memory usage are increased. At some stage, groovy either crashes with OutOfMemory error or stops processing to further requests as CPU is exhausted.

We used JProfiler to investigate the root cause of the problem and found that much of the heap was occupied by some integer, char and Hashtables. We do not use Hashtables in our system and we only use a few integer variables. It was also observed that groovy was making recursive calls to the clouser but we could not identify which clousers are they. We only make a few clouser calls and they are not recursive, the depth of our clouser calls are upto only 2 levels.

JProfile CPU and Memory images are attached. From the memory usage graph it can be seen that the objects are constantly increasing into the memory and are not released. CPU usage graph clearly shows that clousers are called recursively. I suspect that once a clourser is called, all subsequent clourser calls added to the top of previous clouser calls and are not released even if the call to main clouser completes.

We have attached the code snippet for following groovy scripts
1. Runner.groovy - Entry point to the application. It takes other groovy scripts as an argument and executes them. We have implemented some custom commands in this file like 'include' which includes content of one groovy script into another. All these commands are executed as clouser.
2. Test1.groovy - This is the script which is passed as an argument to Runner.groovy and is executed by it.

Command: groovy Runner.groovy Test1.groovy

Can someone please check and advise why CPU and memory are growing?

import com.mii.context.*

if (args.length != 1)
USAGE: SendExpectRunner DSL_FILE
def contents = new File(args[0]).text

def include(file) {
def c = new File("./include/"+file).text
c = """
import com.mii.context.*
return {
def f =
f.delegate = del
f.resolveStrategy = Closure.DELEGATE_FIRST

def bind(params) {
params?.each { key ,value ->
binding[key] = value

def add(c) {
c = "return {\n"+c+"\n}"
def f =
f.delegate = this
f.resolveStrategy = Closure.DELEGATE_FIRST

def closureString = {

return {


onSuccess:{ params, s ->
onFailure:{ params, f ->
onTimeout:{ params, t ->



/* ScriptContext class is defined in java */
def context = new ScriptContext()
def closure =
closure.delegate = context
closure.resolveStrategy = Closure.DELEGATE_FIRST

========= Test1.groovy =========
/* Custom command 'include', details are in Runner.groovy */
include "SNMPMessages/SNMP_Dictionary.groovy"

/* Configurable Parameters */

APN = ''

printLog = {
def LogFile = new File(LOG_FILE)
currDate = getDateTimeString()
LogFile.append(currDate+" : "+logMessage+"\n")

/* Main Clouser */
Simulated_Call_Tester_Main = {

logMessage = " "

/* Initialize Variables */

/******* Call Setup *********/

if (CallSetupOIDReceived == 1) {
CallSetupOIDReceived = 0
/* Closure Call */
} else {
Expected_OID = Call_Setup
logMessage = "[Simulated_Call_Tester_Main] Expect Call_Setup OID ("+Expected_OID+")"

/* Closure Call */

/* Closure Call */

/* Closure Call */

if (activeCallFlag != 1)

/******* Call Clear Down ********/




[Thumbnail for cpu_usage.jpg]

[Thumbnail for memory_usage.jpg]

I agree. Here's the link:
subject: High CPU and Memory Usage in Groovy Application
jQuery in Action, 3rd edition