Click here to Skip to main content
15,899,754 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I run my program for about 3 hours the I get the following error:

OutOfMemoryException was Unhandeld
Screen-compatible bitmap cannot be created. The screen bitmap format cannot be determined.

My program store large data which read from OPC sever in access database, also my program declare many controls at run time and make changes to the properties for these control.


My prorgram have two threads one for the main form, and another one for reading from the opc server.
Thanks in advance for any help.
Posted
Updated 27-Jun-11 20:48pm
v3
Comments
[no name] 23-Jun-11 5:37am    
what is the configuration of the system?
muathvb1 23-Jun-11 5:52am    
My program should continously read from an OPC server, so I make an infinte loop
to read from the server.
And according to the data which I get from the server I will display the result on the main form.
Sandeep Mewara 23-Jun-11 5:39am    
It's clear that your code is going into an infinite loop somewhere and thus an outofmemory exception. Now, either you find that or share the related code here so that others might point that out to you.
muathvb1 23-Jun-11 5:45am    
my prorgram has an infinte loop but this infinite loop is used to contiously read from the opc server, but I made this loop in different Thread not th main thread of the main form.
Sergey Alexandrovich Kryukov 23-Jun-11 12:15pm    
It's good to use a separate thread but such thread can leak the memory as well :-)
--SA

[AFTER DISCUSSION in comments to the answer]

If you did not pay attention on System.IDisposal, with System.Drawing you could possibly leak a lot of memory!

This interface allows to call its method called System.IDisposable.Dispose() usually used to clean different things. Very typically it is used by System.Drawing to dispose unmanages resources.

Here is how you should do:
C#
//sloppy version:
try {
   Brush myBrush = new SolidBrush(/*...*/);
   //use myBrush;
} finally { //in case exception was thrown
   myBrush.Dispose();
}

//This is strictly equivalent to the following code
//using special construct "using" designed for automatic call to Dispose;
//more elegant and supportable:
using (Brush myBrush = new SolidBrush(/*...*/) {
   //use myBrush
} //end using auto-calls myBrush.Dispose()


This is not just Brush. Check up every single .NET library object you create if their types implement IDisposable. When this is the case, you should always use the codelet with "using" I've demonstrated above to avoid serious leak. I don't understand how you failed to come across this important aspect before. (Maybe you're a beginner, so it's a very important thing to take into account.)

This construct and interface are one of the most important features if .NET. If you don't know it and trying to do GDI-based development, chances are, you lead a lot of unmanaged resources.

—SA
 
Share this answer
 
v3
Memory profilers may help you understand what could be going on.
Run them with your application to figure out the exact case of the problem.
 
Share this answer
 
Comments
muathvb1 26-Jun-11 2:16am    
How can I use them?
Abhinav S 26-Jun-11 2:24am    
Here is one such tool - check its documentation
The idea that .NET manages memory when using large objects, is a fantasy. You need to call dispose, but even then, in WPF apps especially, you need to basically reset the GC from time to time, or you will crash. The whole thing is very poorly written. The person who said to use the GC was, sadly, spot on.
 
Share this answer
 
Comments
muathvb1 28-Jun-11 9:54am    
How can I reset the GC?
Does there anydrawback for this or not?
Use Garbage Collection periodically to clear unwanted data from RAM.
 
Share this answer
 
Comments
muathvb1 23-Jun-11 5:47am    
How can I do this?
what is the code
PrafullaVedante 23-Jun-11 7:55am    
GC.collect()
Sergey Alexandrovich Kryukov 26-Jun-11 0:34am    
This is incorrect solution -- will not change anything, I'll guarantee that.
--SA
CPallini 23-Jun-11 5:58am    
Shouldn't garbage collection happen automatically?
Sergey Alexandrovich Kryukov 23-Jun-11 12:14pm    
Yes of course. GC.Collect only affects the exact moment of collection, could be useful in some quite rare cases. It can make things even worse.
I voted 1 for this answer.

Most likely, the problem is failure to release resources which is usually done via IDisposable.Dispose() using "using", as you pointed out.

I can be managed memory leak as well, but as bitmap is involved, it's more likely the failure to release.
--SA

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