(2) learning C # Memory Management

Source: Internet
Author: User

1. What memory space do you usually access when running your program?

The computer said to himself, "This person wants to declare an integer", "This person's method", or "This person wants to create an object"

1. Where is the information stored in the memory?

2. Where are the information used to describe this integer, these objects and methods?

3. What is the way these memories are organized?

 

2. There are three types of spaces, but two of them are most closely related to us.

1. Type 1 Bucket: used to store all static variables and constants

We can think of them as variable that won't change. This is a specially divided space. It is a special space where static variables and constants are stored, so where is this special space? We are not particularly concerned about it. In short, it is our special little cute.

Because when the program starts running, these things will be placed in the memory, and our constants will never change, this means they do not need to be placed in the memory that we often access. They just need to stay somewhere in the memory and then we know how to access it.

 

2. Type 2 Bucket: used to store dynamic variables

1). What are dynamic variables?

Dynamic variables are the variables applied for using new. So when you apply for an object using new, such as new goval (parameter), a dynamic variable is obtained, the memory corresponding to the dynamic variable, that is, the heap you applied for with new.

2). What is heap?

Heap is like a pile of clothes. When you need clothes, you say, "Hey, I need some new clothes." Then it gives you some clothes, after you finish wearing these clothes, you will say, "Hey, I don't want to wear these clothes any more." Then the magic thing is that the clothes you gave up have become a piece of cloth, the computer knows that you are no longer wearing these clothes. It says, "I want to get these memories back." This is called garbage collection.

The essence of garbage collection: When the memory in the heap is no longer needed, it will be reclaimed.

 

3. Bucket 3: used to store local variables

These local variables are stored in a place called Stack.

What is stack?

It is different from the concept of heap. For the stack, When you access local variables in a method, or when you want to pass some parameters when calling the method, such as these parameters, you actually copied the values of these parameters and allocated another space in the memory to store them, in terms of time, some space will be automatically allocated to you in the stack. These variables are active when you access local variables and the latter transmits parameters or call methods, when these variables are no longer active, such as the end of a method call or the right curly braces, they will disappear and cannot be called in any form, then the stack said, "Hey, I want to get the memory space back ".

4. Introduction to static variables, dynamic variables, and local variables

1) static variables: All static variables, or special variables, are stored in relatively low memory locations. That is to say, the values corresponding to these locations are relatively small, for example, in the 1000 position, note that the 1000 is hexadecimal instead of decimal.

2) Heap: When the heap increases, it increases down. What does it mean to grow bigger? That is to say, when you started from 2000, you said, "Hey heap, I need to prepare some space for my new goval." It said, "Okay, I will allocate some memory for you here, it's okay at the place 2004, "and then you said," I need to prepare some space for my new gwreck. It says, "Okay, 2008 is yours, I will allocate space here, "So when the heap is getting bigger, we will say that the heap is getting bigger and bigger.

3) STACK: the stack starts from some relatively high addresses and starts from FFFF. When it allocates memory, it will increase upwards. That is to say, these address values will be smaller and smaller. Another parameter is used to describe local variables, parameters, and dynamic variables. Of course, it is mainly about dynamic variables. That is, the space they occupy.

4) different variables actually require buckets of different sizes. For example, an integer, int must be at least C #, and Java occupies four bytes. Dynamic variables also have some additional memory overhead, which is actually some additional memory space, which is some space outside the space occupied by integers and characters, used to save other information about your object.

 

3. What happened when the program starts running or when a new object is created ......

Preparation:

1. We can create a point by calling the constructor and tell it the X and Y coordinates. Then it will save the coordinates and you can move them.

2. At the beginning, this class was only available. The memory has not been assigned and nothing has been done yet. We write a program, create a vertex object, and call its corresponding method. Run the program to know what happened in the computer memory.

3. Run:

1) The first thing: Call the run method. When the program starts running, the run method will be called.

When a method is called, we will create these local variables and parameters, but run does not have parameters, so we do not need to arrange space for the parameters, but it does have two local variables P1 and P2, but it does not include any information, because my program has just started and has not run to the new point location.

