C # When will the class allocate memory?

Source: Internet
Author: User

 

I always thought that the memory allocation was when the constructor was called. I am wrong now.

 

I want to take notes when I was reading the value assignment operation of C ++ yesterday. When a class is assigned to another class, the constructor is called to assign values to each member variable. static member variables do not need to be assigned values because they belong to the class and are shared by all instances of the class, therefore, you do not need to be assigned a value. The method belongs to a class and cannot be assigned a value.

 

Suddenly, you don't need to call the constructor when calling a static property or method, because calling the constructor only generates a new instance. Does the static property or method belong to a class, when I call them, I don't need to generate instances at all. I suddenly want to scream, how to allocate memory without calling constructor, where the memory class attributes are stored, and how to access them without even memory.

 

So I guess the constructor has been allocated before it is called. The constructor only performs initialization.

 

The following is my test:

 

We can see that before calling the constructor, the member variables have been initialized, a = 1, indicating that they already have their memory, otherwise a = 1, s = 1 in 1 where to save, do not tell me that no memory is needed. Call the constructor to initialize non-static member variables. No constructor is called in the Process of calling the static method PrintStatic. We all know that you do not need to call constructor. In a static method, only static methods and attributes can be called. The attribute definition and initial value are all before the constructor is called, everything indicates that the property has been allocated memory before calling the constructor, so do not say: it is allocated when the constructor is called.

 

What is the memory occupied by a class? In my opinion:

 

The memory occupied by a class is equal to = total attribute memory + total Method Memory + Memory occupied by the pointer of the class applied + size of the synchronized index block.

 

The attribute must occupy the memory. As for the method, it should also occupy the memory. I don't know the first address of the method's memory and how to access it. Except for the memory where the method is located, the other three must occupy the memory. Is there any way in the class? Therefore, assigning the basic type to the object type (packing) will allocate a lot of memory.

 

These memory compilers are very clear, so you can allocate the memory before calling the constructor.

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.