Hi,
I am getting an System.OutofmemoryException when using Filestream to load zip file. the file is 521MB. My code is:

//Insert using Filestream, file into SQL Server Table
        private void btnInsert_Click(object sender, EventArgs e)
        {
            OpenFileDialog openFileDlg = new OpenFileDialog();
            openFileDlg.InitialDirectory = Directory.GetCurrentDirectory();
            if (openFileDlg.ShowDialog() == DialogResult.OK)
            {
                FileInfo fi = new FileInfo(openFileDlg.FileName);
                FileStream fs = new FileStream(fi.FullName, FileMode.Open, FileAccess.Read);
                BinaryReader rdr = new BinaryReader(fs);
                byte[] fileData = rdr.ReadBytes((int)fs.Length);
                rdr.Close();
                fs.Close();

My program dies on this line: byte[] fileData = rdr.ReadBytes((int)fs.Length);
i have also attached the detail for the System.OutofMemoryException. I am runing this on a Windows 7, 64bit

machine.

thanks for your help.

Sharon

Edited 3 Years Ago by Dani: Formatting fixed

Attachments
subject: Filestreaming System.OutofMemoryException

Hi,
I am getting an System.OutofmemoryException when using Filestream to load zip file. the file is 521MB. My code is:

//Insert using Filestream, file into SQL Server Table
        private void btnInsert_Click(object sender, EventArgs e)
        {
            OpenFileDialog openFileDlg = new OpenFileDialog();
            openFileDlg.InitialDirectory = Directory.GetCurrentDirectory();
            if (openFileDlg.ShowDialog() == DialogResult.OK)
            {
                FileInfo fi = new FileInfo(openFileDlg.FileName);
                FileStream fs = new FileStream(fi.FullName, FileMode.Open, FileAccess.Read);
                BinaryReader rdr = new BinaryReader(fs);
                byte[] fileData = rdr.ReadBytes((int)fs.Length);
                rdr.Close();
                fs.Close();

My program dies on this line: byte[] fileData = rdr.ReadBytes((int)fs.Length);
i have also attached the detail for the System.OutofMemoryException.  I am runing this on a Windows 7, 64bit machine.

thanks for your help.

Sharon

So... how much RAM do you have in your system? I recommend you keep an eye on the Performance Monitor while your program is running. Watching the amount of free memory available might be very educational.

So... how much RAM do you have in your system? I recommend you keep an eye on the Performance Monitor while your program is running. Watching the amount of free memory available might be very educational.

Hi,

As I mentioned I have a machine that has 8 GB of memory. I had to shut down my machine to take it home last night.

When I tried to load the file again today it loaded with no problem. Normally I will only be loading one file at a

time, but in this case I was testing my Development database and was loading 28 files one right after the other.

Now my question is, since it is working ok now, is there a problem I still need to fix?

Sharon

You are casting to an int=int32. Should that not be long=int64 ?

Hi,

As I mentioned I have a machine that has 8 GB of memory. I had to shut down my machine to take it home last night.

When I tried to load the file again today it loaded with no problem. Normally I will only be loading one file at a

time, but in this case I was testing my Development database and was loading 28 files one right after the other.


Sharon

You are casting to an int=int32. Should that not be long=int64 ?

Hi,

As I mentioned I have a machine that has 8 GB of memory. I had to shut down my machine to take it home last night.

When I tried to load the file again today it loaded with no problem. Normally I will only be loading one file at a

time, but in this case I was testing my Development database and was loading 28 files one right after the other.

Sharon

As I mentioned I have a machine that has 8 GB of memory.

I've re-read your original post a number of times, and I'm still not seeing where you said how much RAM you have.

but in this case I was testing my Development database and was loading 28 files one right after the other.

Are they all about the same size, ~500 MB? If so, there's your answer: ~1/2 GB * 28 = ~14 GB, so if the CLR isn't collecting garbage fast enough, it'll run out of memory quite easily.

Now my question is, since it is working ok now, is there a problem I still need to fix?

I would imagine so. One success does not necessarily mean the issue is fixed. It's possible that, while there still may be a problem, it only comes up under rare circumstances. That may be acceptable to you--if so, then make a note of it and move on.

If you're interested in understanding what's happening and why, please humor me and do your test with the performance monitor running.

This article has been dead for over six months. Start a new discussion instead.