当前位置:网站首页>Memory introduction

Memory introduction

2022-06-24 13:23:00 Chen Bucheng I

One . Memory

Physical memory is also called main memory , Most computers use dynamic random access memory (DRAM). Only the kernel has direct access to physical memory .

Linux The kernel provides each process with an independent virtual address space , And the address space is continuous . such , Processes can easily access memory , More specifically, access to virtual memory .

It can be seen from here that ,32 The kernel space of bit system takes up 1G, At the top , The rest 3G It's user space . and 64 The kernel space and user space of a bit system are both 128T, Occupy the highest and lowest memory space respectively , The rest of the middle is undefined .

When the process is in user mode , Can only access user space memory ; Only after entering kernel state , To access kernel space memory . Although the address space of each process contains the kernel space , But these kernel spaces , In fact, they are all related to the same physical memory . such , After the process switches to kernel mode , You can easily access kernel space memory .

Each process has the entire virtual space available , But physical memory is allocated only when it is actually applied . Allocated physical memory , It's managed by memory mapping . That adds up to the virtual memory of all processes , Much larger than the actual physical memory .

Memory mapping , It's actually mapping the virtual memory address to the physical memory address . To complete the memory mapping , The kernel maintains a page table for each process , Record the mapping relationship between virtual address and physical address , As shown in the figure below :

Page tables are actually stored in CPU Memory management unit for MMU in , such , Under normal circumstances , The processor can go directly through the hardware , Find the memory to access .

When the virtual address accessed by the process cannot be found in the page table , The system will generate a page missing exception , Go into kernel space and allocate physical memory 、 Update process page table , Finally, return to user space , Resume the operation of the process .

Two . Memory footprint

The basic idea of virtual memory is : Program 、 The total size of the data and stack may exceed the size of the available physical memory . The operating system keeps those parts of the program currently in use in main memory , Save the rest on disk . for example , For one 16MB The program , By carefully choosing where to go at each moment 4MB The contents remain in memory , And exchange program fragments between memory and disk when necessary , In this way, the program can be in a 4MB Running on the machine .

The smallest unit of memory is bytes 1M=1024byte( byte ) 1 byte =8bit( The bit ) So the maximum storage value of a byte is 255, Namely 8 individual 1

How many bytes does it take , And interpreter version , System version related . Numbers 4 byte Variable =100 1 Bytes can be stored directly 100 Variable =100( character string ) One character takes one byte Man occupied 2 Byte representation

The variables here , It will occupy memory , But not much , One int Variables of type account for 4 Bytes , A byte is 8 individual bit, Every bit It can only be stored in the 0 or 1

therefore int Type of 4 A total of... Bytes 4*8=32 bit

The biggest expression is 1111111111…..(32 individual 1)

Convert to 10 process ,, Namely int Of a type used in actual calculations 2 Of 32 The next power

If the process is complicated , There are many variables defined , It takes up a lot of memory space

From the memory model , The simplest heap and stack

Some variables are on the stack , Some variables are in the heap . After calculation ( Program end ), Variable destruction , Memory space recycling , So you can see that your memory is down again .

原网站

版权声明
本文为[Chen Bucheng I]所创,转载请带上原文链接,感谢
https://yzsam.com/2021/05/20210524135807594F.html