CS:APP第九章知识总结(virtual memory、内存回收)

在这里插入图片描述

Recall that a DRAM is at least 10 times slower than an SRAM and that disk is about 100,000 times slower than a DRAM. Thus, misses in DRAM caches are very expensive compared to misses in SRAM caches because DRAM cache misses are served from disk, while SRAM cache misses are
usually served from DRAM-based main memory. Further, the cost of reading the first byte from a disk sector is about 100,000 times slower than reading successive bytes in the sector. The bottom line is that the organization of the DRAM cache is driven entirely by the enormous cost of misses.
Because of the large miss penalty and the expense of accessing the first byte, virtual pages tend to be large—typically 4 KB to 2 MB. Due to the large miss penalty, DRAM caches are fully associative; that is, any virtual page can be placed in any physical page. The replacement policy on misses also assumes greater importance, because the penalty associated with replacing the wrong virtual page
is so high. Thus, operating systems use much more sophisticated replacement algorithms for DRAM caches than the hardware does for SRAM caches. Finally, because of the large access time of disk, DRAM caches always use write-back instead of write-through.

页表的作用: maps virtual pages to physical pages.
判断一个virtual page是否在物理内存中,若在则要知道其dram位置;若不在则要知道其disk位置,且能提供一个替换决策。
在这里插入图片描述
PTE,page table entry
在图9.4中,VP05未分配,VP1247属于cached,VP36属于未cached

在这里插入图片描述

operating systems provide a separate page table, and thus a separate virtual address space, for each process. Notice that multiple virtual pages can be mapped to the same shared physical page.

使用VM的好处:
1.Simplifying linking. every process on a given Linux system has a similar memory format.
2.Simplifying loading. loader本身不需要把数据从disk搬运到dram,只需要在页表中分配virtual page.
3.the operating system can arrange for multiple processes to share a single copy of this code by mapping the appropriate virtual pages in different processes to the same physical pages.(如图9.9)
4.Simplifying memory allocation. 在VM中连续的page可以指向physical memory的多处(而无需连续分配)
5.内存保护,如图9.10
在这里插入图片描述

原书中关于地址翻译的部分我就跳过了。

在这里插入图片描述
每个segment在virtual memory中是连续的,不同segment在virtual memory中可以有gap。
在这里插入图片描述

在这里插入图片描述

at any point in time, the swap space bounds the total amount of virtual pages that can be allocated by the currently running processes.

mmap()系统调用使得进程之间通过映射同一个普通文件实现共享内存。普通文件被映射到进程地址空间后,进程可以像访问普通内存一样对文件进行访问,不必再调用read(),write()等操作。

在这里插入图片描述

For each process, the kernel maintains a variable brk (pronounced “break”) that points to the top of the heap.

在这里插入图片描述
显式分配的弊端。

在这里插入图片描述
在这里插入图片描述
free list的优劣:
The advantage of an implicit free list is simplicity. A significant disadvantage is that the cost of any operation that requires a search of the free list, such as placing allocated blocks, will be linear in the total number of allocated and free blocks in the heap.

malloc的对齐:
If we assume a double-word alignment requirement, then the size of each block must be a multiple of two words (8 bytes). Thus, the block format in Figure 9.35 induces a minimum block size of two words: one word for the header and another to maintain the alignment requirement. Even if the application were to request a single byte, the allocator would still create a two-word block.

malloc的具体操作:
When an application requests a block of k bytes, the allocator searches the free list for a free block that is large enough to hold the requested block. The manner in which the allocator performs this search is determined by the placement policy. Some common policies are first fit, next fit, and best fit.
如果空闲块大于申请量,那么可能还会有split和coalesce操作。

如果合并了还是找不到满足的free块:
the allocator asks the kernel for additional heap memory by calling the sbrk function. The allocator transforms the additional memory into one large free block, inserts the block into the free list, and then places the requested block in this new free block.

合并的具体操作:
在这里插入图片描述
the footer is a replica of the header. If each block includes such a footer, then the allocator can determine the starting location and status of the previous block by inspecting its footer, which is always one word away from the start of the current block.
在这里插入图片描述
在这里插入图片描述

原书关于如何实现一个allocator的部分我就跳过了。

关于garbage collection:
In a system that supports garbage collection, applications explicitly allocate heap blocks but never explicitly free them.

在这里插入图片描述
garbage collector的基本原理是,把内存看成有向图,不可达的就是garbage。此处的可达,是指从root出发可达,root定义如下:
Root nodes correspond to locations not in the heap that contain pointers into the heap. These locations can be registers, variables on the stack, or global variables in the read/write data area of virtual memory.

garbage collect的过程(mark & sweep)
在这里插入图片描述
Initially, the heap in Figure 9.52 consists of six allocated blocks, each of which is unmarked. Block 3 contains a pointer to block 1. Block 4 contains pointers to blocks 3 and 6. The root points to block 4. After the mark phase, blocks 1, 3, 4, and 6 are marked because they are reachable from the root. Blocks 2 and 5 are unmarked because they are unreachable. After the sweep phase, the two unreachable blocks are reclaimed to the free list.

与内存相关的常见代码错误:

  1. malloc不清零
    在这里插入图片描述

  2. 栈溢出(gets)

  3. 多维malloc写错
    在这里插入图片描述

  4. off-by-one
    在这里插入图片描述
    第一维分配了n,第二维却用了n+1。

  5. 解引用运算符和其它运算符的优先级问题(如,解引用运算符与–同级)

  6. 错误的指针运算
    在这里插入图片描述

  7. 解引用不存在的变量
    在这里插入图片描述
    return之后这个函数的栈就作废了,所以返回的指针取到的数就无法预测了。

  8. free后又用,取到的数也是无法预测。

  9. memory leak(malloc后忘记free)
    If leak is called frequently, then the heap will gradually fill up with garbage, in the worst case consuming the entire virtual address space. Memory leaks are particularly serious for programs such as daemons and servers.

可见,很多问题都是因指针和显式free(过早free、忘记free)而起。

第十章,IO,第十一章,网络编程,第十二章,并发编程。之后需要学习再看吧,这个系列到此为止了。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值