playframework/playframework — pull request #1402 by jroper (2013-07-31 01:06:03+00:00)
Title: Memory leak

Our app gets slower and slower with each reload until it finally gets totally unresponsive or crashes. This is easy to reproduce. Create a new Play Java app, use the following example Application.java, and then type "play run". Change something minor in it like the message and refresh the page, change the message and refresh the page again, etc. until it gets worse and worse.

package controllers;

import java.util.*;

import play.*;
import play.mvc.*;

import views.html.*;

public class Application extends Controller {

public static List numbers = new ArrayList();

public static Result index() {
Date start = new Date();
boolean didWork = generateRandomData();
String message = "Your application is ready.";
if (didWork) {
Date end = new Date();
long seconds = (end.getTime() - start.getTime()) / 1000;
message = "Your application generated " + numbers.size() + " numbers in " + seconds + " seconds.";
System.out.println(message);
}
return ok(index.render(message));
}

private static synchronized boolean generateRandomData() {
int num = 10_000_000;
if (numbers.size() < num) {
Random r = new Random();
for (int i = 0; i < num; i++) {
numbers.add(r.nextInt());
}
return true;
}
return false;
}

}