如何在线程“AWT-EventQueue-0”中解决异常java.lang.OutOfMemoryError:Java堆空间

时间:2012-10-11 10:20:44

标签: java jvm

我正在尝试读取大小为10 MB的xml文件。我正在使用Windows7操作系统(8GB RAM),Netbeans 7.1.2

    C:\>java -version
    java version "1.7.0_05"
    Java(TM) SE Runtime Environment (build 1.7.0_05-b05)
    Java HotSpot(TM) 64-Bit Server VM (build 23.1-b03, mixed mode)

C:\>free -m
             total       used       free     shared    buffers     cached
Mem:          8169       3175       4993          0          0          0
-/+ buffers/cache:       3175       4993
Swap:         8169         81       8087

我试过C:\>java -Xms5120m -Xmx6144m但没有帮助。

堆栈跟踪:

Exception in thread "AWT-EventQueue-0" java.lang.OutOfMemoryError: Java heap space
    at org.apache.xerces.xni.XMLString.toString(Unknown Source)
    at org.apache.xerces.parsers.AbstractDOMParser.characters(Unknown Source)
    at org.apache.xerces.impl.XMLDocumentFragmentScannerImpl.scanContent(Unknown Source)
    at org.apache.xerces.impl.XMLDocumentFragmentScannerImpl$FragmentContentDispatcher.dispatch(Unknown Source)
    at org.apache.xerces.impl.XMLDocumentFragmentScannerImpl.scanDocument(Unknown Source)
    at org.apache.xerces.parsers.XML11Configuration.parse(Unknown Source)
    at org.apache.xerces.parsers.XML11Configuration.parse(Unknown Source)
    at org.apache.xerces.parsers.XMLParser.parse(Unknown Source)
    at org.apache.xerces.parsers.DOMParser.parse(Unknown Source)
    at org.apache.xerces.jaxp.DocumentBuilderImpl.parse(Unknown Source)
    at javax.xml.parsers.DocumentBuilder.parse(DocumentBuilder.java:208)
    at ReadExcel.xmlappend(ReadExcel.java:343)
    at ReadExcel.jButton7ActionPerformed(ReadExcel.java:331)
    at ReadExcel.access$600(ReadExcel.java:42)
    at ReadExcel$7.actionPerformed(ReadExcel.java:176)
    at javax.swing.AbstractButton.fireActionPerformed(AbstractButton.java:1995)
    at javax.swing.AbstractButton$Handler.actionPerformed(AbstractButton.java:2318)
    at javax.swing.DefaultButtonModel.fireActionPerformed(DefaultButtonModel.java:387)
    at javax.swing.DefaultButtonModel.setPressed(DefaultButtonModel.java:242)
    at javax.swing.plaf.basic.BasicButtonListener.mouseReleased(BasicButtonListener.java:236)
    at java.awt.Component.processMouseEvent(Component.java:6038)
    at javax.swing.JComponent.processMouseEvent(JComponent.java:3260)
    at java.awt.Component.processEvent(Component.java:5803)
    at java.awt.Container.processEvent(Container.java:2058)
    at java.awt.Component.dispatchEventImpl(Component.java:4410)
    at java.awt.Container.dispatchEventImpl(Container.java:2116)
    at java.awt.Component.dispatchEvent(Component.java:4240)
    at java.awt.LightweightDispatcher.retargetMouseEvent(Container.java:4322)
    at java.awt.LightweightDispatcher.processMouseEvent(Container.java:3986)
    at java.awt.LightweightDispatcher.dispatchEvent(Container.java:3916)
    at java.awt.Container.dispatchEventImpl(Container.java:2102)
    at java.awt.Window.dispatchEventImpl(Window.java:2429)

1 个答案:

答案 0 :(得分:1)

我怀疑你的10 MB数据会产生大量的图形对象或其他重量级的对象。最简单的检查方法是添加-XX+HeapDumpOnOutOfMemoryError并分析所有内存的使用位置。

您可能希望减小最大堆大小,因此转储较小,因为某些工具(例如VisualVM)难以加载大堆转储。

Browsing a Heap Dump with VisualVM