私はBinaryFormatterを使用してデータをディスクにシリアル化していますが、スケーラビリティはあまり高くありません。 200Mbのデータファイルを作成しましたが、それを読み込めませんでした(解析が完了する前にストリームの終わりが見つかりました)。それは、脱直列化してから放棄するまで約30分間試行します。これは8GbのRAMを搭載したかなりまともなクアッドCPUボックスに搭載されています。c#シリアル化されたデータ
かなり複雑な構造をシリアル化しています。
htCacheItemsはCacheItemsのハッシュテーブルです。各CacheItemにはいくつかの単純なメンバー(文字列+ intなど)があり、ハッシュテーブルとリンクリストのカスタム実装も含まれています。サブハッシュテーブルはCacheItemValue構造体を指し、現在はキーと値を含む単純なDTOです。リンクされたリスト項目も同様に単純です。
失敗したデータファイルには、約400,000のCacheItemValuesが含まれています。
小規模なデータセットはうまくいきます(デシリアライズして大量のメモリを使用するよりも時間がかかります)。
public virtual bool Save(String sBinaryFile)
{
bool bSuccess = false;
FileStream fs = new FileStream(sBinaryFile, FileMode.Create);
try
{
BinaryFormatter formatter = new BinaryFormatter();
formatter.Serialize(fs, htCacheItems);
bSuccess = true;
}
catch (Exception e)
{
bSuccess = false;
}
finally
{
fs.Close();
}
return bSuccess;
}
public virtual bool Load(String sBinaryFile)
{
bool bSuccess = false;
FileStream fs = null;
GZipStream gzfs = null;
try
{
fs = new FileStream(sBinaryFile, FileMode.OpenOrCreate);
if (sBinaryFile.EndsWith("gz"))
{
gzfs = new GZipStream(fs, CompressionMode.Decompress);
}
//add the event handler
ResolveEventHandler resolveEventHandler = new ResolveEventHandler(AssemblyResolveEventHandler);
AppDomain.CurrentDomain.AssemblyResolve += resolveEventHandler;
BinaryFormatter formatter = new BinaryFormatter();
htCacheItems = (Hashtable)formatter.Deserialize(gzfs != null ? (Stream)gzfs : (Stream)fs);
//remove the event handler
AppDomain.CurrentDomain.AssemblyResolve -= resolveEventHandler;
bSuccess = true;
}
catch (Exception e)
{
Logger.Write(new ExceptionLogEntry("Failed to populate cache from file " + sBinaryFile + ". Message is " + e.Message));
bSuccess = false;
}
finally
{
if (fs != null)
{
fs.Close();
}
if (gzfs != null)
{
gzfs.Close();
}
}
return bSuccess;
}
resolveEventHandlerは、どのように私はこれを改善することができ、私は1つのアプリケーション内のデータを直列化し、別の(http://social.msdn.microsoft.com/Forums/en-US/netfxbcl/thread/e5f0c371-b900-41d8-9a5b-1052739f2521)
質問がある中で、それをロードしてる周りのためだけの仕事ですか?データのシリアライゼーションは常に効率的ではないでしょうか?私自身のルーチンを書いたほうがいいですか?
説明を模倣しようとする例を追加しました。 –
(リンクされたリストの問題を修正; r263。)バイナリを再リリースしていないので、これを評価したり、自分の投稿にコメントを追加したり、私に電子メールを送ってください。(プロファイルを参照) –