声明变量是否已经占用内存 [英] Does declaring a variable already occupy memory

查看:309
本文介绍了声明变量是否已经占用内存的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我一直在寻找答案. 我想知道我的代码何时占用内存.

Hi I have been searching for the answer to this quite long. I want to know things under the hood regarding when memory will be taken up by my code.

例如 int myVar;

Ex. int myVar;

  1. 此代码是否已占用内存?内存,我的意思是堆栈? 最初在.NET中,我注意到该行的默认值为ZERO(0).
  2. 但是变量真正存储了什么?值零或存储值零(0)的地址?
  1. Does this code already take up memory? by memory i mean the stack? and initially in .NET I have noticed that this line will have a default value of ZERO(0).
  2. But what does the variable really store? the value ZERO or an address to where the value ZERO(0) is stored?

据我对引用类型的了解,也是当我编写此行代码时

Also from what I understand about reference type is that when i write this line of code

MyClass myObj; //仅声明

MyClass myObj; // declare only

myObj = new MyClass()//创建实例

myObj = new MyClass() //create instance

我想知道引擎盖下会发生什么.我的假设正确吗? myObj将驻留在堆栈中,并且myObj能够存储地址.在我创建实例时 该对象在堆上创建,并且该对象的地址传递到myObj变量.

I want to know what happens under the hood. Are my assumptions correct? That myObj will reside at the stack and myObj is capable of storing address. and at my creating an instance the object is created at the heap and the address of that object is passed to the myObj variable.

问题是,我想知道下面两个之间是否有更有效的(在内存和性能方面).

The thing is, I want to know if what is more efficient(memory-wise and performance-wise) between the two below.

代码1: int myVar; myVar = FunctionThatReturnsAnInteger(); Console.WriteLine(myVar);

Code 1: int myVar; myVar = FunctionThatReturnsAnInteger(); Console.WriteLine(myVar);

代码

Console.WriteLine(FunctionThatReturnsAnInteger());

Console.WriteLine(FunctionThatReturnsAnInteger());

我想对此做一个很好的解释,类比非常方便.

I want a good explanation for this and analogy is quite handy.

先谢谢了. :)

推荐答案

性能效率-效率更高,但仅需执行少量指令,即可从执行堆栈中存储和加载变量.请记住,抖动可能会在运行时优化您的代码,从而删除所有不必要的指令.

Performance efficiency - more efficient but only by a few instructions, storing and loading the variable from the execution stack. Bear in mind that the Jitter may optimise your code at run-time, removing any unnecessary instructions.

内存效率-效率更高,因为在执行堆栈上没有定义本地var.

Memory efficiency - more efficient as there is no local var defined on the execution stack.

代码清晰-变量的定义更接近其使用位置,使代码更易用.在我看来,将一种方法返回另一种方法甚至更干净,但是确实存在一个缺点,那就是很难检查返回的值(但是在VS 2013中,这是可能的).

Code clarity - having the definition of a variable closer to where it's used makes the code more usable. In my opinion having the one method returning into another is even cleaner however does have the downside that it's harder to inspect the value being returned (in VS 2013 this will be possible however).

具有temp变量的IL:

IL with the temp variable:

  // Code size       13 (0xd)
  .maxstack  1
  .locals init ([0] int32 myVar)
  IL_0000:  call       int32 StackOverflowScratchPad.Program::FunctionThatReturnsAnInteger()
  IL_0005:  stloc.0
  IL_0006:  ldloc.0
  IL_0007:  call       void [mscorlib]System.Console::WriteLine(int32)
  IL_000c:  ret

且不包含:

  // Code size       11 (0xb)
  .maxstack  8
  IL_0000:  call       int32 StackOverflowScratchPad.Program::FunctionThatReturnsAnInteger()
  IL_0005:  call       void [mscorlib]System.Console::WriteLine(int32)
  IL_000a:  ret

奖金-在声明时为temp var赋值的IL:

Bonus - IL when assigning an value to temp var at the declaration:

  // Code size       15 (0xf)
  .maxstack  1
  .locals init ([0] int32 myVar)
  IL_0000:  ldc.i4.5
  IL_0001:  stloc.0
  IL_0002:  call       int32 StackOverflowScratchPad.Program::FunctionThatReturnsAnInteger()
  IL_0007:  stloc.0
  IL_0008:  ldloc.0
  IL_0009:  call       void [mscorlib]System.Console::WriteLine(int32)
  IL_000e:  ret

这篇关于声明变量是否已经占用内存的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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