Red Gate forums :: View topic - IList<T> Custom Collections
Return to www.red-gate.com RSS Feed Available

Search  | Usergroups |  Profile |  Messages |  Log in  Register 
Go to product documentation
ANTS Memory Profiler Previous Versions
ANTS Memory Profiler Previous Versions forum

IList<T> Custom Collections

Search in ANTS Memory Profiler Previous Versions forum
Post new topic   Reply to topic
Jump to:  
Author Message
Ken Alexander



Joined: 17 Jul 2009
Posts: 1

PostPosted: Fri Jul 17, 2009 9:34 pm    Post subject: IList<T> Custom Collections Reply with quote

When using IList<T> for a custom collection. It appears that the underlying array is left in memory after the collection has been GC'd. It says 1 live instance of a size of 16 bytes.

I don't understand why it is left in memory. Any ideas would be appreciated.

Thanks,

Ken
Back to top
View user's profile Send private message
StephenC



Joined: 15 Oct 2007
Posts: 315
Location: Cambridge

PostPosted: Mon Jul 20, 2009 9:00 am    Post subject: Reply with quote

The object retention graph is designed to show what is referencing an object and therefore preventing it from being GC'd.

This should help you identify what's keeping the remaining instance in memory.

Stephen
Back to top
View user's profile Send private message
StephenC



Joined: 15 Oct 2007
Posts: 315
Location: Cambridge

PostPosted: Mon Jul 20, 2009 11:21 am    Post subject: Reply with quote

There's another possibility that it's simply on the Finalizer queue.

If you take another snapshot does the instance still remain?

Stephen
Back to top
View user's profile Send private message
sandyp



Joined: 18 Sep 2009
Posts: 2

PostPosted: Fri Sep 18, 2009 11:20 am    Post subject: Reply with quote

I have the same situation. The very simple WinForms test:

Code:
    public class MyInfoClass
    {
        string _name = String.Empty;

        public MyInfoClass(string name)
        {
            _name = name;
        }

        public string Name
        {
            get
            {
                return _name;
            }
            set
            {
                _name = value;
            }
        }
    }

    public partial class Form1 : Form
    {
        private List<MyInfoClass> _list = null;

        public Form1()
        {
            InitializeComponent();
            List<MyInfoClass> list = new List<MyInfoClass>();
            list.Add(new MyInfoClass("1"));
            list.Add(new MyInfoClass("2"));
            list.Add(new MyInfoClass("3"));

            _list = list;
        }

        private void button1_Click(object sender, EventArgs e)
        {
            _list = null;
        }

        private void button2_Click(object sender, EventArgs e)
        {
            _list.Clear();
            _list = null;
            GC.Collect();
        }
    }


After pressing button2, I have [GC Handle]->System.Object[]->MyInfoClass[] chain and it's remained in memory after new snapshots. Any ideas why?
Back to top
View user's profile Send private message
AndrewH



Joined: 17 Aug 2006
Posts: 137

PostPosted: Fri Sep 18, 2009 12:06 pm    Post subject: Reply with quote

It took me a little while to work this one out Smile It's odd, but you're not seeing something that indicates a memory leak here, I think.

System.Generic.List contains a static reference to an empty array, so that it can represent 0 item lists more effectively: that's the array that you're seeing after you click the button. Note that it's actually there beforehand as well: you'll see two arrays when you take a snapshot before clicking the button.

It's not a memory leak, but rather an implementation detail of the .NET framework. You'll note that the array is always very small and there's only every one of these.

The pattern [GC Handle]->System.Object[]->MyInfoClass[] usually indicates a static variable: this is how they're implemented internally by the .NET framework. The profiler does try to identify which variable contains a particular object provided you're using .NET 2 or later, but there are some CLR limitations that prevent it from working reliably in all cases. Generic classes, such as List<T> is one such example, unfortunately, which is why this gets presented in this way.
_________________
Andrew Hunter
Software Developer
Red Gate Software Ltd.
Back to top
View user's profile Send private message
sandyp



Joined: 18 Sep 2009
Posts: 2

PostPosted: Mon Sep 21, 2009 3:16 pm    Post subject: Reply with quote

Yes, it looks strange but your findings explain everything. Thanks!
Back to top
View user's profile Send private message
Display posts from previous:   
Reply to topic All times are GMT + 1 Hour
Page 1 of 1

 
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum


Powered by phpBB © 2001, 2005 phpBB Group