C # Basics of memory allocation

Source: Internet
Author: User
Tags pears
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.

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 ');        }    }

View Code


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 that it's not going to look back, it won't go to the student method table

Public  abstract class person    {        public  int Id;        public void Eat ()        {            Console.WriteLine ("Eating Pear");        }        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 ();    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 ("Student  is Walking");        }        public override void Run ()        {            Console.WriteLine ("Student  is Running");        }    }

3. Point to the Sun class object

Now I'll add a subclass of student James, which I've already known in the previous example, is only called by the Override keyword override, so I'm going to 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!

public abstract class Person {public int Id;        public virtual void Eat () {Console.WriteLine ("In the Eating Pear");        } public virtual void Walk () {Console.WriteLine ("In A Walk");        The//abstract method can only be declared in an abstract class, so it must be abstract before person and can only be declared and must be implemented in subclasses.        public abstract void Run (); public virtual void Sayhi () {Console.WriteLine ("man said: Hello!        ");        }}public class Student:person {public int studentid;        public virtual void Eat () {Console.WriteLine ("Students are eating pears");        } public override void Walk () {Console.WriteLine ("Student is Walking");        } public override void Run () {Console.WriteLine ("Student is running");        }}public class James:student {public string name;        public override void Eat () {Console.WriteLine ("James is eating pears"); } public override void Walk () {Console.WriteLine("James is Walking");        } public override void Run () {Console.WriteLine ("James is running"); } public override void Sayhi () {Console.WriteLine ("James says: Hello!        "); }    }

The above is the C # basic memory allocation content, more relevant content please pay attention to topic.alibabacloud.com (www.php.cn)!

  • Related Article

    Contact Us

    The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

    If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

    A Free Trial That Lets You Build Big!

    Start building with 50+ products and up to 12 months usage for Elastic Compute Service

    • Sales Support

      1 on 1 presale consultation

    • After-Sales Support

      24/7 Technical Support 6 Free Tickets per Quarter Faster Response

    • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.