C # Basics of memory allocation
1. Create an Object
The process of creating an object is divided into two parts: memory allocation and initialization. There are three main parts of the memory area that the CLR manages in. NET: Stack, GC heap, Loh Heap, and the stack is used primarily to allocate value type data. Its management is system-controlled, rather than GC-controlled, as is the case with GC heaps. When the thread executes the method of the value type instance, the space will be automatically freed, and the execution of the general stack is much more efficient than the capacity is limited. The GC heap is used to allocate small object instances, which are fully controlled by the GC for allocation and recycling of memory. The Loh heap is prepared for large object instances, which are not compressed and are recycled only when the GC is fully reclaimed. In IL, you can see newobj, ldstr (Creating a String object), Newarr (for assigning new array objects), box (boxing), and other common instructions for creating objects. Of course, there are also value types on the heap, such as when a value type is a field of a class, it stores the instance object space in the heap, and when it is boxed, it also causes the value type to exist on the heap. Okay, let's take a look. Create an object's memory allocation, now there is a person class and a student class. So this sentence Student s = new Student () {StudentID = 2, Id = 4}, the S object is created after execution, and below I draw a graph to illustrate the allocation of memory when creating an object, where S object also has synchronous index block and type object pointer I didn't draw it.
View Code
public class Person
{
public int Id;
public void Eat ()
{
Console.WriteLine ("Eat Pear");
}
}
public class Student:person
{
public int StudentID;
public void Gotoschool ()
{
Console.WriteLine ("Go to School");
}
}
2. Parent class object pointing to Child class
We use the parent object to point to subclasses in order to implement polymorphism when we write programs. So when I write to Person p=new Student (), a subclass object is created in the heap, and the following is a memory allocation diagram of the parent class object that points to the child class. I added a virtual method and an abstract method to the person, and overridden the method in the student subclass. It can be seen that once the subclass overrides the virtual or abstract method of the parent class, the 2 methods in the Person method table will be covered by the quilt class, which we can implement polymorphic. There is also a new void Eat () method in the Student method table, but it cannot be called by P because the new Eat () at this time belongs to a subclass. In other words, except for overridden methods, p can only invoke methods in the person method table, and if not found, continues to look for the method of the person parent class until object. Note is not looking back, it does not go to the Student method table to find the method.
View Code
public abstract class Person
{
public int Id;
public void Eat ()
{
Console.WriteLine ("Eating Pears");
}
public virtual void Walk ()
{
Console.WriteLine ("In A Walk");
}
An abstract method can only be declared in an abstract class, so it must be abstract before a person and can only be declared and must be implemented in a subclass.
public abstract void Run ();
}
View Code
public class Student:person
{
public int StudentID;
public void Gotoschool ()
{
Console.WriteLine ("Go to School");
}
Public new void Eat ()
{
Console.WriteLine ("Students Eat apples");
}
public override void Walk ()
{
Console.WriteLine ("The Student is Walking");
}
public override void Run ()
{
Console.WriteLine ("Students are running");
}
}
3. Point to the Sun class object
Now I'm going to add a subclass of student James, which I've learned from the previous example, that only the override keyword overrides the parent class, so I'll remove all the normal methods. The execution code for person p = new James () {name = "James", StudentID = 2, Id = 4}, code and memory allocation diagram below, in order to highlight the point, I have no stooped paragraph. From the results can be seen Sayhi method is finally covered by the sayhi of the Sun class, from here can see the transitive nature of inheritance!
View CodeView CodeView Code
Category: C #
C # Basics of memory allocation