Welcome to “Algorithms and the Beauty of Programming” ↑ pay attention to us!

This article was first published on the wechat official account “Beauty of Algorithms and Programming”. Welcome to follow and learn more about this series of blogs in time.

“These days computers are slow to open and slow to run. Is there a solution?”

“Go to jingdong to buy a 4G memory bar to add to the computer inside!”

In our life, we often come across such conversations. When our computer running programs become slow and jammed, we will hear friends around us to increase the memory of the computer. Why is that? What role does memory play in computer architecture? When we have four gigabytes of memory, how does the operating system manage it?

This series of blogs will delve deeper into these issues, focusing on the memory management part of operating system principles to give you a better understanding of computers. Strive to use the most simple and understandable language to introduce relevant principles and algorithms, even if you do not have any professional foundation, you can feel the charm of the operating system with us.

Problem definition

Before introducing any technology or theory, we should first understand what the problem is to be solved, that is, the definition of the problem, this is a crucial step. When studying, many students often do not know why I need to learn this knowledge point, what is the relationship between this knowledge point and the previous chapters and other courses, and what problems this knowledge point solves. They simply learn a certain knowledge point, ignoring the definition and relevance of the problem.

Each program needs to occupy memory, each program after the end of the run, will release its occupied memory. That’s the first premise you need to understand.

First, let’s introduce what memory management is. The so-called memory management is simply that you want to run a program, you need to allocate the corresponding memory for this program, so how to allocate the memory?

 

Some of you might say, isn’t that easy? For example, IF I have 4 gigabytes of memory and an application comes in, I give it as much memory as possible. When the application is finished, I reclaim the memory as follows:

Figure 1-1 MEMORY distribution of A1, A2, A3 programs

 

The A1 program at t1 requires 512MB of memory, the A2 program at t2 requires 256MB of memory, and the A3 program at t3 requires 1GB of memory. Allocate memory for each program as shown above.

Figure 1-2 Memory distribution after the A1 program runs

After a period of time, the A1 program finishes running, freeing the memory previously occupied.

Isn’t it simple? What is there to discuss?

 

Is it really that simple? Here are a few questions to ask. How can you solve them?

1) The following figure shows the distribution of memory at a certain time. There are four programs running in memory, A1, A2, A3 and A4. A program called A5 needs 512MB of memory and only 256MB of memory is available. Can A5 be executed?

Figure 1-3 Memory distribution at a certain time

 

2) The following figure shows the memory distribution at a certain time. The system available memory is shown in green below: 256 + 256= 512MB of available memory, but the two pieces are not connected together. A program A5 needs 512MB of memory. Can the program A5 be executed?

Figure 1-4 Memory distribution at a certain time

3) The total memory size is 4GB. Suppose a program needs 6GB of memory, can I run the program?

 

From the above mentioned problems, you can see that memory management is not as simple as you think, it needs to solve a lot of problems.

The following blog will explore how operating systems solve these memory management problems and how the solutions proposed by the masters differ from our own. This section wants you to get a sense of what memory management is all about.

 

Tips:

Due to the limited screen size of the mobile phone, please click the picture to enlarge and view it when reading the picture.

All the green parts in this example represent the memory available to the system, and the other colors represent the memory occupied by a program.