I have a C # /. NET 4.0 application that on startup shows two windows with about a dozen controls. When I run my program (Debug or Release does not matter), before I do anything, I see in the Task Manager / Resource Monitor that my program already has 450 MB of private memory. I understand that the task manager is not the most reliable way to measure memory usage, but it is one of the most noticeable for my users.
When I run the VS2010.NET memory allocation performance analysis to fully run my program, it reports about 5 MB of RAM actually allocated for managed objects (my program usually uses several unmanaged objects, but they are very small and I disabled them to simplify this investigation. although not a noticeable effect). Similarly, if I call EmptyWorkingSet () from psapi.dll after my main form has been shown, my personal memory drops to ~ 3.5 MB.
I have already addressed memory traces here and here , but these questions seem to deal with programs that appear as a couple of tens of megabytes. My program shows almost 500 MB , which looks much more alarming.
I can’t imagine that all this is due to overhead; why is there such a huge discrepancy between the VS profiler and the task manager?
Refresh . Interestingly, if I comment out the part of InitializeComponent () that sets up the image lists, the number in the task manager remains below 10 MB. I have two sets of PictureBoxes and ImageLists in which the PictureBox displays one of four images depending on which radio button in the radio button group is selected.
These lines of code are the ones that seem to cause an increase in massive memory:
// // 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 use ImageLists, so I can use transparency. Images are 100x100 mode and occupy up to 26 KB of disk space. Direction images are 208x277 by default and about 75KB on disk. I know that png is a compressed format, but even uncompressed in memory, I would not expect hundreds of megabytes for these seven pictures.
Is there some kind of inefficiency that I know of, and is there an alternative way to dynamically display images with transparency?
Conclusion: Something suspicious with the ImageList class. Sometimes it will lose the alpha channel, and this causes my program to reserve more memory than necessary. It also slowed down the time for extracting the main form (both during work and in the designer).
Resetting the two ImageLists led my program to a much healthier 10 MB of RAM. Thanks to everyone for all the suggestions!