Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

windows service memory hog

Status
Not open for further replies.

ralphtrent

Programmer
Jun 2, 2003
958
US
Hi
I am writing a windows service. To do what I have coded it consumes about 30 megs of memory, which seems like a lot for what I am doing. The stange thing is, as soon as I start the service and before i invoke any code, the service takes up almost 8 megs of memory right off the bat.

Does anyone else have issues with Windows Service in 2.0 as far as memory goes? Does anyone have any idea of any tools I can use to track my memory consumption? I have put in code to write an event log message to show how much memory was currently in use but I need a way to minimize the memory.

Here is the general jist of what i am doing.
Code:
Read a DB to see how many rows I need to process
foreach row start a new thread.
each thread reads the db again to get information about the work (row) it has to do
I use the row to read a column that holds an interval for my timer.  Everytime the interval is hit I read the db again for the same info I already had to see what work is involved
I use that info to read another database to process another request when I get done with that, I send an HTML based email to user and go to sleep again.

while this is going on, in my main thread I query the db once every 30 mintues to see if any new work is out there for me to do
the reason i read the db mutliple times for the same info is because I thought i would be wasting memory hold and passing a datarow to multiple functions as opposed to just an int holding the record id.

I hope this makes sense, i would post code, but there is to much and its pretty wide spread.

Any help is apprectiated.

Thanks,
RalphTrent
 
the reason i read the db mutliple times for the same info is because I thought i would be wasting memory hold and passing a datarow to multiple functions as opposed to just an int holding the record id.
this is most likely the issue. hitting a database is one of the most resource intensive processing in programming.

You should only hold the object in scope as long as it is needed. of course that is subjective to the context. you are working with.

I would invest in jetbrain's dotTrace. this will tell you exactly where your bottlenecks are.

Jason Meckley
Programmer
Specialty Bakers, Inc.
 
I tried to carry the datarow with me where needed and the memory was about the same.

here is another update. I create a new service with NO Code at all. On startup it used 8 megs. I found a post on bytes.com where someone had asked if this is normal. It looks like .net empty apps all consume about 8 megs, has anyone else heard this?
 
are you properly disposing of disposable objects? this would be the next place i look. right now though, you are in the dark until you can pinpoint the bottleneck.

Jason Meckley
Programmer
Specialty Bakers, Inc.
 
you might want to try to look at delegate (event) registration. are you doing any of this?
 
Jason, ill DblCheck, but Im 95% sure I am nulling all objects.

David, I am not coding for any delegets, so unless .net is doing that it self, I am not.

Thanks
 
disposing and null a different. this is important with managing database connections.

one final thought that just occurred to me. is the service causing performance problems? Or do you just want to reduce the amount of memory because it's "to high"?

if there is a performance problem then, yes it should be addressed. if this is more of gut feeling, or just "too much" without quantifying the issue, then you're wasting your time.

Jason Meckley
Programmer
Specialty Bakers, Inc.
 
its the memory more so the performance. 95% of the time, this service is just waiting for a timer to elapse. My concern is that the memory doesn't come back to where it was before the timer elapsed.

Another thing I wondered, I am using, i good about of static methods from another class that I created my self to reduce reduntant code (writting to eventlogs, showing message boxes, etc, etc, etc), does using static methods consume more memory then non-static?

Thanks.
 
static object don't consume more memory then if they were not static. However a static object is static and therefore always exists (unless you are managing when the static object is initialized/disposed. (which is a complex task in itself).

so this may explain the memory usage. once the static object is initialized, it will remain in memory until it is destroyed. (when the service is shutdown, if you are not managing this yourself).

very rarely do you need static objects (or singletons for that matter). And if you do need them, I like to have an IoC container control that (Windsor, Spring, Structure.Map, Unity).

Jason Meckley
Programmer
Specialty Bakers, Inc.
 
OK I will remove the code to the static objects and see if that helps, but can you explian what those IoC containers are?

Thanks.
 
I will warn you, this is not an easy concept to grasp. To fully utilize the power of an IoC will require a change in how you code. IoC's primary concern is creating object graphs.
A requires B and C. B requires E and D1. D1 requires...
when you request A the IoC will initialize and inject the cascade of requirements. here are some links


I think in your scenario and IoC would be overkill simply instantiating the objects when needed would be enough.

Jason Meckley
Programmer
Specialty Bakers, Inc.
 
one final thought, not knowing what the static objects do they may not be the issue. example:
Code:
public class Calculator
{
   public int Add(int left, int right)
   {
      return left + right;
   }
}
isn't an issue.
but if the calculator accessing files, connects to a database. keeps a large collection of whatever. then it may be an issue.

i'll go back to the profiler. you need to quantify where the bottleneck is.

Jason Meckley
Programmer
Specialty Bakers, Inc.
 
Start with running FxCop against your code. You don't need to get all the warnings cleared up, but you should be under 20 or so. None of which have anything to do with disposing of objects or memory allocation (you can ignore warnings about naming conventions, etc. They're nice to follow, but not critical to the problem you're trying to solve)

Chip H.



____________________________________________________________________
www.chipholland.com
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top