I had someone ask me the following question on memory behavior with .NET applications on Windows XP.  I provided the explination below and just wanted to make it public so other folks can benefit from it.  FYI - the explination I provide is not limited to .NET applications, it applies to all running processes.

 


Hi,

 

I recall seeing this posting before but I don’t remember if there was an answer.

If it was, please forward the reply to me again. Thanks.

 

Scenario 1.

=========

Create a new VB.Net win project.

Add a button on a form (no code).

Start the application. Taskmgr will show that it takes around 14-16 MB initially.

Continously press the button.

Taskmgr shows that memory increases all the time (slowly and in small chunks, but still ….)

 

Scenario 2.

=========

Create a new VB.Net project.

Start the application. Taskmgr will show that it takes around 14-16 MB initially.

Minimize the application.

Taskmgr now reports only 820 KB (0.82 MB).

Restore the application.

Taskmgr now reports 2.5 MB.

 

Real scenario.

===========================

App starts and Taskmgr shows it takes 40 MB.

After running a while it is up to 120 MB.

Machine has 512 MB.

Minimizing the app just after startup brings memory down to 4 MB.

This app is targeted to run on Citrix which means (I guess) memory problems.

(I don’t have a repro (yet) and I don’t know the details of this app (yet)).

 

How come memory increases (according to Taskmgr) in Scenario 1. and what is the deal with the memory fluctuations in Scenario 2. ?

 

Any thoughts are much welcome.

 

Mikael


Mikael,

 

What you are seeing is expected behavior.  Here's a recap of scenario two:  Your application loads and its memory footprint grows so the OS allocates larger chunks of memory to it because it has memory available.  If you attach perfmon to your application you'll see the working set and the private bytes increase however most likely the growth between the two counters will not be linear (working set will grow faster than private bytes).  Then the app gets minimized....

 

At this point the os flushes all of the unused memory pages (those not doing any work) of your application to virtual memory (the paging file) and most of the working set is released back to the OS for use in other applications because your app isn't doing anything and no longer needs it.  Hence the decrease to .82MB, most likely this is the memory for the message pump for the application and possibly the main window and it's resources.  Then the application is reactivated...

 

The cpu starts running instructions on your process and pages are fetched from the paging file as necessary.  Since the OS has already loaded most of your application and recognizes that your application is not allocating more memory, it (the os) only allocates very small/if any amounts of working set to your app.  That is why it is now at 2.5MB instead of 16MB.

 

 

The best way for you to view this is to use perfmon and watch working set, virtual memory, and private bytes while you do scenario 2 from your mail.  You'll see the behavior I describe above.  In a citrix enviroment you'll see tha the OS is more sparing with the working set as more and more users consume more resources.

 

Rob