Chapter 4 basics-5th. Process and memory

Source: Internet
Author: User

5.4. Process and memory

When talking about the "library" file, the "function" of the dynamic library has a "function address ". In fact, not only do functions have addresses, but all kinds of data in programs have addresses.

5.4.1. What is a process?

When the program is quietly lying on the hard disk, it is a "File". If you have to make a difference, it is an "Executable File". When the program runs, it jumps from the hard disk into the memory, then it is called a "process/process.

[Classroom assignments]: Learning to observe the process through the "Task Manager"

Press CTRL + ALT + DEL at the same time, or right-click in the blank area of the taskbar, select "Task Manager" in the pop-up menu, and switch to the "process" page, we can see some indicators of each process. Tip: click "View-> Select column" to configure more observation options. The following is an example:

Figure 5-6 view the process through the "Task Manager"

 

5.4.2. process memory space

Files on the hard disk are "dead", and memory processes are "active". It can be said that memory is the world of program survival. How big is the world of every process? The answer is "4G ".

What is "4G? Generally, programmers are used to thinking about the memory size in hexadecimal notation. But we haven't learned the hexadecimal notation yet. Let's use the hexadecimal notation to answer the question: the size of 4G is 4294967295.

Maybe some readers want to open the chassis: obviously only 1 GB memory is inserted, or even only 512 MB memory? How does the operating system allocate 4 GB of memory for each process?

In the "Code-to-program" section at the front of this chapter, we have already said that the program runs on top of the operating system and accesses the hardware through the operating system with a fake hand. Memory is the most important hardware. The old operating system does allow us to directly manipulate the memory, but modern operating systems do not. In this way, the operating system has the opportunity to be tricky-of course, it is supported by hardware.

First, the preparation is not the actual memory space size, but the "memory address" size, more strictly speaking, is the memory "virtual address" (the real memory address is called "physical address ").

If we think of the smallest unit of memory space as a grid. The memory address is the Checkpoint number. Here, 4 GB refers to 4 GB memory "virtual address"-simply put, each process can obtain 4 GB of room numbers, rather than 4 GB of real memory space.

There are few houses, but there are many street cards. Where will these cards end up? Answer: some of them are mounted to real physical addresses, and some may be mounted to "virtual memory (hard disk space)". A large part is not mounted anywhere, the entire process is white lines opened by the operating system.

"Virtual Memory" has little to do with "virtual address. The latter refers to the memory address allocated by the operating system to each process from "0" to "4294967295. The former means that the operating system often uses the hard disk space to pretend to be a memory space to cheat the process.

 

Classroom assignment: Observe the virtual memory configuration of the Local Machine

Right-click "my computer" and select "properties" from the pop-up menu. In the "System Properties" dialog box that appears, switch to the "advanced" Page. Find the "performance" group box, click the "Settings" button in it, and the "performance options" dialog box appears. Switch to the "advanced" page. Find the "virtual memory" configuration above.

 

We will unveil several details about 4G memory space:

First, "the memory is not enough, and the hard disk is used together ". However, please note that the access speed to the real memory is in the "nanoseconds (one in 1‰ S)/ns" level, but the access speed to the hard disk is in the "Millisecond (s)/MS" level.

 

[Important]: Program Design Principles: Remove unnecessary file read/write operations

The program needs to read and write (physical) files normally, but if a program needs to read and write files very frequently, you need to consider whether there is a problem with its design philosophy.

For example, to read the configuration file, it is generally designed to read the memory during the startup of the program, and then continue to use the memory data. If the configuration file is modified externally during the running of the program, you should set it to a "notification" mechanism. That is, after the modification is made externally, the program can receive a notification and re-read the file, instead, it should not be designed to read from the file every time configuration is required (configuration data is often frequently used ).

 

Second, although the commitment is 4 GB, it is actually a pay-as-you-go distribution process. The vast majority of processes require a maximum of several hundred megabytes. The virtual memory address (house number) allocated to each process by the operating system is 4 GB, instead of providing 4 GB memory directly (in that case, the system crashes directly ).

3. In 4G, 2 GB is also needed to reserve and share with its operating system and other processes. In this part of memory, the current process does not have access permissions by default.

Fourth, the last one is very important: who is responsible for the conversion of memory "virtual addresses" and "Physical addresses? Is the hardware, mainly the CPU. Physical memory is a battle for the military-at least for the next decade or not-the read/write memory must be very fast. This requires that the software (including the operating system) cannot be implemented and can only be handed over to the hardware. What the operating system does is to shield most applications from Directly Reading and Writing physical memory permissions.

 

Will it make readers despise the 4G space? And Mo!

