There is always a small doubt -- How much memory does. NET occupy for an object or set? Is there a fast way to get it, instead of simply estimating the size of the analysis object?
After checking MSDN and some other people's analysis, the solution is that the size of the managed code object cannot be truly obtained because the managed object is converted into an unmanaged CLR optimization. But I still don't have to worry about it! Okay, I finally got some results.
The sizeof keyword is used to obtain the size of the value type, such as int (4 bytes) and char (2 bytes). the sizeof operator is only applicable to the value type, not the reference type. Therefore, the keyword for object modification cannot be used.
Check msdn and find the object Marshal. the SizeOf method is interpreted as the unmanaged size of the returned class, but it still cannot be used because it targets unmanaged code. If you directly use an object of the class, an error is reported: "It cannot be used as an unmanaged structure for sending and sending; it cannot calculate a meaningful size or offset. "; This is because the actual size of the object in the memory is not fixed. It is possible to run a value this time. Next time it is another value, there is optimization!
Therefore, to fix the size of an object, the CLR must be told not to optimize the size of the object in the memory. In this case, you need to add the [StructLayout (LayoutKind. Sequential)] feature when declaring the class;
StructLayoutAttribute allows you to control the physical layout of data fields of a class or structure.
LayoutKind enumeration controls the layout of objects when exported to unmanaged code.
Member name |
Description |
Sequential |
The object members are arranged in sequence when they are exported to the unmanaged memory. These members are laid out based on the encapsulation specified in StructLayoutAttribute... Pack and may be discontinuous. |
Explicit |
The precise position of each member of an object in the unmanaged memory is explicitly controlled. Each member must use FieldOffsetAttribute to indicate the position of the field in the type. |
Auto |
The runtime automatically selects an appropriate layout for the members of objects in the unmanaged memory. Objects defined using this enumeration member cannot be exposed outside the hosted code. An exception occurs when you try this operation. |
This means that the memory usage is fixed when the control object is converted to unmanaged code, so that we can use the Marshal. SizeOf method comfortably. Get the size of an object. If an object contains another object, you can use this [StructLayout (LayoutKind. sequential)] can also not be used, if used, is, the size of the object plus the size of this object; if this parameter is not used, the object size is added with four bytes (that is, the reference size of this object ).
When we want to get the size of a set, we cannot add this attribute to the List. Because of the classes in Microsoft dll, how can we get the corresponding size? In fact, a simple way is to convert an object or set into a memory stream, and then get the size of the memory stream. Although there are some other influences, however, you can roughly estimate the memory size of an object or compare the size of two objects and sets!
Main Code:
BinaryFormatter binaryFormatter = = , SeekOrigin.Begin);
In fact, there are many things in it. I just learned a little bit and shared it. Let's discuss it together. If there are any errors, please criticize and point out!