graphics.getDrawGraphics()高内存消耗

时间:2014-03-05 03:28:15

标签: java memory memory-management graphics2d

我的getDrawGraphics()方法存在高内存使用问题。它导致我“线性”内存消耗。除了从一个线程每16毫秒(~60fps)调用一次之外什么都不做,我将从30Mb内存使用量增加到超过一个hundread(平均每秒增加100Kb。有时甚至是1~7Mb)。这是myRepaint()方法。是的,我已经把它放在setIgnoreRepaint(true);

之前
public void myRepaint(){
    if(repaintInProgress) return;
    repaintInProgress = true;

    BufferStrategy strategy = getBufferStrategy();
    Graphics2D graphics = (Graphics2D) strategy.getDrawGraphics();
    /* draw graphics here */
    if(!loadedMap){
        drawMap(graphics);
        loadedMap = true;
    }
    drawTowers(graphics);
    drawCreeps(graphics);
    if(graphics != null) graphics.dispose();
    strategy.show();
    Toolkit.getDefaultToolkit().sync();
    repaintInProgress = false;
}

我尝试清除此功能中的所有行。内存消耗稳定,这意味着这种特定方法会导致内存问题。在逐行搜索之后,我发现getDrawGraphics()是这个问题的根源。所以,我尝试在myRepaint()中只放了这三行代码:

BufferStrategy strategy = getBufferStrategy();
Graphics2D graphics = (Graphics2D) strategy.getDrawGraphics();
graphics.dispose();

这让我相信无论graphics.dispose()是不起作用还是拥有8Gb的RAM,Java都无法释放这些内存。我进行了三次双重检查。我没有在方法执行期间实例化任何对象,因此没有理由增加内存使用量。为了跟踪它,我正在使用Windows任务管理器。

可能是什么问题?我该如何解决?

0 个答案:

没有答案