前言

 
虽然在.Net Framework 中我们不必考虑内在管理和垃圾回收(GC),但是为了优化应用程序性能我们始终需要了解内存管理和垃圾回收(GC)。另外,了解内存管理可以帮助我们理解在每一个程序中定义的每一个变量是怎样工作的。
 

简介

 
这篇文章我们将介绍一些方法参数传递行为在堆与栈中的影响。前几节我们介绍了堆与栈的基本工作原理,程序执行时值类型与引用类型在堆栈中的存储。另外,我们已经介绍了一些关于指针的基本知识。这一节中参数传递对堆栈的影响很重要,下面会慢慢道来。
 

参数,大画面

下面是当代码运行时会产生的一个详细过程。上几节已经介绍过当一个方法被调用时会产生的基本情况,让我们来看一下更加详细的内容。
当我们调用一个方法时会发生以下情形:
  1. 栈会分配一块内存空间给程序执行所需要的信息(我们叫它栈结构Stack Frame)。一个栈结构包含方法调用地址(指针),它以一个GOTO指令的形式存在栈里。因此,当程序执行完方法(method)时,它会知道怎么样返回并继续执行代码。
  2. 方法的所有参数将被复制到栈里,这是我们将要更加详细介绍的部分。
  3. 控制被传递到JIT编译过的方法里,同时线程开始执行代码。此时,我们将有另一个方法呈现在栈结构的“回调栈”里。
代码:
  1. public int AddFive(int pValue)
  2. {
  3. int result;
  4. result = pValue + 5;
  5. return result;
  6. }

栈像下图所示:

 
注意:ReturnValue方法不会存在栈上,图中把ReturnValue作为此栈结构的开始只是为了解释栈原理。
 
像前几节介绍的,值类型和引用类型在栈里的存储是不同的。栈为任何值类型创建副本,栈也为任何引用类型的指针创建副本。
 

值类型传递

下面是值类型传递在栈里的内幕。
 
首先,当我们传递一个值类型变量时,栈会为它分配一块内存空间并把值类型变量的值存储进去。看下面的代码:
  1. class Class1
  2. {
  3. public void Go()
  4. {
  5. int x = 5;
  6. AddFive(x);
  7. Console.WriteLine(x.ToString());
  8. }
  9. public int AddFive(int pValue)
  10. {
  11. pValue += 5;
  12. return pValue;
  13. }
  14. }

当代码执行时,栈为x分配一块内存空间并存储值5

然后,AddFive()被放到栈上,同时栈分配内存空间给参数pValue并复制x的值给它。
当AddFive()执行完成,线程被传递回Go()。同时因为AddFive()执行完,它的参数pValue也实质上被移除。
所以结果是5是合理的。关键点是任何被传递的值类型参数仅是一个碳复制,因为我们希望保护原始变量的值。
有一点要记住的是,如果我们有一个非常庞大的值类型(如,庞大的struct类型)传递到栈里,当处理器循环复制它并循环占有栈空间时将会非常耗资源。栈没有无限的空间去使用,就像用水杯不断的接水早晚会溢出一样。Struct类型可以变得非常庞大,我们要小心并清醒的使用它。
 
下面是一个比较大的struct结构类型:
  1. public struct MyStruct
  2. {
  3. long a, b, c, d, e, f, g, h, i, j, k, l, m;
  4. }

让我们看看执行下面代码Go()方法时再到DoSomething()方法会发生的情况:

  1. public void Go()
  2. {
  3. MyStruct x = new MyStruct();
  4. DoSomething(x);
  5. }
  6. public void DoSomething(MyStruct pValue)
  7. {
  8. // 省略具体实现....
  9. }

这可能会非常低效。想像一下如果我们传递MyStruct几千次,它会怎么样让程序死掉。
 
那么,我们怎么才能回避这样的问题呢?那就是仅传递原始值类型的引用。
public void Go()
          {
             MyStruct x = new MyStruct();
             DoSomething(ref x);
              
          }
 
           public struct MyStruct
           {
               long a, b, c, d, e, f, g, h, i, j, k, l, m;
           }
 
           public void DoSomething(ref MyStruct pValue)
           {
                    // 省略实现....
           }
 
 
