Re: Memory management/leak?

From: Tom Hall (nospam_at_harmonyit.com)
Date: 04/14/04


Date: Tue, 13 Apr 2004 21:25:20 -0400

I even took a look at the decompiled code for Random and DateTime (using
Lutz Roeder's .NET Reflector).
Nothing in there that would hang onto objects. Random does create a seed
array of 56 elements - then its just a bunch of math and array operations to
determine next pseudo-random number.

Also, the empty constructor Random() calls Environment.TickCount internally
(which is Native code). I suspect this might be a bit faster and create
less garbage than you calling DateTime.Now.Millisecond as DateTime.Now
creates a new DateTime object whereas Environment.TickCount is static - so
only one object. Add that to Jon's comments and you should be able to get
the garbage creation done to zero!

Of course the garbage collector won't necessarily kick in unless you
generate a worthwhile amount of garbage (in its opinion).

If you get all the memory back when you call Collect yourself then I don't
think there's anything you can do about it.

Of course the other thing you can do is run your code in a memory profiler.
I personally can recommend SciTech www.scitech.se/memprofiler - I've bought
it and used it to find several annoying memory consuming bugs in my code -
often in places you don't suspect as hanging onto objects - like updating a
GUI.

Hope this helps
Tom

"Jon Skeet [C# MVP]" <skeet@pobox.com> wrote in message
news:MPG.1ae5d45aa365337d98a67c@msnews.microsoft.com...
> James Sadlier <no.spam@spam.me.no.spam> wrote:
>
> <snip>
>
> > Any thoughts?
>
> Apart from anything else, it's not a good idea to use Random like that.
> Create one instance, and then use that everywhere. You might want to
> use one instance per thread, admittedly - though I'd just put some
> locking around each call.
>
> Not sure why it should guzzle up memory though - I can't reproduce your
> results on a small test basis. Do you have a short but complete program
> which demonstrates the problem? See
> http://www.pobox.com/~skeet/csharp/complete.html for what I mean.
>
> --
> Jon Skeet - <skeet@pobox.com>
> http://www.pobox.com/~skeet
> If replying to the group, please do not mail me too



Relevant Pages

  • Re: Tracking down a garbage collection problem
    ... but I don't know what methods are available to the Ruby ... megabytes of memory; if I process many files in a single run then ... I would expect garbage collection to kick in along the way but it ... practice of software engineering, and what separates software ...
    (comp.lang.ruby)
  • Re: 386sx/25mhz compatibility
    ... I'd need to add hard drives also, ... a better computer out of the garbage. ... Pentium, mid-2001, suddenly better computers started appearing. ... later, I got a 50MHz 486 with 16megs of memory, for ten dollars. ...
    (comp.os.linux.hardware)
  • Re: FileStream.Close() & GarbageCollection - Memory Leak Question
    ... I considered the memory a "resource" that would be freed but I have no ... The only resource that's truly "managed" is memory in the garbage ... managed objects can still "own" unmanaged resources (pointers ... You could think of the handle like a dry cleaning ticket. ...
    (microsoft.public.dotnet.languages.csharp)
  • Re: Larry Wall, on Tcl
    ... Sure, if each chunk of memory allocated was only freed in one place, it wouldn't be too painful. ... It keeps the bug from being noticed unless the subsystem using slow physical memory to back the virtual memory is overwhelmed. ... Both of those are prevented by garbage collection. ... What do you do to explicitly free up registers in your C code when you're done with them? ...
    (comp.lang.tcl)
  • Re: is value type really cheap?
    ... > DataTime is a structure therefore a value type. ... There's still the memory allocated for the reference though - that will ... > now double DateTime instances are allocated and but I need none in this case. ... Now suppose DateTime were a reference type and you *did* need it. ...
    (microsoft.public.dotnet.languages.csharp)