views:

35

answers:

1

Hi,

I have webservice, with method:

[WebMethod]
public byte[] GetFile(string FName)
{
    System.IO.FileStream fs1 = null;
                fs1 = System.IO.File.Open(FName, FileMode.Open, FileAccess.Read);
    byte[] b1 = new byte[fs1.Length];
    fs1.Read(b1, 0, (int)fs1.Length);
    fs1.Close();
    return b1; 
}

and it works with small file like 1mb, but when it comes to photoshop's file (about 1,5gb) I get:

System.OutOfMemoryException

on this line:

Byte[] img = new Byte[fs.Length];

The idea is I have winforms application which get this file and saving it on local disc.

+2  A: 

This all depends on where the exception is being thrown. Most likely what is happening is that a file that size is going into the Large Object Heap (LOH) and the memory fragmentation is causing that problem.

http://msdn.microsoft.com/en-us/magazine/cc534993.aspx

http://blogs.msdn.com/maoni/archive/2006/04/18/large-object-heap.aspx

http://blogs.msdn.com/webtopics/archive/2009/05/22/Troubleshooting-System.OutOfMemoryExceptions-in-ASP.NET.aspx

Link to a similiar SO question:
http://stackoverflow.com/questions/2860917/how-do-i-get-c-to-garbage-collect-aggressively/2860930#2860930

Kevin