内存不足错误

时间:2016-01-17 23:31:18

标签: java file-io out-of-memory heap-memory overwrite

我试图在某人获胜或丢失二十一点游戏后重写文件。每次我运行游戏并获胜,我都会收到此错误:

pure c

这是setWinnings方法的代码

Exception in thread "AWT-EventQueue-0" java.lang.OutOfMemoryError: Java heap space
    at java.util.Arrays.copyOf(Unknown Source)
    at java.lang.AbstractStringBuilder.expandCapacity(Unknown Source)
    at java.lang.AbstractStringBuilder.ensureCapacityInternal(Unknown Source)
    at java.lang.AbstractStringBuilder.append(Unknown Source)
    at java.lang.StringBuilder.append(Unknown Source)
    at User.setWinnings(User.java:278)
    at Blackjack.addWinnings(Blackjack.java:182)
    at Blackjack.showWinMessage(Blackjack.java:323)
    at Blackjack.showOutcome(Blackjack.java:308)
    at Blackjack.actionPerformed(Blackjack.java:401)
    at javax.swing.AbstractButton.fireActionPerformed(Unknown Source)
    at javax.swing.AbstractButton$Handler.actionPerformed(Unknown Source)
    at javax.swing.DefaultButtonModel.fireActionPerformed(Unknown Source)
    at javax.swing.DefaultButtonModel.setPressed(Unknown Source)
    at javax.swing.plaf.basic.BasicButtonListener.mouseReleased(Unknown Source)
    at java.awt.Component.processMouseEvent(Unknown Source)
    at javax.swing.JComponent.processMouseEvent(Unknown Source)
    at java.awt.Component.processEvent(Unknown Source)
    at java.awt.Container.processEvent(Unknown Source)
    at java.awt.Component.dispatchEventImpl(Unknown Source)
    at java.awt.Container.dispatchEventImpl(Unknown Source)
    at java.awt.Component.dispatchEvent(Unknown Source)
    at java.awt.LightweightDispatcher.retargetMouseEvent(Unknown Source)
    at java.awt.LightweightDispatcher.processMouseEvent(Unknown Source)
    at java.awt.LightweightDispatcher.dispatchEvent(Unknown Source)
    at java.awt.Container.dispatchEventImpl(Unknown Source)
    at java.awt.Window.dispatchEventImpl(Unknown Source)
    at java.awt.Component.dispatchEvent(Unknown Source)
    at java.awt.EventQueue.dispatchEventImpl(Unknown Source)
    at java.awt.EventQueue.access$500(Unknown Source)
    at java.awt.EventQueue$3.run(Unknown Source)
    at java.awt.EventQueue$3.run(Unknown Source)

我已经尝试进入控制面板并将在java下调用的字段更改为-Xms2048m或其他内容。任何想法,为什么这不会起作用?

编辑:请注意,users.txt的大小只有82个字节。

2 个答案:

答案 0 :(得分:3)

你有一个循环

while(line != null) { 
    ...
}

但是在循环中你永远不会读到另一行。循环永远不会终止,您会继续将相同的数据附加到newFile。最终你消耗了所有的内存。

答案 1 :(得分:0)

您只需在line变量的一个声明阶段为String line = IO.readLine();变量赋值:while并且在line循环之前发生错误,在循环中您总是使用相同的第一个while没有用输入文件中的新行重新分配。

换句话说,line = IO.readLine();循环无限运行,或直到内存耗尽,就像你的情况一样。

您必须在while循环范围结束之前添加a = [['D', 'C', 'B', 'A'], ['1', '3', '2', '0'], ['1', '3', '2', '0']]