BinaryFormatter를 사용하여 디스크에 데이터를 serialize했지만 매우 확장 성이 좋지 않습니다. 200MB의 데이터 파일을 만들었지 만 다시 읽을 수 없습니다 (분석이 끝나기 전에 발생한 스트림의 끝). 그것은 deserialise에 약 30 분을 시도한 다음 포기합니다. 이것은 8Gb RAM이있는 상당히 괜찮은 쿼드 - CPU 상자에 있습니다.C# serialize 된 데이터
상당히 복잡한 구조를 직렬화하고 있습니다.
htCacheItems는 CacheItems의 해시 테이블입니다. 각 CacheItem에는 여러 간단한 멤버 (문자열 + int 등)가 있으며 Hashtable과 연결된 목록의 사용자 지정 구현이 포함되어 있습니다. 하위 해시 테이블은 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는 어떻게이 문제를 개선 할 수 있습니다, 내가 한 응용 프로그램에서 데이터를 serialising 다른 (http://social.msdn.microsoft.com/Forums/en-US/netfxbcl/thread/e5f0c371-b900-41d8-9a5b-1052739f2521)
질문은에 넣기하고있어 주위에 있기 때문에 단지 일을 무엇입니까? 데이터 직렬화는 항상 비효율적일까요? 내 자신의 루틴을 작성하는 것이 더 낫지 않습니까?
설명을 모방하려고하는 예제를 추가했습니다. –
(연결된 목록의 문제 해결, r263 참고 바이너리를 다시 릴리스하지 않았습니다.이 값을 지정하거나 내 게시물에 주석을 추가하거나 전자 메일로 보내주십시오 (프로필 참조) –