473,791 Members | 3,251 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

keeping memory resources cleaned up within timer loop of Windows Service

given
namespace WindowsService1
{
public class Service1 : System.ServiceP rocess.ServiceB ase.........

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

myAL = objCont.CreateA rrayListofObjec ts()

for (int i = 0; i <= myAl.Count; i++){
objInfo = objCont.Hydrate Info_Object();
objCont.DoSomet hing(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,arrayli sts,strings,int egers 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 2817
hazz <hazz@sonic_net > wrote:
given
namespace WindowsService1
{
public class Service1 : System.ServiceP rocess.ServiceB ase.........

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

myAL = objCont.CreateA rrayListofObjec ts()

for (int i = 0; i <= myAl.Count; i++){
objInfo = objCont.Hydrate Info_Object();
objCont.DoSomet hing(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,arrayli sts,strings,int egers 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.co m>
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.co m>
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.co m> wrote in message
news:MP******** *************** *@msnews.micros oft.com...
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.co m>
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
4410
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 process. Is it correct?
8
16006
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 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...
2
13219
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 System.Timers.Timer but i had to remove it because of known bug(842739). Now i am using System.Threading.Timer. It executes routine fine but the delay time is sporadic, sometimes it executes routine after 5 minutes, sometimes 10 minuete, 13...
8
7602
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 wee hours of the morning). From the time the service starts to the first time it hits the database its memory consumption is about 6.9MB which is a figure I can live with. However, after it hits the database for the first time its memory jumps to...
7
4037
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 creates a bunch of PDFs with Crystal Reports. Another service is a remote object which serves as our data access component -- basically it just executes stored procedures and returns datasets. If you watch the services in task manager, you can...
7
1698
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 finding a job, the System.Timers.ElapsedEventHandler creates a new thread to execute the job. The job is pretty basic, it uses and xml document and applies it to an XSLT document using the XSLTransform class.
7
4804
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 System.Net Imports System.Net.Sockets Imports System.Net.Dns Imports System.Text Imports System.IO
5
2686
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 notice the memory leak. The memory usage in the TaskManager for the process can be seen climbing and climbing, MBs at a time. When using MemProfiler, I noticed the Win32 Heap is what seems to be growing. I've been searching on the internet...
17
7971
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 while, the application is using more than 100 or 150 Mb in RAM, and if I let it continue, it can leave the system without memory. I've been watching in some pages that other people has the same problem with this control when keep navigating for a...
0
9517
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 effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
10428
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, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
10207
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
10156
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
9997
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 protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
9030
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
7537
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 presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
5435
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
1
4110
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system

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.