I have a .Net windows service that has 4 threads. Each thread handles large amount of string data. The threads are out to sleep and awaken after 1 minute intervals.
From the start of the service, the memory consumption keeps increasing to a point, where 'out of memory' exception is generated and as such the service has to be restarted.
Any ideas on how the memory can be reclaimed are highly appreciated.
Jon Skeet [C# MVP] - 16 Jul 2004 20:52 GMT
> I have a .Net windows service that has 4 threads. Each thread handles
> large amount of string data. The threads are out to sleep and awaken
[quoted text clipped - 5 lines]
>
> Any ideas on how the memory can be reclaimed are highly appreciated.
Could you post a short but complete program which demonstrates the
problem?
See http://www.pobox.com/~skeet/csharp/complete.html for details of
what I mean by that.

Signature
Jon Skeet - <skeet@pobox.com>
http://www.pobox.com/~skeet
If replying to the group, please do not mail me too
Richard Grimes [MVP] - 26 Jul 2004 22:10 GMT
> I have a .Net windows service that has 4 threads. Each thread handles
> large amount of string data. The threads are out to sleep and awaken
[quoted text clipped - 5 lines]
>
> Any ideas on how the memory can be reclaimed are highly appreciated.
Do you know that some of the string variabkles can be released? If so, make
sure that after you have used them assign the reference to null. This way
there will not be any reachable reference to the string object and hence the
GC can reclaim the data:
class Test
{
string str;
public void Initialize()
{
StringBuilder sb = new StringBuilder();
/*create a huge string*/
for (int idx = 0; idx < 100000; ++idx) sb.Append(idx.ToString());
str = sb.ToString();
/* oops a reference to a huge string is reachable through this object
*/
}
public void use_string()
{
/*use the string here*/
/*finished with the string? then release the reference*/
str = null; /*string is now unreachable, so it can be collected*/
}
}
Note that this code is ugly and you should design your code to obtain
resources as late as possible and release them as soon as possible, so
really you should not have two methods like Initialize and use_string, but
instead have a combined method that creates, uses, and releases the string.
Richard

Signature
my email evpuneqt@zicf.bet is encrypted with ROT13 (www.rot13.org)
sign up for my free .NET newsletter at
http://www.wd-mag.com/newsletters/