I have a Java RESTful service that creates a new JSON object every 10 seconds and pushes it to a URL to be consumed by AngularJS.
The problem is that I don't think the old JSON object is being freed up in memory. When I run task manager, I see that the java
process increases in size approximately every 10 seconds. I don't want this to happen when it's running indefinitely.
How can I fix this?
Here is the class:
@Path("/")
public class MQDepthJson extends TimerTask {
public MQDepthJson() {
}
private int count = 0;
private static final int ARRAY_SIZE = 11;
MQDepth[] mqDepths = new MQDepth[ARRAY_SIZE];
String[] c = {"c1", "c2", "c3", "c4", "c5", "c6", "c7", "c8", "c9", "c10", "c11"};
Timer timer;
public String getMQDepthJson() {
Gson gson = new Gson();
while(count < 11) {
MQDepth mq = new MQDepth();
int currentDepth = mq.calculateCurrentDepth();
mq.setCurrentDepth(currentDepth, c[count]);
mqDepths[count] = mq;
count++;
}
count = 0;
return gson.toJson(mqDepths);
}
@GET
@Path("/mqDepthJson")
@Produces("application/json")
public String runTask() {
while(true) {
Timer timer = new Timer();
timer.schedule(new MQDepthJson(), 10000);
return getMQDepthJson();
}
}
@Override
public void run() {
getMQDepthJson();
}
}