473,385 Members | 1,409 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,385 software developers and data experts.

Memory management/leak?

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?
Nov 16 '05 #1
3 1852
James Sadlier <no*****@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 - <sk***@pobox.com>
http://www.pobox.com/~skeet
If replying to the group, please do not mail me too
Nov 16 '05 #2
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]" <sk***@pobox.com> wrote in message
news:MP************************@msnews.microsoft.c om...
James Sadlier <no*****@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 - <sk***@pobox.com>
http://www.pobox.com/~skeet
If replying to the group, please do not mail me too

Nov 16 '05 #3
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.me.no.spam> wrote in message
news:uZ**************@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?

Nov 16 '05 #4

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

Similar topics

3
by: beattie.stuart | last post by:
I think I've found a memory leak trying to use the system.management.ManagementObject, but it could be my programming skills so I'd appreciate some advice. I've writing a monitoring routine that...
8
by: Adrian | last post by:
Hi I have a JS program that runs localy (under IE6 only) on a PC but it has a memory leak (probably the known MS one!) What applications are there that I could use to look at the memory usage of...
7
by: Salvador | last post by:
Hi, I am using WMI to gather information about different computers (using win2K and win 2K3), checking common classes and also WMI load balance. My application runs every 1 minute and reports...
18
by: diffuser78 | last post by:
I have a python code which is running on a huge data set. After starting the program the computer becomes unstable and gets very diffucult to even open konsole to kill that process. What I am...
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...
3
by: Jim Land | last post by:
Jack Slocum claims here http://www.jackslocum.com/yui/2006/10/02/3-easy-steps-to-avoid-javascript- memory-leaks/ that "almost every site you visit that uses JavaScript is leaking memory". ...
1
by: dh | last post by:
Will GC.GetTotalMemory(true) reliably tell if a console app could have a memory leak, given the nature of memory management of GC in .NET? Like calling it two times, each at the beginning and right...
9
by: benoit808 | last post by:
I don't have a lot of experience with C++ so I apologize if this is a stupid question. I use Paul Nettle's memory manager (mmgr.cpp) which reports a memory leak but I don't think there's one. Here...
1
by: Joe Peterson | last post by:
I've been doing a lot of searching on the topic of one of Python's more disturbing issues (at least to me): the fact that if a __del__ finalizer is defined and a cyclic (circular) reference is...
9
by: jeungster | last post by:
Hello, I'm trying to track down a memory issue with a C++ application that I'm working on: In a nutshell, the resident memory usage of my program continues to grow as the program runs. It...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 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 former...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...

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.