C#.Net对象内存模型及堆/栈数据结构详解 (二)
C#.Net对象内存模型及堆/栈数据结构详解 (二)
C# Heap(ing) Vs Stack(ing) in .NET: Part II 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.
参考文档:
将SQLServer数据类型转换为C#.Net类型 Delphi程序调用C#.Net编译的DLL并打开窗体(详解) C#.Net C/S结构开发框架中数据访问层(DAL)的作用 C#.Net 静态构造器使用详解 C#.Net对象内存模型及堆/栈数据结构详解 (一) C#.Net对象内存模型及堆/栈数据结构详解 (三) C#.Net对象内存模型及堆/栈数据结构详解 (四) C#.Net COM交操作性 - 强类型RCW和弱类型CCW详解 标签:C#.Net组件开发 - 自定义设计器持久化对象的属性 C#.NET C/S结构版本自动升级解决方案2.0详解 (一) C#.NET C/S结构版本自动升级解决方案之升级策略 C# ASP.NET WebApi服务器搭建详解 - IIS服务承载(IIS Hosting IIS宿主) C#数据转换类ConvertEx,封装.Net的Convert对象 CSFramework代码生成器根据数据库表结构生成实体对象模型(C#代码) C# FastReport.NET批量打印条形码报表详解教程
其它资料:
什么是C/S结构? | C/S框架核心组成部分 | C/S框架-WebService部署图 | C/S框架-权限管理 | C/S结构系统框架 - 5.1旗舰版介绍 | C/S结构系统框架 - 功能介绍 | C/S结构系统框架 - 产品列表 | C/S结构系统框架 - 应用展示(图) | 三层体系架构详解 | C/S架构轻量级快速开发框架 | C/S框架网客户案例 | WebApi快速开发框架 | C/S框架代码生成器 | 用户授权注册软件系统 | 版本自动升级软件 | 数据库底层应用框架 | CSFramework.CMS内容管理系统 | |