该对象最终应序列化为大约 350MB,这远低于 32 位应用程序的限制。
我正在设置自己的紧凑型序列化器,以便可以控制序列化,我发现 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