简体   繁体   English

异常OutOfMemoryError:Java堆空间

[英]Exception OutOfMemoryError: Java heap space

I want to save a big object 我想保存一个大物件

 public void saveGenes() throws FileNotFoundException, IOException{
        String file = "genes.obj";
        String start = String.format("%tr", new Date());
        System.out.println("Save "+file+"...... "+start+"\n");
        ObjectOutputStream oos = new ObjectOutputStream(new     BufferedOutputStream(new FileOutputStream(file)));
        Genes g = new Genes();
        g=this;
        oos.writeObject(g);
        oos.close();
        String end = String.format("%tr", new Date());
        System.out.println("Save Completed......" + end);
    }

But I have to try this error Exception in thread "main" java.lang.OutOfMemoryError: Java heap space at java.io.ObjectOutputStream$HandleTable.growEntries(Unknown Source) 但是我必须在线程“ main”中尝试此错误异常。java.lang.OutOfMemoryError:java.io.ObjectOutputStream $ HandleTable.growEntries(未知源)处的Java堆空间

I tested with -Xmx4096M, but the error keeps coming 我使用-Xmx4096M进行了测试,但错误不断出现

You could try breaking up the data into smaller parts also you could try calling System.gc() , but in my experience, it does not do any thing. 您可以尝试将数据分解为较小的部分,也可以尝试调用System.gc() ,但是以我的经验,它什么也没做。 But if it is placed strategically, I've heard that it works. 但是,如果从战略角度考虑放置它,那么我听说它是​​可行的。

I don't know any specific reason as to why this is happening. 我不知道发生这种情况的任何具体原因。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM