序列化期间奇怪的内存不足异常 [英] strange out-of-memory exception during serialization
问题描述
我正在使用VSTS2008 + C#+ .Net 3.5在具有12G物理内存的x64 Server 2003 Enterprise上运行此控制台应用程序。
I am using VSTS2008 + C# + .Net 3.5 to run this console application on x64 Server 2003 Enterprise with 12G physical memory.
这是我的代码,而我在执行语句bformatter.Serialize(stream,table)时发现内存异常。我通过任务管理器的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. :-(
任何想法有什么问题?Net序列化的任何限制?
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;
}
提前感谢
George
thanks in advance, George
推荐答案
注意: DataTable
默认为1. *中使用的xml序列化格式,令人难以置信的低效率。有一件事是切换到较新的格式:
Note: DataTable
defaults to the xml serialization format that was used in 1.*, which is incredibly inefficient. One thing to try is switching to the newer format:
dt.RemotingFormat = System.Data.SerializationFormat.Binary;
重新使用内存不足/ 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?
(编辑:nope:尝试,仍然是错误)
(edit: nope: tried that, still errors)
我也想知道你是否可以获得更好的结果(在这种情况下)使用 table.WriteXml(stream)
例如GZIP,如果空间是溢价。
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屋!