Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why does my Winforms program reserve so much virtual memory?

I have a C#/.NET 4.0 application that when I start it up shows two windows with about a dozen controls. When I run my program (Debug or Release doesn't matter), before I even do anything in it, I see in Task Manager/Resource Monitor that my program already has upwards of 450MB of private memory. I realize Task Manager isn't the most reliable way of measuring memory usage, but it is one of the most visible to my users.

When I run the VS2010 .NET Memory Allocation performance analysis, for a full run of my program, it reports about 5MB of RAM actually allocated for managed objects (my program normally uses a few unmanaged objects as well, but they are very small and to simplify this investigation I've disabled them, though to no notable effect). Likewise, if I call EmptyWorkingSet() from psapi.dll after my main form has been shown, my private memory drops to ~3.5 MB.

I've already looked at the questions about memory footprints here and here, but those questions seem to be dealing with programs that show up as a couple dozen Megabytes. My program shows almost 500MB, which looks a lot more worrisome.

I can't imagine all of that is from overhead; why is there such a huge discrepancy between the VS profiler and Task Manager?

Update: Interestingly enough, if I comment out a the part of InitializeComponent() that sets up the ImageLists, the number in Task Manager stays under 10MB. I have two sets of PictureBoxes and ImageLists where the PictureBox displays one of four images depending on which radio button in a radio button group is selected.

These lines of code are the ones that seem to trigger the massive memory increase:

// 
// directionImageList
// 
this.directionImageList.ImageStream = ((System.Windows.Forms.ImageListStreamer)(resources.GetObject("directionImageList.ImageStream")));
this.directionImageList.TransparentColor = System.Drawing.Color.White;
this.directionImageList.Images.SetKeyName(0, "Dir1.png");
this.directionImageList.Images.SetKeyName(1, "Dir2.png");
this.directionImageList.Images.SetKeyName(2, "Dir3.png");
this.directionImageList.Images.SetKeyName(3, "Dir4.png");
// 
// modeImageList
// 
this.modeImageList.ImageStream = ((System.Windows.Forms.ImageListStreamer)(resources.GetObject("modeImageList.ImageStream")));
this.modeImageList.TransparentColor = System.Drawing.Color.White;
this.modeImageList.Images.SetKeyName(0, "Mode1.png");
this.modeImageList.Images.SetKeyName(1, "Mode2.png");
this.modeImageList.Images.SetKeyName(2, "Mode3.png");

I am using ImageLists so I can use transparency. The Mode images are 100x100 and take up <26KB of disk space each. The Direction images are 208x277 by default and about 75KB on disk. I know png is a compressed format, but even uncompressed in memory I wouldn't expect hundreds of megabytes for these seven pictures.

Is there some inefficiency I'm aware of, and is there an alternate way to dynamically display pictures with transparency?

Conclusion: Something is fishy with the ImageList class. Sometimes it'll lose the alpha channel, and it was causing my program to reserve way more memory than it needed. It also was slowing down the time to draw the main form initially (both while running and in the designer).

Dumping the two ImageLists brought my program down to a much healthier 10MB of RAM. Thanks for all of the suggestions everyone!

like image 451
Troyen Avatar asked Apr 13 '11 23:04

Troyen


People also ask

What happens if virtual memory is too high?

When virtual memory is increased, the empty space reserved for RAM overflow increases. Having enough available space is absolutely necessary for virtual memory and RAM to function properly.

Why is my virtual memory full?

This happens when a person is running a program or multiple programs that are taking up a lot of memory usage and the computer is almost at the maximum of what its memory can handle. You can fix virtual memory problems by manually increasing the size of the virtual memory your computer uses.


1 Answers

My own experience with this problem is that i had 24bit images in my imageList while i had 32bit option set in imagelist's settings.

I've set 24bit in imagelist's properties and the problem has gone. That seems to be a bug which someone should post to MS =)

Sorry for my English.

like image 115
Trudogooglik Avatar answered Oct 19 '22 18:10

Trudogooglik