简体   繁体   English

Java:读取序列化文件时堆空间不足

[英]Java: not enough heap space when reading serialized file

In one method I create a serialized file from an adjacency list which I'm trying to read in another method as follows: 在一种方法中,我从邻接列表创建一个序列化文件,该列表试图在另一种方法中读取,如下所示:

Map<Integer, List<Integer>> adj;
        int[] count;
        int N;
        ArrayList<Double> weights;

            try (
                    ObjectInputStream input1 = new ObjectInputStream(new FileInputStream("GIRGnew5adj.ser"));
                ){


        N = (int)input2.readObject();     
        adj = (Map<Integer, List<Integer>>)input1.readObject();

Now, while the file itself is around 500MB large I get an OOM:Java Heap Space error even when I increase my heapsize to 2000MB but then again it works for a heap of 6000MB. 现在,尽管文件本身大约有500MB,但即使我将大小增加到2000MB,我也会收到一个OOM:Java堆空间错误 ,但是对于6000MB的堆,它仍然可以工作。

I wonder, how come does the same information take so much more space in memory than on hard disk? 我想知道,为什么相同的信息在内存中比在硬盘上占用更多的空间?

This is a very rough path of what happens. 这是发生的事情的很粗略的路径。 Just so you have an idea what the computer does when load the file. 如此您便知道了加载文件时计算机的功能。 This will not be 100% accurate and actual process might differ, be more efficient, this is just for puposes as giving an isight why the computer needs so much ram. 这将不是100%准确,实际过程可能会有所不同,效率会更高,这只是出于目的,为什么计算机需要那么多的内存。

  • The file is loaded into memory: 500mb. 该文件已加载到内存:500mb。
  • The file is parsed, temp variables are allocated during parsing, key value pairs are built up with type descriptions and verified, around 1000-1500mb 解析文件,在解析期间分配临时变量,使用类型描述构建并验证键值对,大约1000-1500mb
  • An object is instantiated. 实例化一个对象。 Default variables are loaded. 加载默认变量。 however much that takes. 但是需要很多。 lets say 0.01mb. 假设0.01mb。
  • Variables are assigned to object, values are cast to actual types. 将变量分配给对象,将值强制转换为实际类型。 another 500mb in temp variables. 另外500mb的临时变量。
  • Variables are in object instance, let's say 400mb. 变量位于对象实例中,比方说400mb。

So a total tally of rougly 3650.01mb. 因此总计3650.01mb。

Then the garbage collector comes by at some point and cleans up the mess and you're left with about 400mb. 然后,垃圾收集器会在某个时候到达并清理垃圾,剩下的空间约为400mb。

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

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