none
ObservableCollection<T> Memory Leak? RRS feed

  • Question

  • I am populating an ObservableCollection<T> where T is a simple model. One of the properties in my model is a byte[]. The ObservableCollection<T> is refreshed often, using these steps:

    1. ObservableCollection<T>.Clear()
    2. Populate ObservableCollection<T>

    Problem: The memory consumption grows everytime the observable collection is repopulated.

    My questions are:

    1. Doesn't the garbage collector free up the model items in the observable collection after the Clear()?
    2. If not, what steps do I need to take to free up the list of items after they have been cleared from the observable collection?
    • The ObservableCollection<T> is bound to a WPF control
    • Type <T> has only .Net int's, string's and the one byte[]
    • Type <T> does not implement IDisposable

    Thanks in advance for your help!

    Friday, August 12, 2016 1:28 PM

Answers

  • How are you measuring memory consumption?

    Because .Net manages its own special memory heap and doesn't necessarily release memory back to the system every single time the garbage collector frees up an object. This would mean it would also need to reallocate memory from the system each and every time a new object was created. And all that allocating/releasing memory would be very inefficient.

    Instead, when the garbage collector frees up an object, that memory is instead just flagged up as unused so that the memory manager knows that space is available for a new object. I believe that memory only gets released 'properly' (back to the operating system) once a critical threshold is reached or the system requests it.

    VS comes with various memory profiling tools (and there are likely 3rd-party products) you can use to check for real memory leaks if you think that is still a possibility.

    • Marked as answer by bobtrowe Friday, August 12, 2016 6:45 PM
    Friday, August 12, 2016 2:06 PM

All replies

  • How are you measuring memory consumption?

    Because .Net manages its own special memory heap and doesn't necessarily release memory back to the system every single time the garbage collector frees up an object. This would mean it would also need to reallocate memory from the system each and every time a new object was created. And all that allocating/releasing memory would be very inefficient.

    Instead, when the garbage collector frees up an object, that memory is instead just flagged up as unused so that the memory manager knows that space is available for a new object. I believe that memory only gets released 'properly' (back to the operating system) once a critical threshold is reached or the system requests it.

    VS comes with various memory profiling tools (and there are likely 3rd-party products) you can use to check for real memory leaks if you think that is still a possibility.

    • Marked as answer by bobtrowe Friday, August 12, 2016 6:45 PM
    Friday, August 12, 2016 2:06 PM
  • Thanks for the reply!

    I'm just watching the memory in Task Manager. So what you said makes sense.

    I will look into the profiling tools.

    Thanks again.

    Friday, August 12, 2016 6:44 PM