last day (15 days later) » 

01:59
0
Q: Cannot find the cause of Memory Leak in C# Winforms Application

technoI have a program that processes multiple images.The memory spikes from 70MB to 300-400-600Mb and comes down when processing a large number of high resolution images. I have disposed of all Bitmaps,used the using statment wherever possible,but the memory leak cannot be fixed.Can someone please hel...

What does FindFont method look like. Fonts should also be disposed if not in use.
@TnTinMn Please see the update.. okay.. But does font consume that much memory?
dbc
dbc
y = UpdatedResizeImage(y, new Size(pictureBox5.Width, pictureBox5.Height)); -- here you replace the old y with a new y and never dispose the first one. Similar problem: Why does my program's memory usage rise quickly and then go flat?. Also, can you simplify this and clean up the formatting somewhat?
@dbc okay... sorry.. i will clean up and fix the formatting..
@dbc How to cleanly create a new instance of y?
Have you thought about calling the garbage collector explicity after processing an image? It helps when you process the images really fast, so the GC would usually wait some time between freeing memory.
TaW
TaW
01:59
Are you sure you actually have a leak or simply enough RAM so GC isn't needed for long stretches? High RAM usage != leak !
This is a big source of memory leaks: pictureBox5.Controls.Clear();. Yuo have to dispose of the control assigned to a container (even though a PictureBox is not a container and you're not showing what is being cleared), not clear the list. Never do that. Clearing the list, you won't dispose of anything. All the child controls are still alive.
@Mitja This will cause performance issues.
@TaW I'm running on a VM with 7.4 GB of RAM
@Jimi Thanks.. i will try doing that.
TaW
TaW
That's a nice chunk of RAM. Whicjh means there is plenty to use and little need to regain it quickly. so you may not even hava a real problem. Can you also check the GDI handles? (In one of the the taskmanager panes) ?
@Jimi I removed those lines and also nothing is added to the picturebox in the loop.Same issue exists.
@TaW The GDI Object number is around 130.
You have some objects there which need to be disposed of. The Font you create (it consumes very little, but it's disposable, so...). Before assigning an Image to a PictureBox, dispose of the previous one. The StringFormat object is also disposable. Other object are unknown (assigned elsewhere) so its unclear what's their destinty (target, displaybitmap). Btw, your code is redacted with some errors in the composition, it wouldn't compile. So, this code may be missing some parts or is different from the original. The whole process is somewhat messy. You need to clean it up.
TaW
TaW
01:59
I can't say that I see that you are actually describing a memory leak. Which would be a case where memory consumption does not spike but grows all the time. Does it reach a limit or does it keep growing?? - 130 GDI objects is not a problem. The problems come when this number also keeps climbing..
@Jimi I have disposed off the Fonts as per the comments on this question.I will dispose off the StringFormat and clean the code.The Image<Bgr, byte> img is an EmguCV Image emgu.com/wiki/index.php/Working_with_Images
@TaW It starts with around 70MB.This is not a constant increase(comes up and down frequently) 70->200->70-->200>100>300>200>400>500>100 and when the process completes it returns back to 70.. I'm dealing with EmguCV Image in the program.. so don't know if that might be the issue.
TaW
TaW
My verdict is: You do not have a memory leak.
Did you forget about the PictureBox.Image assignment?
@TaW so this high spike in memory is not considered as leak.
@Jimi I keep getting 'Parameter not Valid' exception when assigning picturebox image like this 'picturebox.image=bmp;' so I use the new keyword..
Nope. What I said is Before assigning an Image to a PictureBox, dispose of the previous one.
01:59
@Jimi okay... will try doing that..
Also, as TaW noted, the memory usage may increase for a while. If you stop the activity for a while, you may see the yellow cone. Or you may not. It depends on the overall memory requirements. A VM is probably not the best of the environments to test this kind of things.
TaW
TaW
so this high spike in memory is not considered as leak Indeed. A leak is memory that is lost until the application is closed. You spikes are just the system optimizing for speed at the temporary cost of memory.
Of course, you need to fill all the visible holes in your code. After that, a leak is quite visible. Some procedure may cause the memory usage to increase and, after that, it never goes down. A constant increase in the number of handles and/or GDI/User objects in specific activities is also quite visible.
 
2 hours later…
04:16
@Jimi okay....
@TaW okay...

last day (15 days later) »