The memory access mode through the "virtual address" is called the "protection mode". The corresponding mode allows the program to directly access the physical memory, which is called the "real mode ". "Real mode" has many problems.

For example, the entire operating system, or even the entire machine, goes down when a program does not affect the physical memory.

For another example, if there is no virtual memory implemented by the system, the program must handle the temporarily unused data, store the data on the hard disk, and read the data as needed, it is so annoying for programmers. With 4g space, it is much more convenient for programmers to write 6 programs in one breath and face-to-face!

5.4.3. Memory Allocation Test Program

Next, we will write a program to observe how much memory a C ++ program can fetch from the operating system. This is a very small and inaccurate program. Only roughly tests the program running period, and dynamically allocates the amount of memory to it. The memory occupied by the program itself is not counted.

Open code: blocks and create a console project named "memoryalloctest ". Open the default main. cpp file in the project and complete the following code:

# Include <iostream> using namespace STD; int main () {007 unsigned int bytes = 0; 009 while (true) {011 try {013 new char [1024*4]; // 4 K memory allocated each time 014 bytes + = 1024*4;} 016 catch (STD: bad_alloc const & E) {018 STD: cout <E. what () <STD: Endl; 019 break;} 023 STD: cout <bytes <"bytes" <STD: Endl; return 0 ;}

Danger: a greedy program for memory!

Don't rush to compile and run the program. Because it will do its best to swallow your memory (physical or virtual) until the operating system rejects it. In this process, your computer will become unresponsive, and many other processes will become unable to respond in a timely manner, and some processes may even make mistakes. Therefore, before executing this program, please save any content in your modifications and close unnecessary programs as much as possible.

 

The minimum unit of memory is byte ". In row 007, we define a variable named "positive integer/unsigned int" and we will name it bytes. This variable is used to record how many bytes of memory are allocated. Now, it is initialized to 0.

Line is a familiar "endless loop". Since it is an endless loop, we usually have to have an exit to save it: In line 019, we see a break.

When will this break take effect? Here we want to meet new friends: exception. See the 011 and 016 rows to form such a statement structure.

011 try {// code block-1} 016 catch (STD: bad_alloc const & E) {// code block-2}

Code in the range of try {} (code block-1). If an exception defined by C ++ occurs, the program jumps to catch () code in the range of {} (code block-2) continues to be executed.

Exception is a structured jump of program flows supported by C ++. We will learn later. What will happen in this example is:

The 013 lines of code will be allocated again and again in the loop (and will not be released later). Once, this greedy behavior will fail. C ++ specifies that when memory allocation fails, the default action is to throw a C ++ exception data, stop the code, and then jump to a catch code block, in this example, the rows are 018 and 019.

A brief description of the exceptions captured by 018 rows of output. The 019 line is the important "break". Yes, the program memory is exhausted and the loop cannot proceed. At this time, it is not break, and when will it be more?

The following is the result of the program running on my computer. It took more than 6 minutes to allocate nearly 2 GB of memory.

Figure 5-7 Test Results of C ++ program memory allocation

 

Readers may be interested in how C ++ applies for memory in the above Code. Let's also briefly talk about it.

The "New" command in C ++ can apply to the system for memory allocation and decide to apply for multiple bytes based on the "data type. The size of the "char/character" type in C ++ is exactly 1 byte. Therefore, if the 013 line of code is written as follows:

013 new char;

In each loop step, only one byte is allocated, which makes the test more accurate, but the cost may be 1 hour, therefore, in this example, 4 K bytes are applied each time (1 K is 1024 bytes ). In C ++, You need to allocate continuous memory with multiple char sizes at a time by using brackets, as shown in this example:

New char [N];

N indicates the number of instances to be allocated.

Tip: memory size unit

The minimum memory unit is byte/byte.

1 K: 10 to the power of 2, 1024 bytes.

1 m (MB): 2 to the power of 20, 1024*1024 bytes.

1G: 2 to the power of 30: 1024*1024*1024 bytes.

4 GB of memory address, that is, the 32 power of 2 (1024*1024*1024*4 ).

 

5.4.4.32-bit Machine

Now, the last question in this section is: Why do the system prepare 4G bytes for each process, instead of more or less?

The answer process is as follows: we usually use 32-bit computers. These 32 bits correspond to 32 circuits on the hardware. These 32 circuits are either powered on or powered off, which is equivalent to binary, namely 32 1 or 0.

The 32-bit memory address is used to express the memory address. The maximum number is a 32-bit binary number with full 1, that is, the 32 power of 2, which is exactly 4 GB.

On which day we will use all 64-bit machines. The 4G value should be changed to the 64-power of 4 GB: 4 GB!

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.