12. 内存管理(Memory Management)

内存管理目标

  1. MAIN MEMORY
  • Main memory(主内存) is central(中心) to the operation(运作) of a modern computer system.
  • Memory consists of a large array of bytes(一大组字节数据或者字节序列),each with its own address(地址).(内存是由一大组字节组成,每一个字节都有自己的地址)。
  • The CPU fetches(获取 ) instructions from memory according to the value of the program counter(PC).These instructions may cause additional loading from and storing to specific memory addresses.(这些指令可能会导致从特定内存地址加载和存储额外的数据。)
  • A typical instruction-execution cycle, for example ,first fetches an instruction from memory.The instruction is then decoded and may cause operands to be fetched from memory.After the instruciton has been executed on the operands on the operands ,results may be stored back in memory.(典型的指令执行周期,例如,首先从内存中取出一条指令。然后对指令进行解码,可能需要从内存中取出操作数。在对操作数执行了指令后,结果可能会被存回内存。)
  1. 高速缓存cache
  • cache
    • 第一次:cpu读取发现cache
    • 第二次:cpu发现cache,那么就会去读内存
  • 现在有三级缓存,每一个单核中有两个cache,然后多核之外还有一个cache
  1. 保护操作系统和用户进程
  • 用户进程不可以访问操作系统内存数据,以及用户进程空间之间不能互相影响(用户进程空间之间不能互相影响,有些恶意软件,去干扰其他聊天软件的数据)
    • 通过硬件实现, 因为操作系统一般不干预cpu对内存的访问
      • base register:基址寄存器
      • limit register:限长寄存器
    • 上述两个寄存器的值只能被操作系统的特权指令加载,然后限制只可以访问两个寄存器之间的内存空间去读取
  1. 内存管理目标

逻辑地址和物理地址

  1. 地址空间和地址转换
  2. 地址转换时机
  • 一个程序需要进行逻辑地址转换到物理地址,应该在run time这个时机比较好。
  • 没有逻辑地址转向物理地址重新计算的烦恼。
  1. 内存管理单元MMU
  • Memory-Management Unit 完成逻辑地址到物理地址‘运行时’的转换工作
    • 重定位寄存器(relocation register)或基址寄存器

连续内存分配

  1. contiguous memory allocation(连续内存分配)
  • In contiguous memory allocation,each process is contained in a single section of memory that is contiguous to the section containing the next process.(在连续内存分配中,每个进程都包含在一个块中与包含下一个进程的部分相邻的内存部分中。)
    • Memory allocation(内存分配)
    • Memory recycle(内存回收)
    • Memory protection(内存保护)
  1. fixed-sized partition(固定大小分区)
  • Memory is divided(分开) to several(几个) fixed-sized partitions(分区) Each partition may contain exactly one process.(内存被分成几个固定大小的分区。每个分区可能只包含一个进程。)
  • 一个进程占用一个分区,进程再运行的时候会把虚拟地址通过mmu将分区的起始地址加偏移地址转换为物理地址。
  • 会有一个表,记录每一个区域的起始地址,长度,并记录是否有进程占有它。
  • 这个管理方案是早期的管理方案,不灵活。
  1. variable-partition(可变分区)
  • In the variable-partition scheme(可变分区方案),the operating system keeps two tables indicating(指令) which parts of memory are available and which are occupied.(在可变分区方案中,操作系统会维护两个表,指示哪些内存空间是可用的,哪些是被占用的。)
  • Initially,all memory is available for user processes and is considered(被考虑) one large block of available memory ,a hole(孔洞).(最初,所有内存都可供用户进程使用,被视为一大块可用内存,一个孔洞。)
  • Eventually ,as you will see ,memory contains a set of holes of various sizes.(最终,正如你将会看到的那样,内存中包含着各种大小的空洞。)
  1. 动态存储分配问题
  • 后面两个算法需要遍历所有空洞。
  1. 地址转换与保护
  • 两种连续分配方案的地址转换方式是相似的:
    • 物理地址= 基址+ 逻辑地址
  • 地址保护策略:与无限长limit进行比较
  • 大致流程:与limit register进行比较如果是yes,那么就把逻辑地址转换为物理地址(通过基址),如果是no那么就会报错addressing error.
  1. 碎片
  • Fragmentation:some little pieces of memory hardly to be used.(碎片化:一些几乎无法使用的小内存片段。)
    • internal fragmentation(内部碎片,是对固定大小分区的情况的)
    • external fragmentation(外部碎片,是对可变分区)
      • compaction(压缩)
        • static relocation(考虑地址转换是否是动态的,只有是运行时候地址转换的方案才可以使用这个方案)
        • cost(成本,考虑需要额外的空间和时间)
  1. 可变分区和固定分区这两种方案已经被放弃掉了,请看下一章

笔记来自b站
【【Linux操作系统】Lecture 12 Memory Management】https://www.bilibili.com/video/BV1gE411F74T?vd_source=7183d94355e25d7a846a2242f0eb9c7d
感谢老师的网课

posted @ 2024-04-30 16:23  zhudachang  阅读(37)  评论(0编辑  收藏  举报