public class SomeViewModel
{
public List<Something> listOfSomethings = new List<Something>();
public Entity EntityObj;
etc...
etc..
..
}
public class Controller()
{
public SomeViewModel viewModel;
public ActionResult SomeAction()
{
viewModel = populateViewModel();
return View(viewModel);
}
}
The SomeViewModel is a large object that is populated in the controller’s action. Will it be GC’d or cleared from memory when the controller is disposed?
There is no point of this
public SomeViewModel viewModel;field in your controller. Controller actions are independant meaning that if you first invokeSomeActionwhich sets a value for this field and then invoke some other action do not expect this field to survive. So you should simply use this:This being said your current code doesn’t seem to have memory leaks because once the request ends the Controller class will be eligible for GC and so all its instance fields including the view model.