HTMLUnit Java OutofMemory堆空间异常

时间:2015-07-30 06:59:57

标签: java htmlunit heap-memory

我一直在尝试解决从我的程序抛出的Java Heap Space OutOfMemory异常,该程序使用HTMLUnit API来访问页面(页面有大量的javascripts,因此我选择使用HTMLUnit)。程序运行大约1小时,遇到内存异常。我尝试了其他帖子中提出的不同方法,但都没有解决这个问题。 当程序在Linux上运行时:linux TOP命令显示该程序的内存使用量不断增加并达到90%,然后抛出OutOfMemory异常。 这些是我试图解决这个问题的方法:
1.堆积空间增加 2.run on jdk1.8
3.run on open jre1.7
4.使用最新版本的html单元,即htmlunit2.7
5.在finally块中使用了webclient.close()方法

<form id="commentform" method="POST" action="#" class="form form-inline form-contact">
    <p class="push-down-20">
        <input type="text" tabindex="3" size="30" value="" id="url" name="txtTitle">
         <label for="email">REVIEW TITLE<span class="red-clr bold">*</span></label>
    </p>
    <p class="push-down-20">
        <input type="text" aria-required="true" tabindex="1" size="30" value="" id="author" name="txtName" required>
        <label for="author">NAME<span class="red-clr bold">*</span></label>
    </p>
    <p class="push-down-20">
        <input type="email" aria-required="true" tabindex="2" size="30" value="" id="email" name="txtEmail" required>
        <label for="email">MAIL<span class="red-clr bold">*</span></label>
    </p>
    <p class="push-down-20">
        <input type="text" tabindex="3" size="30" value="" id="url" name="txtContact">
         <label for="email">CONTACT<span class="red-clr bold">*</span></label>
    </p>
    <p class="push-down-20">
        <textarea class="input-block-level" tabindex="4" rows="7" cols="70" id="comment" name="txtComment" placeholder="Your Review ..." required></textarea>
    </p>
     <p class="push-down-20">
        <!--<div class="tuto-cnt">-->
             <div class="rate-ex2-cnt">
                <div id="1" class="rate-btn-1 rate-btn"></div>
                <div id="2" class="rate-btn-2 rate-btn"></div>
                <div id="3" class="rate-btn-3 rate-btn"></div>
                <div id="4" class="rate-btn-4 rate-btn"></div>
                <div id="5" class="rate-btn-5 rate-btn"></div>
            </div>
<!--</div>-->
    </p>
    <p>
        <input class="btn btn-primary bold" type="submit" tabindex="5" id="rate-btn" name="rate-btn">Submit</button>
    </p>
</form>

即使我关闭所有窗口,每次新页面访问时此列表计数也会增加:

List<WebWindow> windows = client.getWebWindows();

我想知道HTMLUnit增加内存使用量的原因是什么,以及为什么即使调用webclient.close()方法,程序也不释放内存。 任何与此相关的建议将不胜感激。 谢谢。

编辑:

webclient.close()

2 个答案:

答案 0 :(得分:2)

你可能有内存泄漏,即你正在为一些集合添加一些对象(可能是for k, row in enumerate(state_matrix): for l, element in enumerate(row): state_matrix[k][l] = self.__sBoxSubstitution(element) WebWindow的实例)并且它们一直被保存在那里。 如果情况并非如此,那么它本身就是库中的问题,但首先要检查自己的代码。 BTW WebClient是自动关闭的,您可以使用HTMLPage语法。

答案 1 :(得分:0)

我建议使用try / catch / finally块并放入client.close();在finally块中