简体   繁体   English

C#基础知识-内存管理

[英]C# basics - Memory Management

I am new to the programming in C#. 我是C#编程的新手。

Can anyone please tell me memory management about C#? 谁能告诉我有关C#的内存管理?

Class Student
{

     int Id;
     String Name;
     Double Marks;

     public string getStudentName()
     {
         return this.Name;
     } 

     public double getPersantage()
     {
         return this.Marks * 100 / 500;
     } 
}

I want to know how much memory is allocated for instance of this class? 我想知道为此类的实例分配了多少内存?

What about methods? 那方法呢? Where they are allocated? 它们在哪里分配?

And if there are static methods, what about their storage? 如果有静态方法,那么它们的存储又如何呢?

Can anyone please briefly explain this to me? 谁能简要向我解释一下?

An instance of the class itself will take up 24 bytes on a 32-bit CLR: 类本身的实例在32位CLR上将占用24个字节:

  • 8 bytes of object overhead (sync block and type pointer) 8个字节的对象开销(同步块和类型指针)
  • 4 bytes for the int int的4个字节
  • 4 bytes for the string reference 4个字节作为字符串参考
  • 8 bytes for the double 双倍8字节

Note that the memory for the string itself is in addition to that - but many objects could share references to the same string, for example. 请注意,字符串本身的内存是该内存的补充-例如,许多对象可以共享对同一字符串的引用。

Methods don't incur the same sort of storage penalty is fields. 方法不会产生相同类型的存储惩罚字段。 Essentially they're associated with the type rather than an instance of the type, but there's the IL version and the JIT-compiled code to consider. 从本质上讲,它们与类型关联,而不是与类型实例关联,但是要考虑IL版本和JIT编译的代码。 However, usually you can ignore this in my experience. 但是,根据我的经验,通常您可以忽略这一点。 You'd have to have a large amount of code and very few instances for the memory taken up by the code to be significant compared with the data. 与数据相比,您必须拥有大量的代码和很少的实例,这样代码所占用的内存才有意义。 The important thing is that you don't get a separate copy of each method for each instance. 重要的是,您不会为每个实例单独获得每个方法的副本。

EDIT: Note that you happened to pick a relatively easy case. 编辑:请注意,您碰巧选择了一个相对简单的案例。 In situations where you've got fields of logically smaller sizes (eg short or byte fields) the CLR chooses how to lay out the object in memory, such that values which require memory alignment (being on a word boundary) are laid out appropriately, but possibly backing other ones - so 4 byte fields could end up taking 4 bytes, or they could take 16 if the CLR decides to align each of them separately. 在逻辑上较小的字段(例如, short字段或byte字段)的情况下,CLR选择如何在内存中布置对象,以便适当地布置需要内存对齐的值(在字边界上),但可能支持其他字段-因此4个字节的字段可能最终占用4个字节,或者,如果CLR决定单独对齐每个字节,则它们可能占用16个字节。 I think that's implementation-specific, but it's possible that the CLI spec dictates the exact approach taken. 认为这是特定于实现的,但是CLI规范可能会指示所采用的确切方法。

As, I think Jon Skeet is saying, it depends on a lot of factors, and not easily measurable ahead of time. 就像,我认为Jon Skeet在说的,这取决于很多因素,而且不容易提前进行测量。 Factors such as whether it's running on a 64 bit OS or 32 bit OS must be taken into account, and whether you are running a debug or release version come into play. 必须考虑它是在64位OS还是32位OS上运行,以及是否正在运行调试版本或发行版等因素。 The amount of memory taken up by code depends on the processor that the JITTER compiles to, as different optimizations can be used for different processors. 代码占用的内存量取决于JITTER编译到的处理器,因为不同的优化可用于不同的处理器。

Not really answer, just for fun. 并不是真正的答案,只是为了好玩。

struct Student
{
    int Id;
    [MarshalAs(UnmanagedType.LPStr)]
    String Name; 
    Double Marks; 
    public string getStudentName()
    {
        return this.Name;
    }      
    public double getPersantage()
    {
        return this.Marks * 100 / 500;
    }
}

And

      Console.WriteLine(Marshal.SizeOf(typeof(Student)));

On 64bit return: 在64位返回:

      24

And on 32 bit: 并在32位上:

      16
sizeof(getPersantage());

a good way to find out bytes for it. 找出字节的好方法。 not too havent done much C#, but better with an answer than no answer :=) 不太没做太多的C#,但是有答案总比没有答案好:=)

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

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