是否通过引用类型使用文献保存记忆? [英] Does passing Reference Types using ref save memory?

查看:145
本文介绍了是否通过引用类型使用文献保存记忆?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在C#中,参数的方法可以是引用类型还是值类型。当传递引用类型,引用的一个副本传递。这样,如果一个方法内我们尝试重新分配该传递引​​用另一个对象例如,该方法的外部的重新分配是不可见的。

In C#, the parameters to a method can be either reference types or value types. When passing reference types, a copy of the reference is passed. This way, if inside a method we try to reassign the passed reference to another object instance, outside of the method the reassignment is not visible.

要进行这方面的工作,C#有裁判修改。传递与参考引用类型实际使用原来的基准,而不是一个副本。 (纠正我,如果我错了)。

To make this working, C# has the ref modifier. Passing a reference type with ref actually uses the original reference instead of a copy. (Correct me if I'm wrong).

在这种情况下,因为我们没有创造引用的副本,是我们保存任何记忆?如果一个方法被广泛地称为,这是否提高应用程序的整体性能?

In this case, since we are not creating a copy of the reference, are we saving any memory? If a method is extensively called, does this improve the overall performance of the application?

谢谢!

推荐答案

没有,没有。如果有的话,那是因为额外的查找速度较慢。

Claim

No, it doesn't. If anything, it's slower because of the extra lookup.

还有没有理由以传递引用类型引用,除非你专门打算以后分配给它。

There's no reason to pass a reference type by reference unless you specifically intend to assign to it later.

由于一些人似乎认为,编译器通过可变的自身的,看看这个code拆卸:

Since some people seem to think that the compiler passes "the variable itself", take a look at the disassembly of this code:

using System;

static class Program
{
    static void Test(ref object o) { GC.KeepAlive(o); }

    static void Main(string[] args)
    {
        object temp = args;
        Test(ref temp);
    }
}

这是(在x86,为简单起见):

which is (on x86, for simplicity):

// Main():
// Set up the stack
00000000  push        ebp                    // Save the base pointer
00000001  mov         ebp,esp                // Set up stack pointer
00000003  sub         esp,8                  // Reserve space for local variables
00000006  xor         eax,eax                // Zero out the EAX register

// Copy the object reference to the local variable `temp` (I /think/)
00000008  mov         dword ptr [ebp-4],eax  // Copy its content to memory (temp)
0000000b  mov         dword ptr [ebp-8],ecx  // Copy ECX (where'd it come from??)
0000000e  cmp         dword ptr ds:[00318D5Ch],0  // Compare this against zero
00000015  je          0000001C               // Jump if it was null (?)
00000017  call        6F910029               // (Calls some internal method, idk)

// THIS is where our code finally starts running
0000001c  mov         eax,dword ptr [ebp-8]  // Copy the reference to register
0000001f  mov         dword ptr [ebp-4],eax  // ** COPY it AGAIN to memory
00000022  lea         ecx,[ebp-4]            // ** Take the ADDRESS of the copy
00000025  call        dword ptr ds:[00319734h] // Call the method

// We're done with the call
0000002b  nop                                // Do nothing (why?)
0000002c  mov         esp,ebp                // Restore stack
0000002e  pop         ebp                    // Epilogue
0000002f  ret                                // Return

这是从的优化的的code编译。很显然,有一个变量的地址被传递,而不是变量本身。

This was from an optimized compilation of the code. Clearly, there's an address of a variable being passed, and not "the variable itself".

这篇关于是否通过引用类型使用文献保存记忆?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