It's not a secret anymore!*
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 OCA/OCP Java SE 7 Programmer I & II Study Guide this week in the OCPJP 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
Author

High CPU and Memory Usage in Groovy Application

xpratik Patel
Greenhorn

Joined: Mar 03, 2011
Posts: 1
Hi,

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?



Runner.groovy
==========
import com.mii.context.*

if (args.length != 1)
{
System.out.println("""
USAGE: SendExpectRunner DSL_FILE
""")
System.exit(-1)
}
def contents = new File(args[0]).text

def include(file) {
def c = new File("./include/"+file).text
c = """
import com.mii.context.*
return {
$c
}"""
def f = Eval.me(c)
f.delegate = del
f.resolveStrategy = Closure.DELEGATE_FIRST
f.setBinding(binding)
f.call()
}


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

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


def closureString = {

return {

binding.setVariable('del',delegate)

defaults(
onSuccess:{ params, s ->
bind(params)
s?.call()
},
onFailure:{ params, f ->
bind(params)
f?.call()
},
onTimeout:{ params, t ->
bind(params)
t?.call()
}
)

add(contents)

}
}

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


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

/* Configurable Parameters */
CONFIG_DATA = {

APN = 'xantic.bgan.inmarsat.com'
}


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


/* Main Clouser */
Simulated_Call_Tester_Main = {

while(1)
{
logMessage = " "
printLog()

/* Initialize Variables */
CONFIG_DATA()

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

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

/* Closure Call */
printLog()

/* Closure Call */
GetExpectedOID()

/* Closure Call */
Test_Call_Setup()
}

if (activeCallFlag != 1)
continue;

/******* Call Clear Down ********/
Test_Call_ClearDown();

}
}

Simulated_Call_Tester_Main()
==============================================




Thanks,
Pratik


[Thumbnail for cpu_usage.jpg]

[Thumbnail for memory_usage.jpg]

 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: High CPU and Memory Usage in Groovy Application