[RESOLVED]Initializing a new class does this use memory?

Hello,

A question that has just come to mind, when you declare a new variable say

var m = new SomeModel();

if you have roughly two hundred people hitting the same page through the day would the above statement use al ot of memory as its constantly creating the variable? or am I just being stupid?

Yes ,

Any obejcet initialization would consume memory. If ur looking at performance try to dispose the object once the request of served.

How do you dispose of a model in mvc?

you can dispose it as any other object :) ,

using(var ctx = new EntityModel())
{
    // do what you want 
  
}

Harrison.Scott

How do you dispose of a model in mvc?

You don’t need to. Once the request has finished executing, the .NET framework will take care of disposing whatever it feels it needs to dispose of. That’s why .NET code is referred to as "managed code". You only need to explicitly dispose of unmanaged resources
such as database connections, images, SMTP server connections etc.

Harrison.Scott

if you have roughly two hundred people hitting the same page through the day would the above statement use al ot of memory as its constantly creating the variable?

The object will live for as long as the request takes to complete – possibly 500 milliseconds. The memory required will depend on the properties of the model that need to be accommodated. An int will require 4 bytes of memory. A bool will require 1 byte. A
string will require 20 bytes + 2 bytes per character.  But for this is not something you should need to worry about generally.

actually .net uses a garbage collector (GC), so object memory is only reclaimed during a GC run, or when memory is low. On a large memory machine, it may take a long time to for the memory to be released once the object is not in use (no references). This
long delay, is why you need to call Dispose on objects that contain unmanaged resources.

bruce (sqlwork.com)

This long delay, is why you need to call Dispose on objects that contain unmanaged resources.

My understanding is that you need to call Dispose on unmanaged resources because the GC doesn’t know what to do otherwise. Apparently, "it [the GC] doesn’t know how to release and clean up the unmanaged resource."http://msdn.microsoft.com/en-us/library/498928w2(v=vs.110).aspx

Leave a Reply