堆内存和平板分配 [英] Heap Memory and Slab allocation

查看:78
本文介绍了堆内存和平板分配的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我对heapfree list感到困惑.我有几个问题,我对malloc在C中的工作方式有自己的理解.如果我错了,请纠正我.

I'm confused regarding heap and free list. I have a few questions and I have my own understanding of how malloc works in C. Please correct me if I'm wrong.

  • 堆内存是否组织为数据的链接列表(空闲列表) 块?
  • 堆内存和空闲列表之间有区别吗?
  • Is the heap memory organized as a linked list (free list) of data blocks ?
  • Is there a difference between heap memory and free list ?

我对存储分配的理解(有待改进):- 当我们调用malloc时,它会在堆中分配内存,并通过从free list中选择合适大小的数据块来进行分配,对吗?

My understanding of storage allocation (open for improvement) :- When we call malloc, it allocates memory in the heap, and it does so by picking a data block of suitable size from the free list, right ?

当malloc返回某个内存块时,会将其从空闲列表中删除,并在页表中更新该内存块的物理地址.

When a certain block of memory is returned by malloc, it is removed from the free-list and the physical address of that block of memory is updated in the page table.

当使用free()释放内存时,数据块将被重新插入到空闲列表中,并且可能是为了减少碎片,并与相邻块和页表项中的present位结合在一起已清除.

When the memory is free'd using free(), the data block is inserted back in the free-list, and possibly , to reduce fragmentation, conjoined with neighbouring block, and the present bit in the page table entry is cleared.

所以整个堆都是一个空闲列表(空闲块的链接列表)+分配的数据块.

So the entire heap is a free-list(linked list of free blocks) + allocated data blocks .

这是对存储分配的全面了解吗?

Is that a comprehensive picture of storage allocation ?

摘自有关内存管理的Linux内核开发(罗伯特·洛夫)一章, Slab分配

EDIT : From Linux Kernel Development (Robert Love) Chapter on memory Management , Slab allocation

空闲列表包含一个已分配的可用数据块 结构.当代码需要数据结构的新实例时, 可以从空闲列表中获取其中一种结构,而不是分配 足够的内存量并将其设置为数据结构. 以后,当不再需要数据结构时,将其返回给 免费列表,而不是释放列表.从这个意义上说,免费清单 充当对象缓存,缓存常用类型的对象."

"A free list contains a block of available, already allocated, data structures. When code requires a new instance of a data structure, it can grab one of the structures off the free list rather than allocate the sufficient amount of memory and set it up for the data structure. Later, when the data structure is no longer needed, it is returned to the free list instead of deallocated. In this sense, the free list acts as an object cache, caching a frequently used type of object."

自由列表被称为可用的,分配的数据结构块".

Free-list is mentioned as a "block of available, allocated data structure."

  • 在空闲列表中时,如何 分配 ?
  • 又如何将内存块返回到空闲列表_ 不是 _与释放该块相同?
  • 平板分配与存储分配有何不同
  • How is it allocated, when it is in a free-list ?
  • And how is returning a block of memory to free list _not_ the same as deallocating that block ?
  • How is slab allocation different from storage allocation

推荐答案

malloc()与页面表并没有真正的联系;它分配虚拟地址,内核负责跟踪页面实际存储在物理RAM或磁盘中的位置.

malloc() isn't really related to the page table; it allocates virtual addresses, and the kernel is responsible for keeping track of where the pages are actually stored in physical RAM or on disk.

malloc()通过brk()系统调用与内核交互,该调用要求内核为进程分配更多页面,或将页面释放回内核.因此,实际上有两个级别的内存分配:

malloc() interacts with the kernel via the brk() system call, which asks the kernel to allocate more pages to the process, or releases pages back to the kernel. So there are really two levels of memory allocation:

  • 内核将页面分配给某个进程,从而使这些页面无法供其他进程使用.从内核的角度来看,页面可以位于任何地方,并且页面表可以跟踪页面的位置,但是从进程的角度来看,这是一个连续的虚拟地址空间. brk()操纵的程序中断"是内核将允许您访问的地址(因为它们与分配的页面相对应)和如果您尝试访问它们将导致分段错误的地址之间的边界.
  • malloc()分配程序数据段的可变大小部分,以供程序使用.当数据段的当前大小内没有足够的可用空间时,它将使用brk()从内核获取更多页面,从而使数据段更大.当发现数据段末尾的某些空间未被使用时,它使用brk()将未使用的页面返回给内核,从而使数据段更小.
  • The kernel allocates pages to a process, making those pages unavailable for use by other processes. From the kernel's standpoint, the pages can be located anywhere and their locations are tracked by the page table, but from the process's standpoint, it's a contiguous virtual address space. The "program break" that brk() manipulates is the boundary between addresses that the kernel will let you access (because they correspond to allocated pages) and addresses that will cause a segmentation fault if you try to access them.
  • malloc() allocates variable-sized portions of the program's data segment for use by the program. When there's not enough free space within the current size of the data segment, it uses brk() to get more pages from the kernel, making the data segment bigger. When it finds that some space at the end of the data segment is unused, it uses brk() to give the unused pages back to the kernel, making the data segment smaller.

请注意,即使在该进程中运行的程序实际上并没有将页面用于任何东西,也可以将页面分配给该进程(由内核).如果free()位于数据段中间的内存块,则free()的实现不能使用brk()缩小数据段,因为在较高地址处还有其他分配的块.因此,即使从malloc()立场来说,页面是可用空间",从内核的角度来看,这些页面仍会分配给您的程序.

Note that pages can be allocated to a process (by the kernel) even if the program running in that process isn't actually using the pages for anything. If you free() a block of memory that's located in the middle of your data segment, the implementation of free() can't use brk() to shrink the data segment because there are still other allocated blocks at higher addresses. So the pages remain allocated to your program from the kernel standpoint, even though they're "free space" from the malloc() standpoint.

您对空闲列表的工作方式的描述对我来说似乎很正确,尽管我对内存分配器的实现方式并不熟悉.但是您从Robert Love发表的报价听起来像是在谈论Linux内核中的内存分配,这与malloc()在用户空间进程中的内存分配无关.这种免费列表的工作方式可能有所不同.

Your description of how a free list works sounds right to me, though I'm no expert in how memory allocators are implemented. But the quote you posted from Robert Love sounds like it's talking about memory allocation within the Linux kernel, which is unrelated to memory allocation by malloc() within a userspace process. That kind of free list probably works differently.

这篇关于堆内存和平板分配的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