Reputation: 285
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?
Upvotes: 0
Views: 510
Reputation: 1039298
There is no point of this public SomeViewModel viewModel;
field in your controller. Controller actions are independant meaning that if you first invoke SomeAction
which 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:
public class HomeController: Controller
{
public ActionResult SomeAction()
{
var viewModel = populateViewModel();
return View(viewModel);
}
public ActionResult SomeOtherAction()
{
var viewModel = populateViewModel();
return View(viewModel);
}
}
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.
Upvotes: 2
Reputation: 8540
if populateViewModel method does not use disaposable resources (as data context) or uses and disposes them, your code should be fine.
Upvotes: 0