这样就能节省内存并提升内存使用效率
 
唯一需要注意的是传递引用时我们在访问原始变量x的值,任可对pValue的改变都会影响到x。
下面的代码会将x改变成"12345",因为pValue.a实际上指向原始x声明时所在的内存地址。
  1. public void Go()
  2. {
  3. MyStruct x = new MyStruct();
  4. x.a = 5;
  5. DoSomething(ref x);
  6. Console.WriteLine(x.a.ToString());
  7. }
  8. public void DoSomething(ref MyStruct pValue)
  9. {
  10. pValue.a = 12345;
  11. }

前言

 
虽然在.Net Framework 中我们不必考虑内在管理和垃圾回收(GC),但是为了优化应用程序性能我们始终需要了解内存管理和垃圾回收(GC)。另外,了解内存管理可以帮助我们理解在每一个程序中定义的每一个变量是怎样工作的。
 

简介

 
继续上篇未完成的“参数传递对堆栈的影响”。
 

引用类型传递

 
传递引用类型跟上一节所示例中用引用的方式传递值类型相似。
 
如果使用引用类型(原文可能笔误,写的是值类型):
  1. public class MyInt
  2. {
  3. public int MyValue;
  4. }

然后调用Go()方法,MyInt会被放到堆里因为它是一个引用类型。

  1. public void Go()
  2. {
  3. MyInt x = new MyInt();
  4. }

 
如果执行下面代码中的Go():
  1. public void Go()
  2. {
  3. MyInt x = new MyInt();
  4. x.MyValue = 2;
  5. DoSomething(x);
  6. Console.WriteLine(x.MyValue.ToString());
  7. }
  8. public void DoSomething(MyInt pValue)
  9. {
  10. pValue.MyValue = 12345;
  11. }

会发生这种情况:

 
  1. 开始调用Go(),栈分配一块内存空间给x。
  2. 执行行到DoSomething(),栈分配一块内在空间给pValue。
  3. x的值是堆中MyInt对应在栈里的内存地址,复制x给pValue。
因此,我们用pValue改变MyInt的MyValue的值时,x最终也会获得这个改变的值"12345“。
如果我们用引用的方式传递一个引用类型变量呢?
 

用引用的方式传递引用类型

我们有一个类Thing, 类Animal和Vegetables衍生于Thing:
  1. public class Thing
  2. {
  3. }
  4. public class Animal:Thing
  5. {
  6. public int Weight;
  7. }
  8. public class Vegetable:Thing
  9. {
  10. public int Length;
  11. }

执行下面的Go()方法:

  1. public void Go()
  2. {
  3. Thing x = new Animal();
  4. Switcharoo(ref x);
  5. Console.WriteLine(
  6. "x is Animal    :   "
  7. + (x is Animal).ToString());
  8. Console.WriteLine(
  9. "x is Vegetable :   "
  10. + (x is Vegetable).ToString());
  11. }
  12. public void Switcharoo(ref Thing pValue)
  13. {
  14. pValue = new Vegetable();
  15. }

x最终变成Vegetable。

打印结果:
  1. x is Animal    :   False
  2. x is Vegetable :   True

让我们看看堆栈里到底发生了什么情况

 
  1. 调用Go()方法,栈分配一块内存空间给x。
  2. 堆分配一块内存空间给Animal。
  3. 开始执行Switcharoo()方法,栈分配一块内存空间给pValue并指向x。
  4. 栈分配一块内存空间给Vegetable。
  5. pValue改变了x的值使其指向Vegetable的内在地址。
如果我们不是用ref传递的,打印结果正相反。
 
 

总结

 
我们已经演示了参数传递是怎么在内在中处理的。在接下来的文章里,存储在栈中的引用变量会产生什么情况以及怎么解决对象复制带来的问题。

