473,473 Members | 2,136 Online
Bytes | Software Development & Data Engineering Community
Create Post

Home Posts Topics Members FAQ

keeping memory resources cleaned up within timer loop of Windows Service

given
namespace WindowsService1
{
public class Service1 : System.ServiceProcess.ServiceBase.........

private void timer1_Elapsed(object sender, System.Timers.ElapsedEventArgs e)
{
ArrayList myAL = new ArrayList();
CustomClass objCont = new ControllerClass;
CustomClass objInfo = new InfoClass;

myAL = objCont.CreateArrayListofObjects()

for (int i = 0; i <= myAl.Count; i++){
objInfo = objCont.HydrateInfo_Object();
objCont.DoSomething(objInfo)
}
}
how do I think about this service from a memory allocation/deallocation
perspective? I would like to know just by looking at code that any
objects,arraylists,strings,integers that are created, are going to be
deallocated at the end of the timer process.

how do i think in the simplest way, about where and when I declare and
instantiate reference types so that I know they will be cleaned up.

If my main processing loop in my timer event which will loop n times every n
seconds is also instantiating other objects, each of which has its own
constructors and object declarations corresponding to typical application
needs, how do I think about the allocation/deallocation of those memory
resources? I want to make sure there are no memory orphans that I have
created when I simply set

Is there an article that would contain a fairly simple explanation of this?
After attempting to instrument my app into Perfmon, CLRProfiler and then
creating a trace class which logs memory info before and after each
function, I found this to be too complicated and reactive. If I knew exactly
what was going on just by looking at my initialization code, I wouldn't need
the instrumentation tools.

I know the same rules apply here as any application but given here that even
8 bytes that are not deallocated will be stolen from the available memory
people every time the timer fires. Within each firing of the timer is a loop
that could conceivably instantiate obects which themselves may not
deallocate their member variables. I don't want a memory leak, even a
trickle. ;-)

