Click here to Skip to main content
Licence CPOL
First Posted 27 Sep 2008
Views 7,426
Downloads 72
Bookmarked 12 times

Simple Memory Cleaner that forces GC to collect objects

By onxonx | 27 Sep 2008 | Unedited contribution
In a separate thread there is a loop to invoke GC.Collect()

1
1 vote, 50.0%
2

3
1 vote, 50.0%
4

5
2.67/5 - 2 votes
μ 2.67, σa 2.47 [?]

Introduction

Pretty often postponed destroying of objects in .NET makes tests harder to read as you are not sure when objects are ready to be removed. Let me stress that this is not for PRODUCTION product of course. It's a really bad idea to force GC job, especially so often. But as an example - while playing with .NET Remoting objects, you might be interested to know when those are ready to be collected. This sample MemoryCleaner class runs in a separate thread and forces GC to remove object periodicaly.  

Using the code 

Here is code of MemoryCleaner class. Full solution with test is available in download. It was written with VS2008 but it would work for any version:

    public class MemoryCleaner
    {
        private const int PERIOD_IN_MS = 500;

        private static int Counter_;

        private Thread thread_;
        private AutoResetEvent event_ = new AutoResetEvent(false);

        public MemoryCleaner()
        {
        }

        public void Start()
        {
            Stop();
            thread_ = new Thread(new ThreadStart(run));
            thread_.Name = string.Format("MemoryCleaner#{0}", Interlocked.Increment(ref Counter_));
            thread_.IsBackground = true; // this makes thread to be stopped when Main thread is over
            event_.Reset();
            thread_.Start();
        }

        public void Stop()
        {
            if (thread_ != null)
            {
                event_.Set();
                thread_.Join();
                thread_ = null;
            }
        }

        private void run()
        {
            while (!event_.WaitOne(PERIOD_IN_MS, false))
            {
                GC.Collect();
                GC.WaitForPendingFinalizers();
            }
        }
    }		 

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

About the Author

onxonx

Software Developer (Senior)

United States United States

Member


Sign Up to vote   Poor Excellent
Add a reason or comment to your vote: x
Votes of 3 or less require a comment

Comments and Discussions

 
You must Sign In to use this message board. (secure sign-in)
 
Search this forum  
 FAQ
    Noise  Layout  Per page   
  Refresh
GeneralThanks !!! PinmemberRoman Lerman22:19 9 Aug '09  

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.

Permalink | Advertise | Privacy | Mobile
Web01 | 2.5.120210.1 | Last Updated 27 Sep 2008
Article Copyright 2008 by onxonx
Everything else Copyright © CodeProject, 1999-2012
Terms of Use
Layout: fixed | fluid