Even though with the .NET framework we don't have to actively worry about memory management and garbage collection (GC), we still have to keep memory management and GC in mind in order to optimize the performance of our applications. Also, having a basic understanding of how memory management works will help explain the behavior of the variables we work with in every program we write. In this article I'll cover some of the behaviors we need to be aware of when passing parameters to methods.

In Part I we covered the basics of the Heap and Stack functionality and where Variable Types and Reference Types are allocated as our program executes. We also covered the basic idea of what a Pointer is.

Parameters, the Big Picture.

Here's the detailed view of what happens as our code executes. We covered the basics of what happens when we make a method call in Part I. Let's get into more detail...

When we make a method call here's what happens:

  1. Space is allocated for information needed for the execution of our method on the stack (called a Stack Frame). This includes the calling address (a pointer) which is basically a GOTO instruction so when the thread finishes running our method it knows where to go back to in order to continue execution.
  2. Our method parameters are copied over. This is what we want to look at more closely.
  3. Control is passed to the JIT'ted method and the thread starts executing code. Hence, we have another method represented by a stack frame on the "call stack".

The code:

public int AddFive(int pValue)
          {
                int result;
                result = pValue + 5;
                return result;
          }

Will make the stack look like this:

NOTE : the method does not live on the stack, and is illustrated here just for reference as the beginnnig of the stack frame.
 
As discussed in Part I, Parameter placement on the stack will be handled differently depending on whether it is a value type or a reference type. A value types is copied over and the reference of a reference type is copied over.ed over.

Passing Value Types.

Here's the catch with value types...

First, when we are passing a value types, space is allocated and the value in our type is copied to the new space on the stack. Look at the following method:

class Class1

{

public void Go()

{

int x = 5;

AddFive(x);

Console.WriteLine(x.ToString());

}

public int AddFive(int pValue)

{

pValue += 5;

return pValue;

}

}

As the method executes, space for "x" is placed on the stack with a value of 5.

Next, AddFive() is placed on the stack with space for it's parameters and the value is copied, bit by bit from x.

When AddFive() has finished execution, the thread is passed back to Go() and because AddFive() has completed, pValue is essentially "removed":

So it makes sense that the output from our code is "5", right? The point is that any value type parameters passed into a method are carbon copies and we count on the original variable's value to be preserved.

One thing to keep in mind is that if we have a very large value type (such as a big struct) and pass it to the stack, it can get very expensive in terms of space and processor cycles to copy it over each time. The stack does not have infinite space and just like filling a glass of water from the tap, it can overflow. A struct is a value type that can get pretty big and we have to be aware of how we are handling it.

Here's a pretty big struct:

public struct MyStruct

{

long a, b, c, d, e, f, g, h, i, j, k, l, m;

}

Take a look at what happens when we execute Go() and get to the DoSomething() method below:

public void Go()

{

MyStruct x = new MyStruct();

DoSomething(x);

}

public void DoSomething(MyStruct pValue)

{

// DO SOMETHING HERE....

}

This can be really inefficient. Imaging if we passed the MyStruct a couple thousand times and you can understand how it could really bog things down.

So how do we get around this problem? By passing a reference to the original value type as follows:

public void Go()

{

MyStruct x = new MyStruct();

DoSomething(ref x);

}

public struct MyStruct

{

long a, b, c, d, e, f, g, h, i, j, k, l, m;

}

public void DoSomething(ref MyStruct pValue)

{

// DO SOMETHING HERE....

}

This way we end up with more memory efficient allocation of our objects in memory.

The only thing we have to watch out for when passing our value type by reference is that we have access to the value type's value. Whatever is changed in pValue is changed in x. Using the code below, our results are going to be "12345" because the pValue.a actually is looking at the memory space where our original x variable was declared.

public void Go()

{

MyStruct x = new MyStruct();

x.a = 5;

DoSomething(ref x);

Console.WriteLine(x.a.ToString());

}

public void DoSomething(ref MyStruct pValue)

{

pValue.a = 12345;

}

Passing Reference Types.

Passing parameters that are reference types is similar to passing value types by reference as in the previous example.

If we are using the value type

public class MyInt

{

public int MyValue;

}

And call the Go() method, the MyInt ends up on the heap because it is a reference type:

public void Go()

{

MyInt x = new MyInt();

}

If we execute Go() as in the following code ...

public void Go()

{

MyInt x = new MyInt();

x.MyValue = 2;

DoSomething(x);

Console.WriteLine(x.MyValue.ToString());

}

public void DoSomething(MyInt pValue)

{

pValue.MyValue = 12345;

}

Here's what happens...

  1. Starting with the call to Go() the variable x goes on the stack.
  2. Starting with the call to DoSomething() the parameter pValue goes on the stack.
  3. The value of x (the address of MyInt on the stack) is copied to pValue

So it makes sense that when we change the MyValue property of the MyInt object in the heap using pValue and we later refer to the object on the heap using x, we get the value "12345".

So here's where it gets interesting. What happens when we pass a reference type by reference?

Check it out. If we have a Thing class and Animal and Vegetables are both things:

public class Thing

{

}

public class Animal:Thing

{

public int Weight;

}

public class Vegetable:Thing

{

public int Length;

}

And we execute the Go() method below:

public void Go()

{

Thing x = new Animal();

Switcharoo(ref x);

Console.WriteLine(

"x is Animal    :   "

+ (x is Animal).ToString());

Console.WriteLine(

"x is Vegetable :   "

+ (x is Vegetable).ToString());

}

public void Switcharoo(ref Thing pValue)

{

pValue = new Vegetable();

}

Our variable x is turned into a Vegetable.

x is Animal    :   False
x is Vegetable :   True

Let's take a look at what's happening:

  1. Starting with the Go() method call, the x pointer goes on the stack
  2. The Animal goes on the hea
  3. Starting with the call to Switcharoo() method, the pValue goes on the stack and points to x
  4. The Vegetable goes on the heapthe heap
  5. The value of x is changed through pValue to the address of the Vegetable

If we don't pass the Thing by ref, we'll keep the Animal and get the opposite results from our code.

If the above code doesn't make sense, check out my article on types of Reference variables to get a better understanding of how variables work with reference types.

In Conclusion.

We've looked at how parameter passing is handled in memory and now know what to look out for. In the next part of this series, we'll take a look at what happens to reference variables that live in the stack and how to overcome some of the issues we'll have when copying objects.

For now.

[No0000145]深入浅出图解C#堆与栈 C# Heap(ing) VS Stack(ing)理解堆与栈2/4的更多相关文章

  1. [No0000144]深入浅出图解C#堆与栈 C# Heap(ing) VS Stack(ing)理解堆与栈1/4

    前言   虽然在.Net Framework 中我们不必考虑内在管理和垃圾回收(GC),但是为了优化应用程序性能我们始终需要了解内存管理和垃圾回收(GC).另外,了解内存管理可以帮助我们理解在每一个程 ...

  2. [No0000147]深入浅出图解C#堆与栈 C# Heap(ing) VS Stack(ing)理解堆与栈4/4

    前言   虽然在.Net Framework 中我们不必考虑内在管理和垃圾回收(GC),但是为了优化应用程序性能我们始终需要了解内存管理和垃圾回收(GC).另外,了解内存管理可以帮助我们理解在每一个程 ...

  3. [No0000146]深入浅出图解C#堆与栈 C# Heap(ing) VS Stack(ing)理解堆与栈3/4

    前言   虽然在.Net Framework 中我们不必考虑内在管理和垃圾回收(GC),但是为了优化应用程序性能我们始终需要了解内存管理和垃圾回收(GC).另外,了解内存管理可以帮助我们理解在每一个程 ...

  4. Java 堆内存与栈内存异同(Java Heap Memory vs Stack Memory Difference)

    --reference Java Heap Memory vs Stack Memory Difference 在数据结构中,堆和栈可以说是两种最基础的数据结构,而Java中的栈内存空间和堆内存空间有 ...

  5. java - Stack栈和Heap堆的区别

    首先分清楚Stack,Heap的中文翻译:Stack—栈,Heap—堆.         在中文里,Stack可以翻译为“堆栈”,所以我直接查找了计算机术语里面堆和栈开头的词语:        堆存储 ...

  6. 内存,堆,栈,heap,stack,data

    1. 基本类型占一块内存. 引用类型占两块. 2. 类是静态概念. 函数中定义的基本类型变量和对象的引用类型变量都在函数的栈内存. 局部变量存在栈内存. new创建的对象和数组,存在堆内存. java ...

  7. iOS:堆(heap)和栈(stack)的理解

    Objective-C的对象在内存中是以堆的方式分配空间的,并且堆内存是由你释放的,即release 栈由编译器管理自动释放的,在方法中(函数体)定义的变量通常是在栈内,因此如果你的变量要跨函数的话就 ...

  8. iOS中的堆(heap)和栈(stack)的理解

    操作系统iOS 中应用程序使用的计算机内存不是统一分配空间,运行代码使用的空间在三个不同的内存区域,分成三个段:“text segment “,“stack segment ”,“heap segme ...

  9. stm32 堆和栈(stm32 Heap & Stack)【worldsing笔记】

    关于堆和栈已经是程序员的一个月经话题,大部分有是基于os层来聊的.   那么,在赤裸裸的单片机下的堆和栈是什么样的分布呢?以下是网摘:     刚接手STM32时,你只编写一个 int main() ...

随机推荐

  1. 格雷码(Gray code)仿真

    作者:桂. 时间:2018-05-12  16:25:02 链接:http://www.cnblogs.com/xingshansi/p/9029081.html 前言 FIFO中的计数用的是格雷码, ...

  2. CINATRA发布第一个版本

    cinatra是什么? cinatra是C++开源社区–purecpp发起的一个开源项目,现在正式发布第一个版本cinatra0.9.0,cinatra是一个现代C++写的web framework, ...

  3. 手动下载python更新后 换回以前版本

    因为用的时Ubuntu略低版本的,不想更新版本,但是经常更新内核和其他软件,尤其是最近自己更新python,但是软件更新救出错了,而且不能打开“Languae Support”(软件支持)和ibus输 ...

  4. C语言 · 2n皇后问题

    基础练习 2n皇后问题   时间限制:1.0s   内存限制:512.0MB        锦囊1 搜索算法. 锦囊2 先搜索n皇后的解,在拼凑成2n皇后的解. 问题描述 给定一个n*n的棋盘,棋盘中 ...

  5. Fixed Partition Memory Management UVALive - 2238 建图很巧妙 km算法左右顶点个数不等模板以及需要注意的问题 求最小权匹配

    /** 题目: Fixed Partition Memory Management UVALive - 2238 链接:https://vjudge.net/problem/UVALive-2238 ...

  6. An SPI class of type org.apache.lucene.codecs.PostingsFormat with name 'Lucene50' does not exist. You need to add the corresponding JAR file supporting this SPI to your classpath. The current classp

    背景介绍: 当ES中guava库与hive等组件的库冲突时,对Elasticsearch库进行shade,relocate解决库冲突问题. 当使用"org.apache.maven.plug ...

  7. ZMQ通信模式

    本文简要介绍ZMQ常用的通信模式 请求响应模式 常规搭配:ZMQ_REQ + ZMQ_REP 带输入负载(Round Robin)均衡搭配:ZMQ_REQ + ZMQ_ROUTER 消息分发搭配:ZM ...

  8. vim 脚本——插件

    :help usr_41.txt 查看vim默认可添加插件的路径 :set runtimepath? 查看vim系统插件与脚本位置 :echo $VIMRUNTIME :echo $VIM 查看所有插 ...

  9. 总结一下搭建简单Web服务器的一些方法

    使用nodejs+anywhere模块搭建静态文件服务器 anywhere随时随地将你的当前目录变成一个静态文件服务器的根目录. 安装npm install anywhere -g,然后进入任意目录在 ...

  10. [Stats385] Lecture 04: Convnets from Probabilistic Perspective

    本篇围绕“深度渲染混合模型”展开. Lecture slices Lecture video Reading list A Probabilistic Framework for Deep Learn ...