Re: Memory management/leak?

From: Roy Fine (rlfine_at_twt.obfuscate.net)
Date: 04/14/04


Date: Tue, 13 Apr 2004 23:20:17 -0400

James,

Does the 8K per loop increase over a long period of time? You may expect to
see the private bytes increase up to some limit before GC kicks in - and
since you have declared that you don't want to control the when for GC, you
pretty much have to llive with the default behavior.

If the privates bytes for your service goes well beyond the value
corresponding to 2/3 of the physical memory on the machine, then you have a
problem, else thats just .Net doing to work for you.

As for the following line of code:
> myRandomObject = null;

Take it out - it does nothing good, and only serves as noise. If it is a
long time between iterations of the loop, then the 8K is nothing to worry
about. If it is a short time, then the next iteration the original object
no longer has a reference - which is exactly what you are attempting with
the null assignment.

Creating the Random object in the thread and using it once is bad - you are
not getting a stream of random numbers, you are just getting the first value
from the stream every time (albeit based on a new seed value). These pseudo
random algorithms are scrutinized rather closely for randomness over a
rather large set for an arbitrary seed. I know of no works that has looked
at the distribution of 'n' sets of size 1 with a non-random seed (the
milliseconds is an *ok* seed, but it is not random over the range)

regards
roy fine

"James Sadlier" <no.spam@spam.me.no.spam> wrote in message
news:uZyWDMUIEHA.3240@TK2MSFTNGP12.phx.gbl...
> I'm writing a windows service, that does some stuff every second in a
loop,
> generating random numbers. It works fine apart from the fact that for some
> reason it's using an extra 8K of memory every loop. I've isolated it down
to
> the use of the Random object. When I remove the use of this object my
> service's memory usage stays static.
>
> Any idea what's going wrong? Random doesn't implement IDisposable, so I
> can't call Finalize() or Dispose() on it. Nullifying the object doesn't
seem
> to be cleaning it up, and I don't want to go calling GC.Collect() all the
> time.
>
> The code basically looks like this:
>
> while(true)
> {
> DoStuff();
> Thread.Sleep(1000);
> }
>
> ...
>
> private void DoStuff()
> {
> //if I remove the next two lines the problem goes away
> Random myRandomObject = new Random(DateTime.Now.Millisecond);
> myRandomObject = null;
>
> /* some other work*/
> }
>
>
> Any thoughts?
>
>



Relevant Pages

  • Re: How can I pause and resume a loop with a button press?
    ... but it doesn't loop, ... Private rs As DAO.Recordset ... Private Sub cmdResume_Click ... Do While LoopCount < 10000 And LoopContinue ...
    (microsoft.public.access.formscoding)
  • Re: Thread.Abort()
    ... private _myThread ... private _continueEvent System.Threading.ManualResetEvent ... private enumerated type _state (pause, stop, start, resume) ... end loop ...
    (microsoft.public.dotnet.languages.csharp)
  • Re: WebBrowser (loop automation)
    ... You have to set up an event driven "loop" if you will. ... you'll have class level private variables that hold ... tasks on the webpage. ... Uri myUri = new Uri; ...
    (microsoft.public.dotnet.framework.windowsforms)
  • Re: File List Box Question
    ... Here's a way to get the number of selected items without the loop. ... Private Sub Command1_Click ...
    (microsoft.public.vb.general.discussion)
  • Re: Fast string operations
    ... > is why people use unsafe code now and then to use pointer arithmetic to ... > loop over arrays without all the unnecessary bounds checking. ... don't return the trimmed string". ... The customer perceives this as a memory leak. ...
    (microsoft.public.dotnet.languages.csharp)

Loading