1

The object should ultimately serialize to about 350MB which is well under the limit for 32 bit apps.

I am setting up my own Compact Serializer so I can control serialization and I am finding that the MemoryStream consumption is reaching a Capacity of 67,108,864 bytes and the position is 59,913,379 and it throws an OutOfMemoryException. Is there any workaround for this? My object is recursive and the serialize method is called recursively is there any problem with allocation that comes from this?

Thanks in advance.

EDIT

    [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")));
            }

Using this I get an System.OutOfMemoryException when the length is 94610430

n8CodeGuru
  • 413
  • 1
  • 6
  • 13

0 Answers0