Lets say I have a class with a really large object, which lets say is a list of objects and loads of data into memory by fetching from the DB. This is for a WPF application.
public class MyClass {
public ReallyLargeObject largeObject;
//Other properties.
}
I have a view model(VM1) object which has a reference to bind this class to the XAML.
I need another view model (VM2) with only a certain properties of MyClass and not the largeObject.
Two scenarios:
I can assign VM2.myClass = VM1.myClass
var viewModel1 = new VM1() { myClass = new MyClass() { largeObject = new ReallyLargeObject(); //initialize other properties } }; var viewModel2 = new VM2() { myClass = viewModel1.myClass };
I can create a new object of type MyClass from VM1.myClass but this time setting largeObject = null
var viewModel1 = new VM1() { myClass = new MyClass() { largeObject = new ReallyLargeObject(); //initialize other properties } }; var viewModel2 = new VM2(){ myClass = new MyClass(){ //initialize other properties } };
Both the viewmodels will be in memory during runtime fairly for a significant amount of time.
I'm facing an issue with the first approach taking a lot of memory and the screen being rather slow.
Would creating a slimmer object from the original object reduce the memory used?
I'm confused cause the view model 2 technically has only the reference to the object in view model 1.
How does .NET behave when storing references to same object in two wrapping objects?
Is it like other languages where the reference alone would be stored and the memory allocated on the heap to the object is the same?