I was wondering how much memory does an object that inherits from "object" and has no fields/properties take ? And I guess methods don't. Right ? I am talking for .net objects.
相关问题
- how to define constructor for Python's new Nam
- Generic Generics in Managed C++
- Keeping track of variable instances
- What uses more memory in c++? An 2 ints or 2 funct
- How to Debug/Register a Permanent WMI Event Which
The only overhead that you would incur with a reference type would be 4 bytes for the type object pointer and 4 bytes for the sync block index.
So in total, 8 bytes of overhead.
Okay, as both Andrew and Guffa have given answers which I believe to be wrong...
There's an 8 byte overhead for all objects (on x86), but there's also a minimum size of 12 bytes. I don't know why... but it means that these two classes both take 12 bytes per instance:
Test:
This is ugly because I've deliberately not gone for any generic types or anything else that could cause issues. A few test runs:
(JITting overhead etc explains why the number isn't always an exact integer - hence why I do the division in floating point.)
Testing with an extra int field shows the usage going up to 16, which proves it is actually doing something sensible :)
An object has two references/pointers additional to it's own data.
So, on a 32 bit system the object would take 8 bytes, on a 64 bit system it would take 16 bytes.
Correction:
As Jon stated, the minimum size for an object is 12 bytes. The information that I found so far says that the GC requires this.