free memory with C#  
Author Message
setareh





PostPosted: Visual C# General, free memory with C# Top

hi

my operatin system is windows server 2003.

I have program that use big text files , when i run that my computer Page File Usage is being the highest . If i close my application , Page File Usage won't decrese how can i free memory



Visual C#16  
 
 
littleguru





PostPosted: Visual C# General, free memory with C# Top

You could call GC.Collect(); to ask the Garbage collector to do a collection. But you can't really free any memory in C#. It's up to the runtime to do the work for you. But the runtime can only free objects who have no reference at them. You should also check if you are referencing objects, that aren't used anymore. For example you hold them in lists etc without using them.

 
 
Formerly Known As Eam





PostPosted: Visual C# General, free memory with C# Top

This is expected behaviour. I wouldn't lose too much sleep over it if I were you. Garbage collection won't help, as the memory is marked as "free" after the program exits anyways.

If you really must, you can adjust the maximum page file size in the "System" control panel icon.

 
 
SvenC





PostPosted: Visual C# General, free memory with C# Top

When your app needs more memory than you physically have some other memory will get swapped out into the page file. That is your first increase in page file usage. After you closed your app that memory is available again and other processes will get their memory pages swapped back into physical memory. That should be why you see page file usage a while after your app was closed.

Also, some memory is used to cache disk data so it can be used faster when accessed again. The cache manager will also need some time to decide that parts from your large text file are not worth caching any more. I am not aware if you can control the amount or time taken for the cache.

In the end I guess you just have to continue using the system a while after your app was closed to see a decrease in page file usage - assuming that the remaining apps do not need more memory than physically available.

--
SvenC


 
 
littleguru





PostPosted: Visual C# General, free memory with C# Top

It's true that .NET doesn't release fast memory. That's because allocating new memory from OS takes some time, but it's releasing memory if seen that it isn't used anymore (for a time). The GC is quite an advanced piece of code that does different checks to understand if to completely free (give back to the OS) memory. It's not normal that an application (also one in .NET) takes more and more memory for no obvious reason. It's also possible to create memory leaks in .NET by holding objects in static lists etc. I would therefore suggest to do a check to see if somewhere data is hold and a reference is still pointing at it after there is no real need for it.

 
 
Ting.Choo.Chiaw





PostPosted: Visual C# General, free memory with C# Top

http://forums.microsoft.com/MSDN/ShowPost.aspx PostID=982721&SiteID=1
 
 
SvenC





PostPosted: Visual C# General, free memory with C# Top

Hmm - all that code is doing is to page out as many pages as it can. That will also page out pages still needed by other apps. So in general that will make system performance worse.

I agree that you might see more free memory in the task manager because cache memory is freed also. But the price has to be payed by other apps which get a slowdown because their pages need to be swapped back in.

But if that optimization "feels" good to the user, well that's what a good placebo is all about

--
SvenC


 
 
Ting.Choo.Chiaw





PostPosted: Visual C# General, free memory with C# Top

GC will wait for its interval to free resources. Therefore you could try this

see how i free resource without exiting the application (coding with red color)

http://forums.microsoft.com/MSDN/ShowPost.aspx PostID=982721&SiteID=1&mode=1


 
 
Ting.Choo.Chiaw





PostPosted: Visual C# General, free memory with C# Top

The best thing to do is to do in C++ way / asp way:

object = null;

that's the only way u free resources in .NET without exit ur application. See the link i provided somewhere in this page.