当前位置:网站首页>On virtual memory and oom in project development
On virtual memory and oom in project development
2022-06-26 03:02:00 【Jerish_ C】
This is a 【 Game development stuff 】 The first 55 Original article
The article is about Windows Part of the content of the platform virtual memory is transferred to the user “ Nanjing ZhouRunfa ”
Memory management is the core and important content of operating system , It is also one of the biggest headaches for game developers . Because the game needs to load a lot of art resources in the research and development , Create and destroy various objects frequently , It is easy to encounter various memory problems , For example, the most common OOM,"Out of Memory"(OOM).
>>UE4 Trigger OOM Logic
Generally speaking , It's hard for us to OOM Produced Dump Problems are found immediately in the documents , There are many specific reasons for the crash , Such as :
The code at a certain location incorrectly requested a large amount of memory due to logic problems
Memory leak , As a result, less and less memory resources are available , Finally, it collapsed
The logic code is not standardized , Frequent requests for large amounts of space and resident memory
The system runs a large number of processes at the same time , Memory space is really tight
Some cases are caused by problems with our program code , Some may be related to the system environment at that time ( Operating conditions , Set up ) of , Usually we need to combine Dump And some output Log To analyze the specific situation at that time . For example, the following log screenshot , Show that the game program encountered OOM Problem and printed the current memory data (Windows 64 position ,16G Memory ), You can see that although the virtual memory is almost used up (11MB), But in the end, the current machine still has a lot of physical memory (9GB).
>> Trigger OOM Memory related to printing Log
For students who have a certain computer foundation , You should know that the address space of modern operating system processes is a virtual memory address space ,32 Bit systems can theoretically have 4G Virtual memory ,64 Bit systems can have up to 16777216T Virtual memory , Why is it still triggered when there is enough physical memory OOM Well ? Analyze memory problems , We can't just limit ourselves to project code , It needs to be excavated and deepened as far as possible from the system level .
Virtual memory overview
Memory is one of the core components of a computer . Ideally , We hope CPU Before executing instructions, you can quickly ( Better be faster than the execution time of the instruction ) Get a lot of data from memory , So as to efficiently play the role of electronic computers “ Storage Computing ” function . But in reality , Due to hardware limitations, we have to design the memory as follows “ register —— Cache —— Main memory —— Hard disk ” Hierarchical structure to meet the goal as much as possible .
Cache and register space is very limited , It is designed to improve the speed of computation and access . by comparison , The development of main memory and hard disk storage technology can better cope with the increasing memory demand of applications . In early systems , Memory ( Main memory ) It is also a very expensive resource , If a large number of processes need to apply for physical memory , There must be many processes that cannot run due to insufficient memory . To manage memory more efficiently and safely , Modern operating systems put forward a very important abstract concept of memory —— Virtual memory , And use this to manage memory resources . Generally speaking , Virtual memory is a cache that treats memory as a hard disk , Only the content that is currently frequently used is stored in the memory , Others are first thrown to the hard disk for caching . If you want to use something on your hard drive , Just change this content to the memory .
Virtual memory mainly solves the problem :
Memory size limit , A large number of processes require a large amount of memory , But in fact, many memory contents are rarely accessed , There is no need to keep using memory
Consistent address space , It is convenient for programs to write code and manage memory
Protect the space security of the process , Avoid interaction between processes
Virtual memory technology involves hardware exceptions 、 Assembler 、 The linker 、 loader 、 Shared objects 、 file 、 Processes and almost all aspects of the system , So this content is very important in any field of computer . Generally speaking , We need to understand the following large pieces of virtual content :
1. The operating system uses the paging mechanism to manage the memory in the virtual address space , That is to say, virtual address to physical address mapping and exchange in and out are performed on a page by page basis . Usually the size of a page is 4KB.( Below VP It's just a page )
2. Physical addresses translated from virtual addresses require a table to index and correlate , This table is the page table . A page table is essentially a map, according to key( Virtual page number Virtual Page Number, abbreviation VPN) Get the corresponding value( Physical page box number Physical Page Number, abbreviation PPN, That is, the base address of the physical address ). Page tables are memory resident , If the memory space is large , It can be designed as a multi-level page table , The first level page table resides in kernel space .
3. The virtual memory space is the same for all processes ( such as 32 The virtual memory space of each process in the bit system is 0X00000000-0XFFFFFFFF), So we need to go through a transformation ( Address translation ) To access real physical space , The unit responsible for the transformation is called MMU,Memory Management Unit,MMU After the virtual address is translated into the physical address, the physical memory is accessed through the memory bus :
4. Translation and mapping process , The page table is responsible for storing 、MMU Responsible for calculation . The details also relate to the virtual space / Organization and design of physical space address , Page table design, etc
other TLB Speed up , Translation details , Multi level page table , Kernel Implementation and other contents you can refer to 《 Deep understanding of computer systems 》《 In depth understanding of Linux kernel 》 Other information
https://hansimov.gitbook.io/csapp/part2/ch09-virtual-memory/9.6-address-translation
https://www.youtube.com/watch?v=ZjKS1IbiGDA
https://zhuanlan.zhihu.com/p/370092684
The return question
On the basis of understanding the concept of virtual memory , We can go back and analyze the previous OOM problem . Logically speaking , Each process has its own virtual address space , Only related to the number of bits of the operating system ,64 Bit system , Namely 2^64Byte=16777216T. But if you fully support 64 Bit address space , The implementation of the page table of the operating system will be very complex , And usually processes don't use that much space ( Hard disks are generally not used ), So now Windows Only to 48 Bit virtual address space . Among them high 47 Bit for system use , User processes can only be assigned to 47 position =128TB, This is also very big .( Besides , Early hardware manufacturers also restricted the address bus , such as Intel stay 2015 Year of 64 Only the maximum... Is provided on the bit system 64TB The address space of is 48 Bit address addressing ,2022 The restrictions have been lifted in . about 32 The bit system is always maintained in the linear logical address space 2^32Byte=4GB, Maximum physical address space 2^36Byte=64GB)
>>Intel The official manual
https://www.intel.com/content/www/us/en/developer/articles/technical/intel-sdm.html
There is an official software ,Testlimit, You can see the virtual address space limit of the current system , The interior is constantly used VirtualAlloc Reverse Virtual memory . The local test results are as follows , Indeed 128TB.( Actually Windows There are also physical memory limitations , such as Win10 64 The enterprise version of bit can only use the largest 6TB Physical memory )
Testlimit - Windows Sysinternals | Microsoft Docs
https://docs.microsoft.com/zh-cn/sysinternals/downloads/testlimit
https://docs.microsoft.com/zh-cn/windows/win32/memory/memory-limits-for-windows-releases?redirectedfrom=MSDN#physical-memory-limits-windows-10
The problem now is , My process can have such a large virtual memory space , How can it be used up ? The problem lies in the non main memory part of the virtual memory —— Paging files .
Paging files
Virtual memory is managed in pages , Swap memory in and out . If the physical memory is insufficient , With some sort of elimination mechanism , Swap out some pages in physical memory to disk , In this way, the physical memory has space to store new contents . These pages stored on disk are called paging files (Linux It's called Swap Swap space ).
When an application requests memory , It's actually a piece of space on the disk , Memory is only used as an accelerated cache structure . How much memory can the program request , First, it is limited by the virtual address space (64 The bit system space size is large enough to ignore this limitation ), Then there is the remaining disk space .
The general meaning is OOM The error has nothing to do with the actual amount of physical memory
https://docs.microsoft.com/en-nz/archive/blogs/ericlippert/out-of-memory-does-not-refer-to-physical-memory
“Out Of Memory” Does Not Refer to Physical Memory
Our hard disk space is also limited , The operating system will not use all the disks for paging files , As shown in the picture, we can Windows Set it on the . The operating system will control the space occupied by the paging file according to the system load , At present, it is about 29GB( All in C disc ), The load will increase automatically ( There are limits ), Of course, we can also manually customize the size .
You can also view in the system manager ,
In the use : Used physical memory size
Has been submitted : The preceding number is the memory requested by all processes , It can be simply understood as malloc Total number of call requests , The following numbers are the total virtual memory size supported by the system , For physical memory + Paging file size .
The meaning of submission is the same as Windows Memory operation interface ,malloc Call in Windows Medium is equal to Reverse + Commit,Reverse Just record the virtual memory address status in the page table , For physical memory 、 There is no disk consumption .
Paging files are common to all processes , Transparent to users by default , Not visible on disk , But it can be turned on through options .
So you can C The name of the disk is pagefile.sys The paging file of .
Experimental tests
1. Request physical memory limit
At one 16GB Experiment on a computer , The initial memory state is as follows :
constantly malloc Memory , Until return NULL, Memory status at this time
look 45GB Is the upper limit , At this time, the paging file size in the disk is 33GB. The upper limit is estimated by the system according to the physical memory size and the remaining disk space .
If the automatic management mode is changed to manual , hold C Pan Fen 100GB For paging files . So when malloc To return to NULL when , Memory status is
You can also see that there are 1.5GB Of physical memory available . look down C disc , There is a lot less space
2. Insufficient disk space
If there is not enough disk space , Will the paging file also become very small ?
The first C Disk space is full , Only about 3G Space
And then on and on malloc, Finally, the virtual memory limit is very small
Based on the above data and experiments , Usually two situations lead to Windows Upper OOM
1 When running the game , Too many other programs have been run , Used up paging files , Because the default management mode has an upper limit for paging files
2 Insufficient disk space , Paging file cannot be resized
If the physical memory is very sufficient now ,C The disk has little space , This is the time to apply 1G Will memory crash ? The answer is no ,OOM It's physical space + The disk will crash when the total number of pages is not enough .
stay Windows On , We can print the size of the paging file to help verify . Below virtual memory It should be understood as virtual memory space ( Do not directly understand it as virtual memory ),paging file Is the paging file size ,PageFileUseage Indicates the used size of the current process paging file . Mainly used Windows api Of Global Memory StatusEx and GetProcess Memory Info.
expand :Linux Virtual memory and memory allocation on
stay Linux On the system , After the program is compiled, it will be in the corresponding elf The virtual memory address of each region is recorded in the file of format , Like code snippets (.text) Data segment (.data). When loaded into memory , Will be mapped to various locations in the virtual memory .
The virtual memory is organized into the following areas , Each process maintains a separate task structure task_struct To record the location information of these areas .
Linux The concept similar to that of a paging file is called swap space (Swap Space ), We can go through free Command to view information about memory .
About paging files /Swap There is one point about space that needs to be emphasized —— branch Page file /Swap Space is not equal to virtual memory , Or virtual memory can run without hard disk . Virtual memory is a standard abstract mechanism in modern operating systems , You can use the space on the hard disk for storage , When the physical memory space is insufficient, replace the unused memory to the hard disk . But we can turn this option off (Windows Select non paged file in settings ,Linux perform sudo swapoff -a), Let all the space requested by the process be mapped to physical memory , The advantage is that we can avoid the interaction between memory and hard disk , But it's also easier to trigger OOM, Therefore, it is generally recommended to have sufficient memory / Use when the system environment is relatively stable .
Of course , When we actually run C In language malloc(Linux May call according to the application size brk perhaps mmap,ptmalloc Memory allocator implementation ) Or directly call some system calls , It is very possible to just apply for a piece of virtual memory space , These spaces are not directly mapped to physical memory . When we access this memory , Will trigger the page missing interrupt , Then the actual allocation and mapping to physical memory , stay Linux It is generally called the delayed allocation mechanism of memory . in general , Different operating systems may behave differently ,Windows For small memory requests, you will usually directly call heapalloc Directly map to the corresponding physical memory ( The conclusion comes from the network , To be verified ).
Conclusion
This article focuses on the development scenarios of actual projects , Explore and excavate some details and vague concepts that have been neglected in previous studies . Memory problems are often strange , It needs to be understood in principle to better solve . But to understand virtual memory in depth 、 Understanding the principle of memory allocator certainly needs further research and learning , Including debugging project source code 、Linux Source code 、 View various documents, manuals, etc , There will be new learning achievements to share with you again ~
Previous articles are recommended
Game development technology series 【 Want to do game development , I should be able to order something ?】
Unreal Engine Technology Series 【 Use the Unreal Engine 4 year , I want to talk about his network architecture again 】
Game popular science series 【 Inventory of those in the game “ Development skills to deceive players' eyes ”】
C++ Interview series 【 The most complete in history C++/ Game development interview experience summary 】
I am a Jerish, Netease game engineer ,5 Years of experience . The official account will regularly export articles on dry cargo and popular science. , Follow my reply keywords to get game development 、 operating system 、 interview 、C++、 Game design and other related books and references .
边栏推荐
- 在UE内添加控制台工程(Programs)
- 使用IDEA画结构图
- ORB-SLAM3论文概述
- Cox regression model
- P2483-[模板]k短路/[SDOI2010]魔法猪学院【主席树,堆】
- R 语言降维的 PCA 与自动编码器
- PCA and automatic encoder for dimension reduction of R language
- 2022-06-25: given a positive number n, it means that there are tasks 0~n-1. Given an array of length N, time[i] means the time when task I is completed. Given a two-dimensional array matrix, matrix[j]
- gstreamer分配器与pool的那些事
- OpenAPI 3.0 specification - Food Guide
猜你喜欢
学习太极创客 — MQTT(四)服务端连接操作
UE5全局光照系統Lumen解析與優化
Multi surveyor Gongshu Xiao sir_ The solution of using websocket error reporting under working directory
《你不可不知的人性》经典语录
How to prompt
Gd32 ADC acquisition voltage
Camtasia 2022 nouvelle vidéo d'ordinateur d'enregistrement ultra - clair
Une citation classique de la nature humaine que vous ne pouvez pas ignorer
浅谈虚拟内存与项目开发中的OOM问题
Problem solving process of no internet access
随机推荐
Version management tool usage
Oracle练习
Une citation classique de la nature humaine que vous ne pouvez pas ignorer
Interviewer: please name four ways to exchange the values of two variables without using third-party variables
R 语言马尔可夫链蒙特卡洛:实用介绍
Oracle connectivity issues and Solutions
2021-08-04
Utonmos: digital collections help the inheritance of Chinese culture and the development of digital technology
GD32 ADC采集电压
2022-06-25: given a positive number n, it means that there are tasks 0~n-1. Given an array of length N, time[i] means the time when task I is completed. Given a two-dimensional array matrix, matrix[j]
Literature reading --- optimize RNA SEQ research to study herbicide resistance (review)
Learn from Taiji makers - mqtt (V) publish, subscribe and unsubscribe
golang--channal与select
解读Oracle
Never criticize
Notes on the 3rd harmonyos training in the studio
Create a nonlinear least squares test in R
Authorization of database
[machine learning] case study of college entrance examination prediction based on multiple time series
Leetcode 176 The second highest salary (June 25, 2022)