Click here to Skip to main content
13,042,630 members (82,541 online)
Click here to Skip to main content


26 bookmarked
Posted 31 Oct 2010

GC Algorithm and Story of WeakReference

, 31 Oct 2010
WeakReference comes very handy when working with Large objects. In this application, I have demonstrated how you could easily use this class to improve the performance of your application.
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace WeakReferenceTestSample
    public class WeakReferenceUsage
        WeakReference weakref = null;

        private SomeBigClass _somebigobject = null;

        public SomeBigClass SomeBigObject
                SomeBigClass sbo = null;
                if (weakref == null)  //When it is first time or object weakref is collected.
                    sbo = new SomeBigClass();
                    this.weakref = new WeakReference(sbo);
                    this.OnCallBack("Object created for first time");
                else if (weakref.Target == null) // when target object is collected
                    sbo = new SomeBigClass();
                    weakref.Target = sbo;
                    this.OnCallBack("Object is collected by GC, so new object is created");
                else // when target object is not collected.
                    sbo = weakref.Target as SomeBigClass;
                    this.OnCallBack("Object is not yet collected, so reusing the old object");

                this._somebigobject = sbo; //gets you a strong reference
                return this._somebigobject;
                this._somebigobject = null;

        # region GarbageEvent

        public event Action<string> CallBack;

        public void OnCallBack(string info)
            if (this.CallBack != null)
        # endregion


By viewing downloads associated with this article you agree to the Terms of Service and the article's licence.

If a file you wish to view isn't highlighted, and is a text file (not binary), please let us know and we'll add colourisation support for it.


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


About the Author

Abhishek Sur
India India
Did you like his post?

Oh, lets go a bit further to know him better.
Visit his Website : to know more about Abhishek.

Abhishek also authored a book on .NET 4.5 Features and recommends you to read it, you will learn a lot from it.

Basically he is from India, who loves to explore the .NET world. He loves to code and in his leisure you always find him talking about technical stuffs.

Presently he is working in WPF, a new foundation to UI development, but mostly he likes to work on architecture and business classes. ASP.NET is one of his strength as well.
Have any problem? Write to him in his Forum.

You can also mail him directly to

Want a Coder like him for your project?
Drop him a mail to

Visit His Blog

Dotnet Tricks and Tips

Dont forget to vote or share your comments about his Writing

You may also be interested in...

Permalink | Advertise | Privacy | Terms of Use | Mobile
Web02 | 2.8.170713.1 | Last Updated 31 Oct 2010
Article Copyright 2010 by Abhishek Sur
Everything else Copyright © CodeProject, 1999-2017
Layout: fixed | fluid