使用java.lang.OutOfMemoryError的Junit测试已被捕获,Java堆空间

时间:2013-03-11 14:18:58

标签: java testing junit playframework

我正在尝试对Web应用程序进行一些junit测试,并且我不断收到OutOfMemoryError。我知道它在代码中发生了什么,但我不确定它为什么会这样做。

Survey survey = new Survey();
        SurveyLayoutColumn column1 = new SurveyLayoutColumn();
        column1.addAnswerSpace(new Answer());
        SurveyLayoutColumn column2 = new SurveyLayoutColumn();
        column2.addAnswerSpace(new Answer());
        column2.addAnswerSpace(new Answer());
        SurveyLayoutColumn column3 = new SurveyLayoutColumn();
        column3.addAnswerSpace(new Answer());
        survey.addLayoutColumn(column1);
        survey.addLayoutColumn(column2);
        survey.addLayoutColumn(column3);
        survey.addQuestion(new Question());
        survey.addQuestion(new Question());
        survey.addQuestion(new Question());
        survey.addQuestion(new Question());
        Application app = new controllers.Application();
        app.saveSurvey(survey);
        long count = Survey.count();
        assertEquals(count,0);

它发生在app.saveSurvey(确定),这是该方法的代码

public static void saveSurvey(Survey survey) {
        System.out.println("Survey title is: " + survey.title);

        survey.published = true;
        survey.save();

        Map<String, Object> viewData = new HashMap<String, Object>();

        viewData.put("status", "success");

        JSONSerializer serializer = new JSONSerializer();

        serializer
            .include("status")
            .exclude("*");

        renderJSON(serializer.serialize(viewData));
    }

为什么会发生这种情况会有任何帮助。

编辑: 这是跟踪

java.lang.OutOfMemoryError: Java heap space
    at java.util.Arrays.copyOf(Arrays.java:2882)
    at java.lang.AbstractStringBuilder.expandCapacity(AbstractStringBuilder.java:100)
    at java.lang.AbstractStringBuilder.append(AbstractStringBuilder.java:390)
    at java.lang.StringBuilder.append(StringBuilder.java:119)
    at play.data.binding.Unbinder.unBind(Unbinder.java:106)
    at play.data.binding.Unbinder.unBind(Unbinder.java:110)
    at play.data.binding.Unbinder.unBind(Unbinder.java:63)
    at play.data.binding.Unbinder.unBind(Unbinder.java:110)
    at play.data.binding.Unbinder.unBind(Unbinder.java:110)
    at play.data.binding.Unbinder.unBind(Unbinder.java:63)
    at play.data.binding.Unbinder.unBind(Unbinder.java:110)
    at play.data.binding.Unbinder.unBind(Unbinder.java:110)
    at play.data.binding.Unbinder.unBind(Unbinder.java:63)
    at play.data.binding.Unbinder.unBind(Unbinder.java:110)
    at play.data.binding.Unbinder.unBind(Unbinder.java:110)
    at play.data.binding.Unbinder.unBind(Unbinder.java:63)
    at play.data.binding.Unbinder.unBind(Unbinder.java:110)
    at play.data.binding.Unbinder.unBind(Unbinder.java:110)
    at play.data.binding.Unbinder.unBind(Unbinder.java:63)
    at play.data.binding.Unbinder.unBind(Unbinder.java:110)
    at play.data.binding.Unbinder.unBind(Unbinder.java:110)
    at play.data.binding.Unbinder.unBind(Unbinder.java:63)
    at play.data.binding.Unbinder.unBind(Unbinder.java:110)
    at play.data.binding.Unbinder.unBind(Unbinder.java:110)
    at play.data.binding.Unbinder.unBind(Unbinder.java:63)
    at play.data.binding.Unbinder.unBind(Unbinder.java:110)
    at play.data.binding.Unbinder.unBind(Unbinder.java:110)
    at play.data.binding.Unbinder.unBind(Unbinder.java:63)
    at play.data.binding.Unbinder.unBind(Unbinder.java:110)
    at play.data.binding.Unbinder.unBind(Unbinder.java:110)
    at play.data.binding.Unbinder.unBind(Unbinder.java:63)
    at play.data.binding.Unbinder.unBind(Unbinder.java:110)

1 个答案:

答案 0 :(得分:0)

好吧,似乎Unbinder.unbind()处于一个循环中,调用自身,并且每次调用它都会吸收一点内存;它可以通过堆栈来实现,它在每次调用时使用,或者连接到AbstractStringBuilder。

内存不足是堆栈跟踪不一定指向问题行的一次;在这些情况下,堆栈跟踪通常会显示一些这样的循环。