This question is a little confused. However lets test it out.
Given
public class MyGCCollectClass
{
public List<Version> Garbage { get; set; }
public List<int> MyInnocentList { get; set; }
public List<int> Main()
{
Garbage = new List<Version>();
for (var i = 0; i < 10000000; i++)
{
Garbage.Add(new Version());
}
MyInnocentList = new List<int>();
return MyInnocentList;
}
}
We can look at whats being collected and not
// Lets start from a collect
GC.Collect();
GC.WaitForPendingFinalizers();
Console.WriteLine("Memory used before collection: {0:N0}", GC.GetTotalMemory(false));
// create a class/viewmodel
var gcCollectClass = new MyGCCollectClass();
// create a world of garbage
// return back a list that is part of the class
var list = gcCollectClass.Main();
// lets see whats GC has
Console.WriteLine("Memory used: {0:N0}", GC.GetTotalMemory(true));
// make sure our garbage is still alive
Console.WriteLine(gcCollectClass.Garbage.Count);
// Kill the original class
gcCollectClass = null;
// Force a collection
GC.Collect();
GC.WaitForPendingFinalizers();
// double check the list is still alive
Console.WriteLine(list.Count);
// Lets make sure we havent caused a memory leak
Console.WriteLine("Memory used after full collection: {0:N0}",
GC.GetTotalMemory(true));
Output
Memory used before collection: 30,088
Memory used: 307,138,940
10000000
1
Memory used after full collection: 29,968
The fact is, just because you are returning a list from your class and its part of the class doesn't mean returning it will stop your viewmodal from being cleaned up. It "will" get collected in this situation and there are no memory leaks
Further reading
Why doesn't C# support the return of references?
Memory in .NET - what goes where
The memory slot for a variable is stored on either the stack or the
heap. It depends on the context in which it is declared:
- Each local variable (ie one declared in a method) is stored on the stack. That includes reference type variables - the variable itself is
on the stack, but remember that the value of a reference type variable
is only a reference (or null), not the object itself. Method
parameters count as local variables too, but if they are declared with
the ref modifier, they don't get their own slot, but share a slot with
the variable used in the calling code. See my article on parameter
passing for more details.
- Instance variables for a reference type are always on the heap. That's where the object itself "lives".
- Instance variables for a value type are stored in the same context as the variable that declares the value type. The memory slot for the
instance effectively contains the slots for each field within the
instance. That means (given the previous two points) that a struct
variable declared within a method will always be on the stack, whereas
a struct variable which is an instance field of a class will be on the
heap.
- Every static variable is stored on the heap, regardless of whether it's declared within a reference type or a value type. There is only
one slot in total no matter how many instances are created. (There
don't need to be any instances created for that one slot to exist
though.) The details of exactly which heap the variables live on are
complicated, but explained in detail in an MSDN article on the
subject.
https://ericlippert.com/2011/06/23/ref-returns-and-ref-locals/