简体   繁体   English

C# .Net 中的方法下的内存分配

[英]Memory allocation under Methods in C# .Net

I'm still learning C# and I'm under difficult case understanding memory allocation in methods.我仍在学习 C# 并且我在理解方法中的内存分配方面遇到了困难。 Let's imagine the situation that I got some reference object, and in a method I'm assigning existing object, what will be done in memory?让我们想象一下我得到了一些引用对象的情况,并且在我分配现有对象的方法中,将在内存中做什么?

I found Are instance methods duplicated in memory for each object?我发现每个对象的实例方法是否在内存中重复? but it is not so clear for me in case that I describe.但在我描述的情况下,对我来说并不是那么清楚。 Any other references will be very much appreciated.任何其他参考将不胜感激。

public class ClassToBeAssigned : IClassToBeAssigned {}
public interface IClassToBeAssigned{}

public class AllocatingClass
{
  private ClassToBeAssigned testAssigment;

  // Just as example
  void Main()
  {
       // new allocation in memory
       testAssigment = new ClassToBeAssigned();
       Assign(testAssigment);
  }

  // create here copy of context by assigned
  void Assign(IClassToBeAssigned assigned)
  {
       // What will happend now if there are 4x method calls ?
       DoSomething(assigned);
       DoSomething(assigned);
       DoSomething(assigned);
       DoSomething(assigned);
  }
  void DoSomething(IClassToBeAssigned assignIt)
  {
       // What is happening here in memory allocation for that reference each call ?
       IClassToBeAssigned dealWithIt = assignIt;
  }
}

I just got a bit confused about what is happening there, also I found lots of information, but nothing for this specific problem.我只是对那里发生的事情有点困惑,我也找到了很多信息,但没有针对这个特定问题。

Assigning a reference doesn't create a copy in C#.分配引用不会在 C# 中创建副本。 You only create a new instance in Main , so there's only one instance of ClassToBeAssigned .您只需在Main创建一个新实例,因此只有一个ClassToBeAssigned实例。

The exception is with value types;值类型除外; these need special care.这些需要特别照顾。 If ClassToBeAssigned was a struct rather than a class , each call of Assign(testAssignment);如果ClassToBeAssignedstruct而不是class ,则每次调用Assign(testAssignment); would in fact create a new instance that is a copy of testAssignment .实际上会创建一个新实例,它是testAssignment的副本。 Note that even in this case, DoSomething(assignment);请注意,即使在这种情况下, DoSomething(assignment); will not result in a new copy.不会产生新的副本。 If you want to understand more about this topic, read up on boxing in C#.如果您想了解有关此主题的更多信息,请阅读 C# 中的拳击。

As for DoSomething , it does absolutely nothing, so it's not clear what you would expect to happen there :)至于DoSomething ,它绝对什么都不做,所以不清楚你会在那里发生什么:)

DoSomething would allocate some bytes from the stack for the dealWithIt variable, so depending on the system that would be 32 or 64 bits it allocates, since dealWithIt is just a reference. DoSomething会从堆栈中为dealWithIt变量分配一些字节,因此根据系统,它会分配 32 位或 64 位,因为dealWithIt只是一个引用。
The reference from assignIt would then be copied to that allocated memory, to then be immediately be released again, since the method is done and the stack moves back to the previous method.来自assignIt的引用将被复制到分配的内存中,然后立即再次释放,因为该方法已完成并且堆栈移回前一个方法。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM