0
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?

2 Answers 2

2

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.

Sign up to request clarification or add additional context in comments.

Comments

0

if populateViewModel method does not use disaposable resources (as data context) or uses and disposes them, your code should be fine.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.