Reducing memory usage in a windows service

G

Greg Merideth

I've written a basic windows service to provide some helper xml functions for
my web methods and even thou the service is only about 1k lines long with 1
timer, its mem usage is 10m and its vm mem usage is 14! The same code written
as a program that requires you to click on the menu options to fire off the
events takes up 4/9mb.

I've seen examples where calling SetProcessWorkingSetSize(hWnd, -1, -1); does
the same thing as minimizing a program to reduce the memory use but I can't
minimize a service and calling the setprocess function lowers the memory usage
by 0 bytes.

Are there any other ways to lower the memory usage? Even after some 40+ hours
of running the service was still consuming 10mb of ram. I just dont see how 1
timer and some xml calls with file operations takes up 10mb.
 
N

Nicholas Paldino [.NET/C# MVP]

Greg,

You should expect this. The 10 MB is for the runtime, mainly for the
memory management. This is just the price you pay for it.

Hope this helps.
 
J

Jochen Kalmbach

Greg said:
I've written a basic windows service to provide some helper xml
functions for my web methods and even thou the service is only about
1k lines long with 1 timer, its mem usage is 10m and its vm mem usage
is 14! The same code written as a program that requires you to click
on the menu options to fire off the events takes up 4/9mb.

See: Analyzing Common CLR Performance Problems
http://blogs.msdn.com/akhune/archive/2004/06/11/153734.aspx

In short: The GC always allocated at al lot of memory in advance...
I've seen examples where calling SetProcessWorkingSetSize(hWnd, -1,
-1); does the same thing as minimizing a program to reduce the memory
use but I can't minimize a service and calling the setprocess function
lowers the memory usage by 0 bytes.

With 'SetProcessWorkingSetSize' you do not minimize the memory!!! you only
minimize the working set (and thus leads to more page-faults).

Are there any other ways to lower the memory usage? Even after some
40+ hours of running the service was still consuming 10mb of ram. I
just dont see how 1 timer and some xml calls with file operations
takes up 10mb.

To reduce the memory usage dramatically write your code in unmanaged C++.

I think with using the server-GC you can also reduce some preallocated
memory (but I forgott the link...)

Greetings
Jochen
 
J

Jochen Kalmbach

Hello Patrick,
Has it been compiled as a Release versus the default Debug?

Would this make a difference !?
The Debug version only has the "DebuggableAttribute" set to (true, true).

This only disables the optimizer and does not track some JIT infos...

Greetings
Jochen
 
I

Ignacio Machin \( .NET/ C# MVP \)

Hi,

In my experience I think that around 8-10 is the lower memory consuption of
any .net program, Do you really find that big?
Doom 3 consume around 500 MB :)

Cheers,
 
G

Greg Merideth

Its in release mode but after reading the other messages I'll look into moving
the entire set of helper functions into c++ to lower the memory consumption.

I understand that the memory management is part of it so mabye the .net c# way
isn't a good way to go, even with the ease of it, as a process to create
services. Mabye with visual studio 2010 it might be less memory intensive :)

Now to go mess up my machine with c++!
 
W

Willy Denoyette [MVP]

Reducing the working set by calling SetProcessWorkingSetSize, only works for
UI style applications and is considered bad practice.
Not sure what you mean with "set of helper functions into c++ ", but if you
mean by that that you are reimplementing part of your service in C++ and
keep another part managed, then this won't reduce the working set.
Also I'm not clear on what memory counter you are looking at? If it's the
"working set" you should know that a large amount of this one consists of
shared/shareable pages.


Willy.
 
E

Etienne Boucher

..NET programs don't use all the memory they allocate. If you fill up your
ram, the memory management of .NET applications will change to a more a more
agressive mode. For exemple I have a "small" winform application that takes
20Mb at launch. If I fill up my ram (by launching 3 virtual machines, damn
1Gb is hard to fill up!) it's memory usage goes down to 2Mb.

Of course, going C++ is going to save you even more than that.

Etienne Boucher
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top