Thank you, -Greg
Nov 17 '05 #1
4 2793
hazz <hazz@sonic_net> wrote:
given
namespace WindowsService1
{
public class Service1 : System.ServiceProcess.ServiceBase.........

private void timer1_Elapsed(object sender, System.Timers.ElapsedEventArgs e)
{
ArrayList myAL = new ArrayList();
CustomClass objCont = new ControllerClass;
CustomClass objInfo = new InfoClass;

myAL = objCont.CreateArrayListofObjects()

for (int i = 0; i <= myAl.Count; i++){
objInfo = objCont.HydrateInfo_Object();
objCont.DoSomething(objInfo)
}
}
how do I think about this service from a memory allocation/deallocation
perspective? I would like to know just by looking at code that any
objects,arraylists,strings,integers that are created, are going to be
deallocated at the end of the timer process.
They won't be. They'll be deallocated at some stage after that.
how do i think in the simplest way, about where and when I declare and
instantiate reference types so that I know they will be cleaned up.
If you're only using local variables and not storing the values
anywhere else which might cause them to hang around, they'll all be
*eligible* for garbage collection at the end of the method (and some of
them may be eligible earlier). That doesn't mean they'll be garbage
collected immediately, however.
If my main processing loop in my timer event which will loop n times every n
seconds is also instantiating other objects, each of which has its own
constructors and object declarations corresponding to typical application
needs, how do I think about the allocation/deallocation of those memory
resources? I want to make sure there are no memory orphans that I have
created when I simply set


Basically, think about what could get at the objects you've created. If
nothing can get to them any more, they're eligible for garbage
collection.

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet
If replying to the group, please do not mail me too
Nov 17 '05 #2
> Basically, think about what could get at the objects you've created. If
nothing can get to them any more, they're eligible for garbage
collection.


Thank you for the reply Jon. I think what you said here was very key but I
am trying to understand better, with respect to looking at my code, is how
do I know if nothing can get to them anymore? Conversely, what I have to do
now is look for the objects for which something can get to them. And I am
still trying to get my arms around that. If I instantiate an object in a
loop, how does this fit into that test?

One specific question I had about my service is this. Presumably anything
that gets instantiated in a constructor is elgible for GC. Where is my
constructor in a service. I go right into my elapsed_time event with my main
loop which is creating objects, hydrating them, assigning values to string
and integer variables which are defined within that timer's elapsed event.

thanks again, -greg
Nov 17 '05 #3
hazz <hazz@sonic_net> wrote:
Basically, think about what could get at the objects you've created. If
nothing can get to them any more, they're eligible for garbage
collection.
Thank you for the reply Jon. I think what you said here was very key but I
am trying to understand better, with respect to looking at my code, is how
do I know if nothing can get to them anymore?


Well, something can only get at an object if you've told it where it
is. Very few classes will store static references to objects you tell
them about, and hopefully they should make it very clear when they're
going to do so! So, work out which objects you've told about which
objects (eg which lists you've added references to) and make sure that
either those objects are eligible for garbage collection at the same
time, or that you've removed the references to the objects which you
*want* to be eligible.
Conversely, what I have to do
now is look for the objects for which something can get to them. And I am
still trying to get my arms around that. If I instantiate an object in a
loop, how does this fit into that test?
That depends on what you do with the object. If you add it to a list
for example, then it'll live at least as long as the list does, unless
you later remove it. If you just use the value of some property of the
object which doesn't know its "parent", then the object will be
eligible for garbage collection when the loop goes into its next
iteration.
One specific question I had about my service is this. Presumably anything
that gets instantiated in a constructor is elgible for GC. Where is my
constructor in a service. I go right into my elapsed_time event with my main
loop which is creating objects, hydrating them, assigning values to string
and integer variables which are defined within that timer's elapsed
event.


I can't remember much about how the services infrastructure works
(every so often I work it out but then forget it again), but I assume
that either you've got some code which *does* call a constructor, you
the parameterless constructor for your service is called automatically
by the services infrastructure.

It's not that "anything that gets instantiated in a constructor is
eligible for GC" - it's "anything which is no longer referenced"
wherever it's been instantiated.

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet
If replying to the group, please do not mail me too
Nov 17 '05 #4
Thank you so much for taking the time to explain it Jon. I know this is
basic stuff but it has been subtle. I am re-reading your explanation because
there are some key concepts I keep hearing, like objects whose references
have been removed.
Appreciatively,
-Greg

"Jon Skeet [C# MVP]" <sk***@pobox.com> wrote in message
news:MP************************@msnews.microsoft.c om...
hazz <hazz@sonic_net> wrote:
> Basically, think about what could get at the objects you've created. If
> nothing can get to them any more, they're eligible for garbage
> collection.


Thank you for the reply Jon. I think what you said here was very key but
I
am trying to understand better, with respect to looking at my code, is
how
do I know if nothing can get to them anymore?


Well, something can only get at an object if you've told it where it
is. Very few classes will store static references to objects you tell
them about, and hopefully they should make it very clear when they're
going to do so! So, work out which objects you've told about which
objects (eg which lists you've added references to) and make sure that
either those objects are eligible for garbage collection at the same
time, or that you've removed the references to the objects which you
*want* to be eligible.
Conversely, what I have to do
now is look for the objects for which something can get to them. And I am
still trying to get my arms around that. If I instantiate an object in a
loop, how does this fit into that test?


That depends on what you do with the object. If you add it to a list
for example, then it'll live at least as long as the list does, unless
you later remove it. If you just use the value of some property of the
object which doesn't know its "parent", then the object will be
eligible for garbage collection when the loop goes into its next
iteration.
One specific question I had about my service is this. Presumably anything
that gets instantiated in a constructor is elgible for GC. Where is my
constructor in a service. I go right into my elapsed_time event with my
main
loop which is creating objects, hydrating them, assigning values to
string
and integer variables which are defined within that timer's elapsed
event.


I can't remember much about how the services infrastructure works
(every so often I work it out but then forget it again), but I assume
that either you've got some code which *does* call a constructor, you
the parameterless constructor for your service is called automatically
by the services infrastructure.

It's not that "anything that gets instantiated in a constructor is
eligible for GC" - it's "anything which is no longer referenced"
wherever it's been instantiated.

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet
If replying to the group, please do not mail me too

Nov 17 '05 #5

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

29
by: keredil | last post by:
Hi, Will the memory allocated by malloc get released when program exits? I guess it will since when the program exits, the OS will free all the memory (global, stack, heap) used by this...
8
by: Greg Merideth | last post by:
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...
2
by: linesh.gajera | last post by:
Hi Guys, I am creating a Windows service that call a routine at given interval. Once routine is complete, windows service should wait for 5 minutes and then call the routine again. I was using...
8
by: nautonnier | last post by:
I know my problem has been discussed ad nauseum but I still don't get it so please bear with me. I have written a service which performs some work against a database once a day (usually in the...
7
by: HeatherS | last post by:
We are having issues with our windows services using memory and never releasing it. We have one service that has a file watcher which takes an xml file, inserts some records into a database, and...
7
by: Malcolm Klotz | last post by:
Hello, I have created a Windows Service application in VB.Net 2005. The service is pretty basic, it uses the System.Timers.Timer class to poll a database that checks for jobs to perform. Upon...
7
by: Morten Snedker | last post by:
Others had the same problem and I understand that I have to use the System.Timers.Timer instead for the one for forms. So that's what I do, but still it doesn't trigger: '--code begin Imports...
5
by: RobbGMelenyk | last post by:
I've got a Windows Service written in C# that is having some unfortunate memory issues. I've been working with .NET MemProfiler and AllocationProfiler. But you don't have to use those programs to...
17
by: Cesar | last post by:
Hello people. I'm having a Winform app that contains a webbrowser control that keeps navigating from one page to another permanentrly to make some tests. The problem I'm having is that after a...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
1
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...
0
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...
0
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
0
muto222
php
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.