Why does my Winforms program reserve so much virtual memory?

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!

+8
c # memory
source share
5 answers

png already has transparency. Just make white transparent and save the image. Then use them as usual.

0
source share

My own experience with this problem is that I had a 24-bit image in my imageList, while I had a 32-bit option set in the imagelist settings.

I set 24bit in the imagelist properties and the problem disappeared. This seems to be a mistake someone should send to MS =)

Sorry for my English.

+2
source share

The .Net infrastructure was designed to work as quickly as possible, taking into account available resources. The application will continue to consume more and more memory because it is requested (and available) only by letting go when you specifically call the garbage collector or when another application needs the resources that it clogs.

Minimize the application and you should see a better idea of ​​how much memory your application is using.

if you return to using it, it will remain in the lower state of the resource until it is used and consumed again. collapse it again to find out how much is actually (not) used again. it is built into the memory management system .net frameworks.

+1
source share

Someone explained several reasons why the memory jumped from 34 MB to 145 MB: Finding the true amount of memory in a Windows application

+1
source share

First off, have you tried Debug Diag ? It will analyze the dump of your process and give an excellent graphic memory that will help you understand who allocated all this memory.

In addition, make sure that neither your compiled .exe, nor any of your plug-in / download builds are very large - it is possible that all ~ 500 MB are just loaded DLLs. This can happen if (for example) large resources are built into the assembly.

0
source share

All Articles