surpavan
surpavan

Reputation: 1412

Memory Consumption of Collection Types

I am trying to make a small app that uses a multiple hierarchy of type List within a List for some 20-30 levels. I tried with:

System.Collections.ComponentModel.ObservableCollection, but at run time, I got an OutOfMemoryException error. Then, I tried with List, and this time I did not get such an error.

What type of collection consumes the least amount of space? Or, what would be a good way to achieve this type of hierarchy? I just need a collection; I don't need change notifications, etc. I am using .NET 4 with VB, Linq, and WPF. I achieved the code looping process with Parallel.ForEach threading.

Edit: The Program is for string the file system data into Sql CE DB and retrieving it back. Hence, the hierarchy can be of 20-30 levels also.

Edit: There would be about 80000 Queries with linq for getting the hierarchy. The Type of data I am using is as follows:

Public Structure FileRecord
        Property ID As String
        Property Namee As String
        Property Size As String
        Property IsFolder As Boolean
        Property DateModified As Date
        Property FullPath As String
        Property Disk As String
        Property ParentID As String
        Property Items As List(Of FileRecord)
End Structure

Upvotes: 0

Views: 516

Answers (2)

lesscode
lesscode

Reputation: 6361

The performance characteristics of the various .NET collection classes vary widely, and the type of collection you'll want to use will also depend on how you will want to access the collection. As usual, there are trade-offs to be made between performance (in time and memory) and simplicity or convenience.

That said, one of the simplest and most performant collection types in .NET is probably Array.

Upvotes: 1

Devin
Devin

Reputation: 1064

The overhead associated with each collection wouldn't cause an OOM exception, as described above because both implement List<T>. The ObservableCollection can be used in conjunction with INotifyCollectionChanged to alert views and presenters/viewmodels of changes to the collection.

The bigger question is, what kind of type are you using? If you're running out of memory, each type might be allocating an unnecessary amount of memory. Also, I wouldn't use an ObservableCollection unless you intend to use binding.

Upvotes: 2

Related Questions