public class MyClass
{
int i = 0;
string str = "here";
MyStruct mystruct;
B b;
ArrayList myList = new ArrayList(10);
public MyClass()
{
}
....
}
public struct MyStruct
{
public int i;
public float f;
}
public class B
{
...
}
Want to learn how an instance of a class is created in the background. When this statement
MyClass myClass = new MyClass();
is evaluated. What will happen in the backgroud? My following statements are correct or not (for 32-bit OS machine)?
- A memory space will be created and referenced as
myClass; - within above memory space, 4 bytes is used for the value of
int i; - within above memory space, 4 bytes is used for the reference of
string str; the actual value of thestris stored in other location (where?) - within above memory space, 8 bytes is used for the value of
MyStruct mystruct(because MyStruct is 8 bytes); - within above memory space, 4 bytes is used for the reference of the
B bobject; memory for b object will be allocated in somewhere else when it is instantiated; - within above memory space, 4 bytes is used for the reference of the
ArrayList myList; actual memory space forArrayList myListis allocated in other place and referenced in here asmyList; - another 4 or 8 bytes from above memory space is used for object metadata;
- …;
You have the basic idea in place. The actual specifics of this, aside from what you include, is implementation specific. However, for a couple of your points:
3) The actual string is typically stored in its own memory space, just like any other class. However, since you’re using a string literal in this case, it’ll most likely be in the string intern pool, which is (I believe) stored in the large object heap. For details on string interning, see String.Intern. (If you allocated the string on the fly, instead of using a literal, the string would be stored in the normal managed heap of your application.)