简体   繁体   English

使用块与 function 块

[英]using block Vs function block

I want to know which is better way regarding memory management from both cause我想从这两个原因了解关于 memory 管理的更好方法

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?哪一个从 memory 中删除第一个 object? first, second or both same?第一个,第二个还是两者都一样?

The first one using dispose the object resources and should free the resources healed by the object.第一个using处理 object 资源并释放由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:这里值得一提的是 using 语句被转换为类似:

{
    Entities myentities = new MyEntities();
    try
    {
        myentities.AddTotblUsers(user);
        myentities.SaveChanges();
    }
    finally
    {
        if (myentities != null)
            ((IDisposable)myEntities).Dispose();
    }
}

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.所以它将整个 object 包装在 try/finally 块中,当完成使用它时,它总是调用 dispose 来释放资源,即使在使用过程中抛出异常,我们也确信我们的资源可能已被释放。

using statement is useful only for objects implementing IDisposable , usually where the underlying resource is unmanaged. using语句仅对实现IDisposable的对象有用,通常在底层资源是非托管的。

See this: http://msdn.microsoft.com/en-us/library/yh598w02.aspx看到这个: http://msdn.microsoft.com/en-us/library/yh598w02.aspx

For other scenarios, it is advisable to let automatic garbage collector do its job.对于其他场景,建议让自动垃圾收集器完成其工作。

See this: http://msdn.microsoft.com/en-us/library/aa691138(v=vs.71).aspx看到这个: http://msdn.microsoft.com/en-us/library/aa691138(v=vs.71).aspx

Using of using is the better way.使用using是更好的方法。

The using statement ensures that Dispose is called even if an exception occurs while you are calling methods on the object. using 语句确保即使在您调用 object 上的方法时发生异常,也会调用 Dispose。 You can achieve the same result by putting the object inside a try block and then calling Dispose in a finally block;您可以通过将 object 放在 try 块中,然后在 finally 块中调用 Dispose 来获得相同的结果; in fact, this is how the using statement is translated by the compiler.事实上,这就是编译器翻译 using 语句的方式。

the first is better - at least for cases where MyEntities implements IDisposable... but it is better esp.第一个更好 - 至少对于 MyEntities 实现 IDisposable 的情况......但它更好,尤其是。 if any exception occurs...如果发生任何异常...

see http://msdn.microsoft.com/en-us/library/yh598w02.aspx请参阅http://msdn.microsoft.com/en-us/library/yh598w02.aspx

The using block is typically used for an object that contains references to un-managed objects or other things that won't get garbage collected. using 块通常用于 object,其中包含对非托管对象或其他不会被垃圾回收的对象的引用。 Using block can only be used with objects that support the IDisposible interface. Using 块只能用于支持IDisposible接口的对象。 It has no effect on when the value gets garbage collected.它对值何时被垃圾收集没有影响。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM