Memory usage rises constantly.

rh37hd

Member
Joined
Apr 15, 2011
Messages
6
Programming Experience
Beginner
Hello,

Here is my scenario. I made a service that has a timer with an interval of 500ms.

When watching the task manager - I notice that every second or so my program uses 4 more kb of memory. I've even commented out all of the code in the timer - the memory usage still goes up (much slower though).

Obviously as a service this runs all day - after about a day the program is taking up several hundreds of MBs and continues to rise - it never hits a peak (starts off at about 30 MBs).

I'm disposing every object that has the option. I've read that on windows form applications - memory usage doesn't go down until the form is minimized. I copied my service code to a timer on a windows form and the memory would rise - until I minimized it and then the memory would go back down.

Basically - how can I get the memory usage down? It's almost like I need to minimize/maximize my application - but how can I do that if it is a service?

gc.Collect does nothing - (I've read that it releases the memory to .net, not windows). Is there a way to make my application release it's memory to windows?

How can I make my application release it's memory back to windows without restarting the service?

Thanks!
 
.Net managed memory is divided into three generations, where GC only collects when each generation is full, so seeing what appears to be continuous memory increase is expected, especially if 'nothing' happens. Doing a GC.Collect (which you shouldn't) also may only mean objects are transferred to another generation, perhaps to be released later. Forcing objects generation upgrade also means it takes longer time for them to be released than it naturally would. As jmcilhinney said, disposing and dereference objects that is no longer needed is all you should normally do for the automatic memory management to perform at best.
 
Ok - so 15 hours ago my program was at 15MB in the task manager. Now it's at 90MB.

Are you saying when Windows runs out of memory, then my program's memory will go down, and never before that? Is this normal .Net operation?

This looks bad for end users as they only see the memory increasing (it never has gone down in the 15 hour span - it only increased).
 
If you have reason to believe there is something wrong with your code you should review it with this in mind, perhaps with help of a memory profiler, there is only two things you need to look for as explained.
"how it looks" in Task Manager is not really relevant.
 
So your saying it can be perfectly normal for an application to grow to use all of the available memory?

I guess the only way to find out would be to let it run on a computer until it uses 100% of the memory and see what happens?
 
So your saying it can be perfectly normal for an application to grow to use all of the available memory?
What makes you think your application use all available memory? Do you know you are keeping lots of really large objects in memory?

About Task Manager, I've looked there also a few times, and for apps that I know use a really great deal of memory I've seen numbers like 100-200mb, but that has never bothered me because I know when objects are released and used memory turns into allocated. 200mb is also a rather small number compared to available, especially is temporary context.
 
This is a simple application that monitors the print queue. Every object is released right away - why would it continue to grow? I'm not storing anything at all.

This leads me back to my initial question - I wish I could do a Me.WindowState=FormWindowState.Minimized from a service - is there a different command that would at least show the correct memory in the task manager?
 
I think you should forget Task Manager and concentrate on development.
 
What Task Manager is displaying is not wrong. That's the amount of memory allocated to your program so that's what Task Manager displays. .NET has been built to work that way. There have been hundreds, if not thousands, of man hours spent designing the .NET memory management subsystem. Do you think that over the nearly 10 years that .NET has been around that every program has been gobbling memory and noone has noticed? What you're seeing is absolutely normal and exactly how .NET applications are designed to work. Either do some reading on .NET memory management and get a proper understanding of how it works or just accept that it works and forget about it. As it is, you're wasting your time trying to solve a problem that doesn't exist.
 
Back
Top