Click here to Skip to main content
15,892,809 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
Hi there,

I was wondering if anyone out there can help me, I'm looping through a selected folder with jpgs, while i'm looping through folder of files i'm querying my sql server 2008 db to see if there is already an image with that name, if there is i've made sure my project asks the user whether he/she would like to replace the old image, i can display the old image fine, but when i get to the messagebox i get an exception out of memory, tried and tried again and still failed. anyone have any idea's?

Thanks
Posted
Comments
[no name] 13-May-13 7:57am    
I do not see any "messagebox" in any of the code that you posted that demonstrates your problem.

Since you are doing this in a loop, it is entirely possible that it's not a "real" memory problem, but a resources one instead.

Are you Disposing of your SqlConnections, SqlCommands, and any SqlDataReaders? Are you createing Bitmaps which you also aren't Disposing? All of these things are limited resources, and if you do not dispose of them when you are finished, then they will only be freed when the Garbage Collector steps in because actual memory is low. This may not happen for a long time, and it is pretty easy to run out of resources well before that happens.
 
Share this answer
 
Comments
Mohammed Hameed 13-May-13 8:06am    
Perfect explanation. I would have given 10+ if possible. Thanks.
AntonyJackson 13-May-13 8:26am    
the only thing that remains open are the sql connections, everything else in the loop closes when it moves on to the next image. sorry i'm fairly new to .Net and still learning as i go along, the first image is bmp pulling from the file info while the other is a from sql and in a memory stream.
OriginalGriff 13-May-13 9:30am    
"Connection", not "Connections" - you shouldn't need more than one, reused.
The other items should be closed, and disposed - try a using block which handles it for you - and Bitmaps very definitely need to be disposed when you have finished with them as there is a very finite limit on bitmap handles you can create system wide.
AntonyJackson 13-May-13 9:41am    
what is block?
OriginalGriff 13-May-13 10:29am    
no, a "using block":
using (SqlCommand cmd = new SqlCommand("SELECT * FROM MyTable", con))
{
...
}
It handles the Dispose for you when the variable goes out of scope.
My solution = Solution 1 + 'I will first loop through folder only to get all jpgs, store in a collection like List<string>. Then in second task, out of this loop, will query the DB once only to verify the images which are already existing (this is achieved by passing the list of string to the query) and in return will get the already existing images names if any and store in a collection/array. Now I will just take a new loop and loop through it and show in message box to the user.

Hope it helps!
 
Share this answer
 
Comments
AntonyJackson 13-May-13 8:30am    
What my aim is, is to do everything in 1 loop, so in my main loop i'm querying to see if the item image already exists, then if it does, i ask if the user would like to replace it with the new image, after it replaces the new image, it then renames the image extension so i can track which images were replaced, inserted or skipped (if the user did not want to replace the image).
in the main loop, i was getting the image from sql with this

VB
Dim imageData As Byte() = DirectCast(SqlcmdImage.ExecuteScalar(), Byte())
                    If Not imageData Is Nothing Then
                        Using ms2 As New MemoryStream(imageData, 0, imageData.Length)
                            ms2.Write(imageData, 0, imageData.Length)
                            frmImportImages.PicNormal.background= Image.FromStream(ms2)
                            ms2.Dispose()
                        End Using
                    End If


I then changed the
VB
frmImportImages.PicNormal.background= Image.FromStream(ms2)
to
VB
frmImportImages.PicNormal.image = Image.FromStream(ms2)


And i've got no more memory errors. thanks everyone for your input, much appreciated, can't believe it was something so small that i had to change.
 
Share this answer
 
To my understanding there isn't much memory allocated here since the vehicles my new list should contain already exist inside the memory. I have to admit Vehicle is a very complex class and I tried to add about 50.000 items to the new list at once. But since all Vehicles in the application come from a database that is only 200MB in size I have no idea what may cause an Out of Memory exception at this point.
 
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