没有合适的资源?快使用搜索试试~ 我知道了~
understanding-glibc-malloc
需积分: 10 8 下载量 56 浏览量
2018-09-27
23:54:24
上传
评论
收藏 1023KB PDF 举报
温馨提示
试读
12页
我认为比较好的理解Linux 堆申请过程的教程,深入理解glibc的结构等
资源推荐
资源详情
资源评论
理解 glibc malloc
译者:猫科龙@csdn
来源:http://blog.csdn.net/maokelong95/article/details/51989081
本篇文章主要完成了对「Understanding glibc malloc」的翻译工作。限于本人翻译水平与专业技术水平(纯粹为了了解内存分配 而翻),本文章必定会有很多不
足之处,请大家见谅,也欢迎大家的指正!
联系邮箱:974985526@qq.com。
2017/03/17
优化排版;
理解 glibc malloc
堆内存是一个很有意思的领域,这样的问题:
堆内存是如何从内核中分配的?
内存管理效率怎样?
它是由内核、库函数,还是应用本身管理的?
堆内存可以开发吗?
我也困惑了很久,但是直到最近我才有时间去了解它。下面就让我来谈谈我的研究成果。开源社区提供了很多现成的内存分配器(memory allocators ):
dlmalloc – General purpose allocator
ptmalloc2 – glibc
jemalloc – FreeBSD and Firefox
tcmalloc – Google
libumem – Solaris
…
每一种分配器都宣称 自己快(fast)、可拓展(scalable )、效率高(memory efficient)!但是并非所 有的分配器都适用于我们的应用。内存吞吐量大(memory hungry)的
应用程序的性能很大程度上取决于内存分配器的性能。
在这篇文章中,我将只谈论「glibc malloc」内存分配器。为了更好地理解「glibc malloc」,我会联系最近的源代码。
历史:ptmalloc2 基于 dlmalloc 开发,并添加了 对多线程的支持, 于 2006 年公布。在 公布之后,ptmalloc2 被整合到 glibc 源代码中,此后 ptmalloc2 所有
的修改都直接提交到 glibc 的 malloc 部分去了。因此,ptmalloc2 的源码和 glibc 的 malloc源码有很多不一致的地方 。(译者注:1996 年出现的 dlmalloc 只
有一个主分配区,为所有线程所争用,1997 年发布的 ptmalloc 在 dlmalloc 的基础上引入了非主分配区的支持。 )
理解 glibc malloc
系统调用
线程处理
Example
输出分析
在主线程 malloc 之前
在主线程 malloc 之后
在主线程 free 之后
在thread1 malloc 之前
在thread1 malloc 之后
在thread1 free 之后
Arena
Arena的数量
Multiple Arena
Multiple Heaps
Chunk
Allocated chunk
Free chunk
Bins
Fast Bin
Unsorted Bin
mall Bin
Large Bin
Top Chunk
Last Remainder Chunk
系统调用
在之前的文章中提到过malloc的内部调用为 brk 或 mmap 。
译者注:其中有一张关于虚拟地址空间分布的图片,我觉得很有助于本篇文章的理解,因此把它放在此处。
线程处理
Linux 的早期版本使用 dlmalloc 为默认内存 分配器,但是因为 ptmalloc2 提供了多线程支持,所以 Linux 后来采用 ptmalloc2 作为 默认内存分配器。多线程支持可
以提升内存分配器的性能,进而间接提升应用的性能。
在 dlmalloc 中,当有两个线程同时调用 malloc 时,只有一个线程能够访问临界区(critical section)——因 为「空闲列表数据结构」(freelist data structure)被所有可
用线程共享。正如此,使用 dlmalloc 的多线程应用会在内存分配上耗费过多时间,导致整个应用性能的下降。
而在 ptmalloc2 中,当有两个线程同时调用 malloc 时,内存均会得到立 即分配——因为每个线程都维护着一个独 立的「堆段」(heap segment),因此维护这些堆
的「空闲列表数据结构」也是独立的。这种为每个线程独立地维护堆和「空闲列表数据结构」的行为就称为 per thread arena。
Example:
/* Per thread arena example. */
#include <stdio.h>
#include <stdlib.h>
#include <pthread.h>
#include <unistd.h>
#include <sys/types.h>
void* threadFunc(void* arg) {
printf("Before malloc in thread 1\n");
getchar();
char* addr = (char*) malloc(1000);
printf("After malloc and before free in thread 1\n");
getchar();
free(addr);
printf("After free in thread 1\n");
getchar();
}
int main() {
pthread_t t1;
void* s;
int ret;
char* addr;
printf("Welcome to per thread arena example::%d\n",getpid());
printf("Before malloc in main thread\n");
getchar();
addr = (char*) malloc(1000);
printf("After malloc and before free in main thread\n");
getchar();
free(addr);
printf("After free in main thread\n");
getchar();
ret = pthread_create(&t1, NULL, threadFunc, NULL);
if(ret)
{
printf("Thread creation error\n");
return -1;
}
ret = pthread_join(t1, &s);
if(ret)
{
printf("Thread join error\n");
return -1;
}
return 0;
}
输出分析
在主线程 malloc 之前
在如下的输出里我们可以看到,这里还 没有「 堆段」 也没有 「每线程栈」(per-thread stack),因为 thread1 还没有创建!
sploitfun@sploitfun-VirtualBox:~/ptmalloc.ppt/mthread$ ./mthread
Welcome to per thread arena example::6501
Before malloc in main thread
...
sploitfun@sploitfun-VirtualBox:~/ptmalloc.ppt/mthread$ cat /proc/6501/maps
08048000-08049000 r-xp 00000000 08:01 539625 /home/sploitfun/ptmalloc.ppt/mthread/mthread
08049000-0804a000 r--p 00000000 08:01 539625 /home/sploitfun/ptmalloc.ppt/mthread/mthread
0804a000-0804b000 rw-p 00001000 08:01 539625 /home/sploitfun/ptmalloc.ppt/mthread/mthread
b7e05000-b7e07000 rw-p 00000000 00:00 0
...
sploitfun@sploitfun-VirtualBox:~/ptmalloc.ppt/mthread$
在主线程 malloc 之后
在如下的输出中我们可以看到堆段产生在数据段(0804b000 - 0806c000)之上,这表明堆内存是通过更高级别的中断产生(也即 brk 中断)。此外,请注意,尽管
用户只申请了 1000 字节的内存,但是实 际产生了 132KB 的堆内存。这个连续的堆内存区域被 称为 「arena」 。因为这个「arena」是被主线程建立的,因此称
为「main arena」。接下来的申请会继续分配这个「arena」的132KB中剩余的部分,直到用尽。当用尽时,它可以通过更高级别的中断扩容,在扩容之后,
「top chunk」的大小也随之调整以圈进这块额外的空间。相应地,「arena」也可以在「top chunk」空间过大时缩小。
注意:top chunk是一个arena中最顶层的chunk。有关top chunk的更多信息详见下述“top chunk”部分。
sploitfun@sploitfun-VirtualBox:~/ptmalloc.ppt/mthread$ ./mthread
Welcome to per thread arena example::6501
Before malloc in main thread
After malloc and before free in main thread
...
sploitfun@sploitfun-VirtualBox:~/lsploits/hof/ptmalloc.ppt/mthread$ cat /proc/6501/maps
剩余11页未读,继续阅读
资源评论
purpledragon9527
- 粉丝: 525
- 资源: 1
上传资源 快速赚钱
- 我的内容管理 展开
- 我的资源 快来上传第一个资源
- 我的收益 登录查看自己的收益
- 我的积分 登录查看自己的积分
- 我的C币 登录后查看C币余额
- 我的收藏
- 我的下载
- 下载帮助
安全验证
文档复制为VIP权益,开通VIP直接复制
信息提交成功