After searching a lot and becoming confused and disapointed I want to post my question here. I had encountered problem of OutofMemoryException when using MemoryStream and finally got around with the help of MemoryTributary. Another problem still remains. I want to convert that stream to byte array but again get OutofMemoryException error when converting MemoryTributary (or even MemoryStream. I do not use MemoryStream now because it cant handle large memory) stream to byte array on windows XP while same code is running without thowing that error on Windwos 7. Follwoing is code:
DataContractSerializer ds = new DataContractSerializer(typeof(List
MemoryTributary stream2 = new MemoryTributary();
ds.WriteObject(stream2, tempdataitems);
byte[] data = stream2.ToArray(); // OutofMemoryException
My machine is core2 with 4GB RAM. I know this is not problem with RAM. Help is appreciated in solving this problem. I have to run that code on WinXP.

Jaganathan BantheswaranRank #170 · 14.1K reputationPosted Dec 5, 2013, 7:24 PM EST
Anzar Khatri15 reputationPosted Dec 6, 2013, 3:44 AM EST
public byte[] ToArray()
{
long firstposition = Position;
Position = 0;
byte[] destination = new byte[Length];
Read(destination, 0, (int)Length);
Position = firstposition;
return destination;
}
Please suggest a solution I'm stuck here.
If still anything u need plz ask me I want this problem to be solved asap.
Jaganathan BantheswaranRank #170 · 14.1K reputationPosted Dec 6, 2013, 7:51 PM EST
convert the stream chunks by chunks...
BufferManager bm = BufferManager.CreateBufferManager(chunkSize * 10, chunkSize);
for (int i = resumeChunk; i < chunks; i++)
{
byte[] buffer = bm.TakeBuffer(chunkSize);
try
{
fileStream.Position = i * chunkSize;
int actualLength = fileStream.Read(buffer, 0, (int)chunkSize);
if (actualLength == 0) break;
//Array.Resize(ref buffer, actualLength);
using (MemoryStream stream = new MemoryStream(buffer))
{
UploadFile(stream, actualLength);
}
}
finally
{
bm.ReturnBuffer(buffer);
}
}
Anzar Khatri15 reputationPosted Dec 9, 2013, 3:44 AM EST
If any1 has checked and it is working on XP plz let me know or any other solution.