Monday, September 15, 2014

ant task to get system memory

I was facing the problem when trying to run a build.xml.
It was throwing OutOfMemory error.
I wanted to investigate it further , and for that I wanted to print out the free memory of the machine before an after my build ant targets starts executing.

There is no out of box solution from Ant.

I had to write an ant task as below :

package org.kp.common.anttask;

import org.apache.tools.ant.BuildException;
import org.apache.tools.ant.Task;

public class GetFreeMemory extends Task {

@Override
public void execute() throws BuildException {
Runtime rt = Runtime.getRuntime();
System.out.println(rt.freeMemory());
}

}
In the build.xml define the task as below :

<taskdef name="getFreeMemory"
classname="org.kp.common.anttask.GetFreeMemory" />


To use it just write 
<getFreeMemory/>


No comments: