在线时间:8:00-16:00
迪恩网络APP
随时随地掌握行业动态
扫描二维码
关注迪恩网络微信公众号
前言虽然在.Net Framework 中我们不必考虑内在管理和垃圾回收(GC),但是为了优化应用程序性能我们始终需要了解内存管理和垃圾回收(GC)。另外,了解内存管理可以帮助我们理解在每一个程序中定义的每一个变量是怎样工作的。
简介这篇文章我们将介绍一些方法参数传递行为在堆与栈中的影响。前几节我们介绍了堆与栈的基本工作原理,程序执行时值类型与引用类型在堆栈中的存储。另外,我们已经介绍了一些关于指针的基本知识。这一节中参数传递对堆栈的影响很重要,下面会慢慢道来。
参数,大画面下面是当代码运行时会产生的一个详细过程。上几节已经介绍过当一个方法被调用时会产生的基本情况,让我们来看一下更加详细的内容。
当我们调用一个方法时会发生以下情形:
代码:
栈像下图所示: 注意:ReturnValue方法不会存在栈上,图中把ReturnValue作为此栈结构的开始只是为了解释栈原理。
像前几节介绍的,值类型和引用类型在栈里的存储是不同的。栈为任何值类型创建副本,栈也为任何引用类型的指针创建副本。
值类型传递下面是值类型传递在栈里的内幕。
首先,当我们传递一个值类型变量时,栈会为它分配一块内存空间并把值类型变量的值存储进去。看下面的代码:
当代码执行时,栈为x分配一块内存空间并存储值5 然后,AddFive()被放到栈上,同时栈分配内存空间给参数pValue并复制x的值给它。
当AddFive()执行完成,线程被传递回Go()。同时因为AddFive()执行完,它的参数pValue也实质上被移除。
所以结果是5是合理的。关键点是任何被传递的值类型参数仅是一个碳复制,因为我们希望保护原始变量的值。
有一点要记住的是,如果我们有一个非常庞大的值类型(如,庞大的struct类型)传递到栈里,当处理器循环复制它并循环占有栈空间时将会非常耗资源。栈没有无限的空间去使用,就像用水杯不断的接水早晚会溢出一样。Struct类型可以变得非常庞大,我们要小心并清醒的使用它。
下面是一个比较大的struct结构类型:
让我们看看执行下面代码Go()方法时再到DoSomething()方法会发生的情况:
这可能会非常低效。想像一下如果我们传递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声明时所在的内存地址。
前言虽然在.Net Framework 中我们不必考虑内在管理和垃圾回收(GC),但是为了优化应用程序性能我们始终需要了解内存管理和垃圾回收(GC)。另外,了解内存管理可以帮助我们理解在每一个程序中定义的每一个变量是怎样工作的。
简介继续上篇未完成的“参数传递对堆栈的影响”。
引用类型传递传递引用类型跟上一节所示例中用引用的方式传递值类型相似。
如果使用引用类型(原文可能笔误,写的是值类型):
然后调用Go()方法,MyInt会被放到堆里因为它是一个引用类型。
如果执行下面代码中的Go():
会发生这种情况:
因此,我们用pValue改变MyInt的MyValue的值时,x最终也会获得这个改变的值"12345“。
如果我们用引用的方式传递一个引用类型变量呢?
用引用的方式传递引用类型我们有一个类Thing, 类Animal和Vegetables衍生于Thing:
执行下面的Go()方法:
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. 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:
The code: public int AddFive(int pValue) 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. 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.
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.
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...
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 Let's take a look at what's happening:
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. |
2023-10-27
2022-08-15
2022-08-17
2022-09-23
2022-08-13
请发表评论