Reputation: 1013
In the thread When immutable collections are preferable then concurrent it is stated that immutable collections may be slower but they save memory. How is it possible if every change of immutable collection results in creating new object?
I understand that concurrent collection refers to one object and it uses synchronization primitives. So how the immutable collection can save more memory than concurrent collection?
Upvotes: 0
Views: 565
Reputation: 43845
An immutable collection can save memory only under very specific circumstances. Mainly when your application's logic requires that snapshots of the collection must be taken frequently. Imagine for example that one workflow of your application has to take a snapshot of the collection every few seconds and do some work with it, while other workflows keep mutating the collection. That's the scenario where the immutable collections shine. Taking a snapshot of a concurrent collection is expensive in all aspects (CPU/memory/contention), while taking a snapshot of an immutable collection is essentially free. The immutable collections by definition have snapshot semantics. The only thing required in order to convert an immutable collection to a snapshot is a memory barrier, so that the thread requesting the snapshot can be sure that it will see the latest and fully initialized value stored in the field or variable. This memory barrier can be imposed by declaring the field as volatile
, or by reading the variable with the Volatile.Read
method. Example:
var snapshot = Volatile.Read(ref _users);
As a side note, it is also possible to update an immutable collection without taking a lock, by using Interlocked
techniques. There is even a class available (ImmutableInterlocked
) that simplifies greatly these operations. Example:
ImmutableInterlocked.Update(ref _users, x => x.Add(user));
Upvotes: 2