Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsFree MagazinesWhite PapersSubmit Content
Discussion GroupsASP.NETWindows FormsLanguages.NET FrameworkVisual Studio.NET
Articles.NET FrameworkASP.NETToolsWindows Forms
.NET DirectoryOpen Source ProjectsUser GroupsWeb Resources
Related Topics
Visual Basic 6SQL ServerMS AccessOther DB ProductsMS Server ProductsMore Topics ...

.NET Forum / .NET Framework / CLR / July 2004

Tip: Looking for answers? Try searching our database.

Memory not being released in .net service

Thread view: 
Enable EMail Alerts  Start New Thread
Thread rating: 
sdc - 16 Jul 2004 20:43 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 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/


Rate this thread:







Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.