다음 코드에서는 File 객체의 모든 Block 객체를 전달하고 BitConverted 기반 직렬화를 수행합니다. 어떤 경우에는 OutOfMemory 예외가 있습니다. 최적화 할 방법이 있습니까?사용자 정의 직렬화를 사용할 때 OutOfMemory 예외
File.Serialze();
public byte[] Serialize()
{
byte[] bytes = new byte[Blocks.Count * Block.Size];
for (int i = 0; i < Blocks.Count; i++)
{
Block block = Blocks[i];
Buffer.BlockCopy(block.Serialize(), 0, bytes, i * Block.Size, Block.Size);
}
return bytes;
}
Block.Serialize()
public byte[] Serialize()
{
byte[] bytes = new byte[Size];
Buffer.BlockCopy(BitConverter.GetBytes(fid), 0, bytes, 0, sizeof(long));
Buffer.BlockCopy(BitConverter.GetBytes(bid), 0, bytes, sizeof(long), sizeof(long));
Buffer.BlockCopy(BitConverter.GetBytes(oid), 0, bytes, sizeof(long) * 2, sizeof(long));
Buffer.BlockCopy(BitConverter.GetBytes(iid), 0, bytes, sizeof(long) * 3, sizeof(long));
Buffer.BlockCopy(BitConverter.GetBytes(did), 0, bytes, sizeof(long) * 4, sizeof(long));
return bytes;
}
대신 바이트 []의 MemoryStream을하고 BitConverter.GetBytes 대신 편이() 메소드
File.Serialize()
public MemoryStream Serialize()
{
MemoryStream fileMemoryStream = new MemoryStream(Blocks.Count * Block.Size);
foreach (Block block in Blocks)
{
using (MemoryStream blockMemoryStream = block.Serialize())
{
blockMemoryStream.WriteTo(fileMemoryStream);
}
}
return fileMemoryStream;
}
Block.Serialize()
public MemoryStream Serialize()
{
MemoryStream memoryStream = new MemoryStream(Size);
memoryStream.Write(ConvertLongToByteArray(fid), 0, sizeof(long));
memoryStream.Write(ConvertLongToByteArray(bid), 0, sizeof(long));
memoryStream.Write(ConvertLongToByteArray(oid), 0, sizeof(long));
memoryStream.Write(ConvertLongToByteArray(iid), 0, sizeof(long));
memoryStream.Write(ConvertLongToByteArray(did), 0, sizeof(long));
return memoryStream;
}
private byte[] ConvertLongToByteArray(long number)
{
byte[] bytes = new byte[8];
bytes[7] = (byte)((number >> 56) & 0xFF);
bytes[6] = (byte)((number >> 48) & 0xFF);
bytes[5] = (byte)((number >> 40) & 0XFF);
bytes[4] = (byte)((number >> 32) & 0XFF);
bytes[3] = (byte)((number >> 24) & 0xFF);
bytes[2] = (byte)((number >> 16) & 0xFF);
bytes[1] = (byte)((number >> 8) & 0XFF);
bytes[0] = (byte)((number & 0XFF));
return bytes;
}
Marc Gravell, 먼저 감사의 말을 들어 주셔서 감사합니다. * 크기는 대용량입니다. 저는 byte [] 대신 MemoryStream을 구현하고 BitConverter.GetBytes() 대신 Shift를 구현했습니다. 같은 문제가 다시 나타납니다. 내 구현을 추가했습니다, 아마도 거기에 몇 가지 문제가있어. – eugeneK
@eugeneK 폭발 할 때 스택 트레이스는 무엇입니까? 만약 데이터가 * large *이면,'MemoryStream'은 여전히 같은 문제를 겪을 것입니다 : 당신은 메모리의 외부 *를 직렬화해야 할 수도 있습니다. 어떤 종류의 크기를 기대하십니까? 대신 파일에 직렬화 해보십시오. –
File.Size가 8K에서 20MB 사이임 – eugeneK