NCache序列化对象,然后仅67MB后抛出OutOfMemoryException

时间:2016-05-18 21:47:07

标签: .net ncache

该对象最终应序列化为大约350MB,这远低于32位应用程序的限制。

我正在设置我自己的Compact Serializer,所以我可以控制序列化,我发现MemoryStream消耗达到67,108,864字节的容量,位置是59,913,379并且它抛出OutOfMemoryException。 这有什么解决方法吗? 我的对象是递归的,并且递归调用serialize方法是否存在来自此的分配问题?

提前致谢。

修改

    [Serializable]
    public class TestObject : ICompactSerializable
    {
        public int[] testArray = new int[]{};
        public List<TestObject> testOfSelf = new List<TestObject>();
        public TestObject()
        {

        }
        public TestObject(TestObject insert)
        {
            testOfSelf.Add(insert);
        }


        public void Deserialize(Alachisoft.NCache.Runtime.Serialization.IO.CompactReader reader)
        {
            this.testArray = reader.ReadObjectAs<int[]>();
            this.testOfSelf = reader.ReadObjectAs<List<TestObject>>();
        }

        public void Serialize(Alachisoft.NCache.Runtime.Serialization.IO.CompactWriter writer)
        {
            writer.WriteObject(this.testArray);
            writer.WriteObject(this.testOfSelf);
        }
    }

        TestObject rootNode = new TestObject();
        int maxArray = 1000;
        int factor = 110058451;
        int ratchetBackDivisor = 5;
        var intArray = from o in new int[maxArray] select rand.Next(0, 10000);
        var sixty_seven_million_lessKilloByte = factor / maxArray;
        int recursionDepth = 20;
        int flatLevel = 5;
            Action<int,TestObject> recursion = null;
            recursion = (i,recurse) =>
                {
                    var newT = new TestObject();
                    newT.testArray = (from o in new int[maxArray] select rand.Next(0, 10000)).ToArray();

                    if (i != 0)
                    {
                        recursion(--i, newT);
                    }
                    recurse.testOfSelf.Add(newT);
                };
            for (int i = 0; i < ((sixty_seven_million_lessKilloByte / recursionDepth) / ratchetBackDivisor); i++)
            {
                //for (int j = 0; j < flatLevel; i++)
                {
                    var to = new TestObject();
                    recursion(recursionDepth, to);
                    rootNode.testOfSelf.Add(to);
                }                
            }
            long length = 0;
            using (Stream S = new MemoryStream())
            {
                BinaryFormatter bw = new BinaryFormatter();
                bw.Serialize(S, rootNode);
                length = S.Length;
                Console.WriteLine(string.Format(@"Total size of object is {0}", S.Length.ToString("#,##0")));
            }

使用这个,当长度为94610430时,我得到一个System.OutOfMemoryException

0 个答案:

没有答案