I want to know which is better way regarding memory management from both cause
With using using
public void AddUser(User user)
{
using (var myentities = new MyEntities())
{
myentities .AddTotblUsers(user);
myentities .SaveChanges();
}
}
Without using using
public void AddUser(User user)
{
var myentities = new MyEntities();
myentities .AddTotblUsers(user);
myentities .SaveChanges();
}
which one remove first object from memory? first , second or both same ?
The first one
usingdispose the object resources and should free the resources healed by the object.Where in the second method, you are relying on the garbage collector to do that for you, However the garbage collector will do it at some not deterministic point while your application is executing.
It worth to mentioned here that the using statement is converted to something like:
So it wrap the whole object at try/finally block and when finish using it, it alwyes calls dispose to free the resource, even if exception is thrown at the process inside the using we are sure that our resource is disposed probably.