2) The second thing: Call New to get a dynamic variable, which means it will be stored in the heap.

A. record the data of a vertex. The role of the constructor is to fill in data (2, 3) in the space allocated by the computer ).

B. Overhead contains additional memory overhead.

C. Why write 1000 in P1? What should we do after we allocate space for this new vertex? We allocate this point to P1. Here is a key point, that is, how does the computer know that P1 and all the information it represents are stored there? Each object has an address. In fact, the computer finds these objects through the memory address of the object. So when I call new, I will apply for a space in the heap. Then assign this space to P1. What is assigned? In fact, it is to assign a value to the memory address of P1, specifically the starting address. You can find all the data contained in P1 through this starting address, and the data is stored in stack (1000). Here, 1000 is the memory address 1000.

3) The third thing: to create a new point, you need to use the heap memory.

Heap said, "The address before 1008 has been allocated, so I will allocate you a space that can hold the next vertex to save your instance variables.

Then the computer said, allocate me a space, call the constructor, fill in the data of the vertex in the storage unit, and assign the starting address 100c to P2. Px and Py are instance variables belonging to P1, and the allocated PX and Py are instance variables belonging to P2.

4) The fourth thing: magical events. Call a method, such as p1.move.

A. When we call this method, we will have some parameters and local variables. That is to say, we need to allocate memory on the stack to call the method. So what happens when we call a method is, "Hey, P1, I want to move you so much away (10, 11 ". So what happened in the memory? What happens in the memory is: we will create a new "stack frame". When you call a method, call the method, or the method that was run will be suspended. The internal situation is that we allocate memory space on the stack, so we call it stack frame, which stores the information needed to call this method. What information, first, is the call of the method, additional memory overhead, need to store all the passed parameters.

B. There is a hidden parameter about this. After I call a method for a specific object, How do I know which object the method is to call? When you call a method for an object, the content that is placed on the stack after the so-called memory overhead is actually a pointer to the object. We call it the this pointer. This is a way for objects to access themselves. It needs to know where it is in memory.

C. It's strange, why do objects need to know where they are staying?

Because when we write a method, we do not associate these methods with some specific objects that have been created. If I create a specific object (such as P1) and then call that method on it, the method should know, "Hey, you asked me to change the PX PY value, you are talking about the px py value. "At this time, this will take effect.

This says, "I know the location of the method corresponding to the object in the memory ." In this way, no matter what operations you perform in the program, you can use this to specify the object you want to access.

When calling a method, you must first know the address of the specific object corresponding to the method. We call P1, where P1 is placed at 1000. Therefore, P1 is 1000, and this refers to the value of 1000. The input parameters are stored in reverse order. Input parameter values, which are copies of input parameters.

5) The fifth thing: Let's see what happens when the move method is called? Now the local variables of move are stored in the stack, and the pointer address corresponding to the object.

A. What is the pixel plus 10 in step 5, but what about the PX? Follow the PX found by this pointer.

How to find it?

B. Find the address stored in this bucket. As a result, it reaches the 1000 position and reads additional memory overhead information. Then it knows the position of PX relative to the starting position of 1000. Then it continues, "Hey, it has been found. Its value is 2, and I add 10 to it to 12 ".

6) The sixth thing is interesting. When the variables in the stack are no longer active, they will be deleted.

A. At this time, the computer will automatically run and say, "Hey, this variable, this memory, will no longer be used. I want to reclaim it ." Therefore, the memory in the stack is the stack frame related to the move method. These are the memory allocated by calling move. That is to say, all memory allocated for moving is automatically withdrawn by the machine. This is called "pop-up in stack ". Imagine that when something on the top of the stack is used up, it will pop up from the top of the stack, and then it will be gone.

B. if I call the move method again? The related information will be re-stored on the stack frame, but it is not needed after a move operation.

Note: The difference between the constructor and other methods does not use the this pointer for constructor.

 

(2) learning C # Memory Management

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.