-1

I have a MVC web application, it is an old and large source, now am modifying it for unit testing purpose. I need to initialize multiple objects in controller's constructor to abstract the DB accessing codes. like below,

            public DownloadsController(ICustomerHistory customerHistory = null, ICustomerInfo customerInfo = null, IDownloadInfo downloadInfo = null, IVersionAccess versionAccess = null,
        ILeadInformation leadInfo = null, ISalesForce salesForce = null,IEvaluationReleaseBuildModel evaluationReleaseBuildModel=null)
    {
        _customerHistory = customerHistory ?? new CustomerHistory();
        _customerInfo = customerInfo ?? new CustomerInfo();
        _downloadInfo = downloadInfo ?? new DownloadInfo();
        _versionAccess = versionAccess ?? new VersionAccess();
        _leadInformation = leadInfo ?? new LeadInformation();
        _salesForce = salesForce ?? new SalesForce();
        _evaluationReleaseBuildModel = evaluationReleaseBuildModel ?? new EvaluationReleaseBuildModel();
    }

now I am afraid of if any memory problem will occur due to creating multiple objects like this.

  1. is it create a memory problem?.
  2. do I need to destroy the object manually or GC will take care of this..?.
  3. if we manually need to destroy, how to destroy it. I don't know when we need to destroy this object

thanks in advance.

Nagaraj M
  • 480
  • 3
  • 17

2 Answers2

2

Check out the conditions for GC specified in MSDN.

It says the conditions are,

•The system has low physical memory.

•The memory that is used by allocated objects on the managed heap surpasses an acceptable threshold. This threshold is continuously adjusted as the process runs.

•The GC.Collect method is called. In almost all cases, you do not have to call this method, because the garbage collector runs continuously. This method is primarily used for unique situations and testing.

If you want your application to take care of the memory allocated to you, you might want to use Dispose.

As MSDN says,

•You implement a Dispose method to release unmanaged resources used by your application. The .NET Framework garbage collector does not allocate or release unmanaged memory.

Here's a detailed explanation from MSDN.

Prajwal
  • 3,930
  • 5
  • 24
  • 50
  • thanks for your response, dispose called when object has no reference right. my doubt is how and when i need to dereference my object..? 1. since my applciation is larger and these created objects used in many places like in models and base so i cant use using options to automatically dispose the object. – Nagaraj M Jan 12 '17 at 04:58
  • I was wrong. Dispose is not called by GC. you need to call it manually to free up the space. If your application takes up a lot of memory, then you will free-up non-important things using dispose. Here's a link about dispose. http://stackoverflow.com/questions/2871888/dispose-when-is-it-called – Prajwal Jan 12 '17 at 05:05
1

To address this in the more general case, there are only a few cases where you have to explicitly free up memory.

The biggest case is where a class holds unmanaged resources (e.g. COM objects, etc.) where you need to perform custom cleanup steps. This is where a Dispose method comes in.

A few other examples: event handlers and collection classes can cause de facto memory leaks by holding onto references to objects after you intend for them to be garbage collected. If, for example, a collection class "lives" longer than some of the items it contains, you'll need to manually remove the items from the collection class when you're done with them.

Apart from these (failing to clean up unmanaged resources or having a de facto memory leak) and a few other edge cases, you can usually trust the GC to do its job well.

So the answer to your original question really depends on whether you hold a reference to these items longer than you need to (it's a little hard to tell how long you're actually holding them - or how long they actually need to be held - based solely on the code sample). If you do, then it could cause memory problems; if you don't, you can trust the garbage collector.