简体   繁体   中英

.NET Object Executable Instructions Memory Consumption?

Is an object's executable code stored once for all instances or once for each instance?

In other words, is an object's code stored once and reused or does each instance have its own copy?

The code is shared, as is the static data.

Only the parts of the class that are specific to an instance are kept separate.

Here's a little more detail, taken from http://msdn.microsoft.com/en-us/magazine/cc188793.aspx

"Whenever an object is created in the heap, each object gets two additional overhead fields associated with it. The first overhead field, the MethodTablePointer, contains the memory address to the type's method table. Basically, this pointer makes it possible to obtain the type information about any object in the heap. In fact, when you call System.Object's GetType method internally, this method follows the object's MethodTablePointer field to determine what type the object is."

So your instance doesn't replicate any of this shared type data/code. Instead, it has a pointer to it.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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