奇怪了内存不足的异常序列化过程中 [英] strange out-of-memory exception during serialization
问题描述
我使用VSTS2008 + C#+。NET 3.5的运行与12G物理内存的x64 Server 2003企业这个控制台应用程序。
I am using VSTS2008 + C# + .Net 3.5 to run this console application on x64 Server 2003 Enterprise with 12G physical memory.
下面是我的code,我觉得执行的语句bformatter.Serialize(流表)时,出现内存溢出异常。我通过任务管理器的Perormance标签监视内存的使用情况,我觉得只有2G的物理内存时使用异常被抛出,所以应该不会出现内存不足。 : - (
Here is my code, and I find when executing statement bformatter.Serialize(stream, table), there is out of memory exception. I monitored memory usage through Perormance Tab of Task Manager and I find only 2G physical memory is used when exception is thrown, so should be not out of memory. :-(
任何想法有什么不好?净系列化任何限制吗?
Any ideas what is wrong? Any limitation of .Net serialization?
static DataTable MakeParentTable()
{
// Create a new DataTable.
System.Data.DataTable table = new DataTable("ParentTable");
// Declare variables for DataColumn and DataRow objects.
DataColumn column;
DataRow row;
// Create new DataColumn, set DataType,
// ColumnName and add to DataTable.
column = new DataColumn();
column.DataType = System.Type.GetType("System.Int32");
column.ColumnName = "id";
column.ReadOnly = true;
column.Unique = true;
// Add the Column to the DataColumnCollection.
table.Columns.Add(column);
// Create second column.
column = new DataColumn();
column.DataType = System.Type.GetType("System.String");
column.ColumnName = "ParentItem";
column.AutoIncrement = false;
column.Caption = "ParentItem";
column.ReadOnly = false;
column.Unique = false;
// Add the column to the table.
table.Columns.Add(column);
// Make the ID column the primary key column.
DataColumn[] PrimaryKeyColumns = new DataColumn[1];
PrimaryKeyColumns[0] = table.Columns["id"];
table.PrimaryKey = PrimaryKeyColumns;
// Create three new DataRow objects and add
// them to the DataTable
for (int i = 0; i <= 5000000; i++)
{
row = table.NewRow();
row["id"] = i;
row["ParentItem"] = "ParentItem " + i;
table.Rows.Add(row);
}
return table;
}
static void Main(string[] args)
{
DataTable table = MakeParentTable();
Stream stream = new MemoryStream();
BinaryFormatter bformatter = new BinaryFormatter();
bformatter.Serialize(stream, table); // out of memory exception here
Console.WriteLine(table.Rows.Count);
return;
}
在此先感谢, 乔治
thanks in advance, George
推荐答案
RE /内存不足的2GB;个人.NET对象(如字节[]
后面的的MemoryStream
)被限制为2GB。也许尝试写入的FileStream
呢?
Re the out-of-memory / 2GB; individual .NET objects (such as the byte[]
behind a MemoryStream
) are limited to 2GB. Perhaps try writing to a FileStream
instead?
(编辑:没了:试过了,还是错误)
(edit: nope: tried that, still errors)
我也想知道,如果你可以用得到更好的结果(在这种情况下) table.WriteXml(流)
,或许与COM pression如GZIP,如果空间是premium。
I also wonder if you may get better results (in this case) using table.WriteXml(stream)
, perhaps with compression such as GZIP if space is a premium.
这篇关于奇怪了内存不足的异常序列化过程中的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!