Click here to Skip to main content
15,887,683 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi In my table, there is a field for store Pictures, it works well and no problem.
But when I read pictures from DB the Message “Out of Memory” will be shown.
I think that commands File.Flush() or File.Dispose() or File.Close() will clean Buffer of Memory but these commands not do any thing.
The code:

C#
System.IO.FileStream File = new System.IO.FileStream("C:\\ TempFile\\" + PictureFileName_Field, System.IO.FileMode.Create, System.IO.FileAccess.Write);
            byte[] bytFile = new byte[File.Length + 1];
            
            bytFile = (byte[])(PictureFile_Field);
            File.Write(bytFile, 0, bytFile.Length - 1);

            File.Flush();
            File.Dispose();
            File.Close();


Languge c#, and db is sql server.
Thanks very much

What I have tried:

I Don't know what must i do.help me
.........................
thanks
Posted
Updated 18-Apr-20 3:32am

I generally make use of a using[^] block when I am using disposable objects.

Not only does it take care of the disposable object itself, any objects created within it will fall out of scope when the block is closed, so the byte array can be picked up by the Garbage Collector immediately.

You can also simplify your code by including a link (using) statement for Sytem.IO at the top of the page, then you can remove the references to it when calling for the FileStream references.
C#
using System.IO.; // top of page
// prior code

using (FileStream File = new FileStream("C:\\ TempFile\\" + PictureFileName_Field, FileMode.Create, FileAccess.Write)) {

	byte[] bytFile = new byte[File.Length + 1];
	bytFile = (byte[])(PictureFile_Field);

	File.Write(bytFile, 0, bytFile.Length - 1);
	File.Flush();

	File.Close();   // some say not needed, but doesn't hurt anything so why not
	File.Dispose(); // can omit, not really needed when using a USING block
}
References:
using statement - C# Reference | Microsoft Docs[^]
FileStream Class (System.IO) | Microsoft Docs[^]
c# - How do I properly dispose the byte array used in the class? - Stack Overflow[^]
 
Share this answer
 
The Flush and Close methods merely ensure that all data has been written to the file correctly. They do not change anything in memory, your code needs to delete any objects that are no longer required..

C#
byte[] bytFile = new byte[File.Length + 1]; // a new array in memory - which you never use

bytFile = (byte[])(PictureFile_Field); // now set it to point to Picture_Field
File.Write(bytFile, 0, bytFile.Length - 1); // write the bytes to a new file

File.Flush();
File.Dispose();  // dispose the File object
File.Close();    // you cannot call Close on a disposed object
//
// but the PictureFile_Field still exists in memory, you need to delete it
 
Share this answer
 
v2
Quote:
... when I read pictures from DB the Message “Out of Memory” will be shown.

Start by looking at how you are reading the data from the DB:
If you are using
SQL
SELECT * FROM ...
Then don't - list your fields so you don't return more data than you need.
Then look at what you are using to fetch the data - if your code uses a SqlDataAdapter to fill a DataTable or DataSet then that requires SQL Server to collect all the data together and transfer it as one lump - pictures can be quite large, so if there are a lot of them, then could overload your system or SQL server (and if you are working with a local DB for development, remember that means at least twice the data space is needed on the same machine). Try switching to a DataReader which returns rows on demand rather than all at once.
Then look at the number of rows you are fetching: unless you use a WHERE clause on your SQL you will get all of them, so it's possible that you are fetching a large number of images you don't need. Add a WHERE clause, or try returning data in pages of ten or twenty instead of all at once, that should reduce memory usage as well.

And keep an eye on the memory monitor in the debugger while you run your app - see how much it's actually using ("Out of memory" errors aren't only triggered by "real memory" running low - you also get it for scarce resources like handles, and graphics contexts, so it may be some other part of your code that is actually causing the problem. Check the exception detail in the debugger to try and find more info.
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900