这里主要是以 C 语言为例,其他语言开发的程序,每个进程都会有一个类似的空间。下面是一段 C 代码:

#include    <stdlib.h>
#include <stdio.h> double t[0x02000000]; void segments()
{
static int s = ;
void *p = malloc(); printf("stack\t%010p\nbrk\t%010p\nheap\t%010p\n"
"static\t%010p\nstatic\t%010p\ntext\t%010p\n",
&p, sbrk(),p,t,&s,segments);
} void writeFreeSpace()
{
char *p = sbrk() - ;
*p = ;
printf("assign to sbrk(0)-1 is succed! \n"); p = sbrk() + ;
*p = ;
printf("assign to sbrk(0)+1 is succed! \n");
} int main(int argc, char *argv[])
{
segments();
writeFreeSpace();
exit();
}

这里主要打印了:指针 p 的地址(stack),进程当前的 break 的位置( heap 的边界),指针 p 指向的地址(heap),全局变量 t 的地址,局部 static 变量的地址,还有函数 segments() 的地址。

使用 gcc 便以后,这段代码的运行结果如下:

stack    0xbfaa9edc
brk 0x18856000
heap 0x18835008
static 0x0804a060
static 0x0804a024
text 0x08048494
Size of heap: 20ff8
assign to sbrk()- is succed!
Segmentation fault

这很好的证明了下图中的分布关系:

其中,可能不常见的是 sbrk() 函数。一般情况下,应用编程的时候不推荐使用 sbrk(),所以我们见得少。通过 man 得知,sbrk( int ) 是用来增加 Heap 的大小的,当给它喂参数 0 ,它返回 Heap 的边界( sbrk(0) 返回的地址已经在 heap() 之外了,可以改动 writeFreeSpace() 中的地址尝试;从上面可以看出,这里 heap 的最小值默认是 132k Bytes(20ff8+8=21000),前面8 个 byte 是保留的,具体作用需要再作了解)。

valgrind

Valgrind是一套用于内存调试、内存泄漏检测以及性能分析的软件开发工具。下面是来自它官网的介绍:

The Valgrind tool suite provides a number of debugging and profiling tools that help you make your programs faster and more correct. The most popular of these tools is called Memcheck. It can detect many memory-related errors that are common in C and C++ programs and that can lead to crashes and unpredictable behaviour.

The Valgrind distribution currently includes six production-quality tools: a memory error detector, two thread error detectors, a cache and branch-prediction profiler, a call-graph generating cache and branch-prediction profiler, and a heap profiler. It also includes three experimental tools: a stack/global array overrun detector, a second heap profiler that examines how heap blocks are used, and a SimPoint basic block vector generator. It runs on the following platforms: X86/Linux, AMD64/Linux, ARM/Linux, ARM64/Linux, PPC32/Linux, PPC64/Linux, PPC64BE/Linux, S390X/Linux, MIPS32/Linux, MIPS64/Linux, ARM/Android (2.3.x and later), X86/Android (4.0 and later), MIPS32/Android, X86/Darwin and AMD64/Darwin (Mac OS X 10.9, with limited support for 10.8).

快速上手:http://valgrind.org/docs/manual/quick-start.html

Linux 下进程的内存空间分配的更多相关文章

  1. 面试问题之操作系统:Linux下进程的内存结构

    转载于:http://www.hqj.com/news/emb184.htm Linux操作系统采用虚拟内存管理技术,使得每个进程都有各自互不干涉的进程地址空间.该地址空间是大小为4GB的线性虚拟空间 ...

  2. linux 下 取进程占用内存(MEM)最高的前10个进程

    # linux 下 取进程占用 cpu 最高的前10个进程ps aux|head -1;ps aux|grep -v PID|sort -rn -k +3|head # linux 下 取进程占用内存 ...

  3. 【网络编程基础】Linux下进程通信方式(共享内存,管道,消息队列,Socket)

    在网络课程中,有讲到Socket编程,对于tcp讲解的环节,为了加深理解,自己写了Linux下进程Socket通信,在学习的过程中,又接触到了其它的几种方式.记录一下. 管道通信(匿名,有名) 管道通 ...

  4. linux 下进程通讯详解

    linux 下进程通讯方法主要有以下六种: 1.管道 2.信号 3.共享内存 4.消息队列 5.信号量 6.socket

  5. Linux下进程通信的八种方法

    Linux下进程通信的八种方法:管道(pipe),命名管道(FIFO),内存映射(mapped memeory),消息队列(message queue),共享内存(shared memory),信号量 ...

  6. Linux下进程隐藏的方法及其对抗

    零.背景 在应急响应中,经常碰到ps命令和top命令查不到恶意进程(异常进程)的情况,会对应急响应造成很大的影响.轻则浪费时间,重则排查不出问题,让黑客逍遥法外.所以这篇博客研究学习如何对抗linux ...

  7. linux下进程的最大线程数、进程最大数、进程打开的文件数

    linux下进程的最大线程数.进程最大数.进程打开的文件数   ===========最大线程数============== linux 系统中单个进程的最大线程数有其最大的限制 PTHREAD_TH ...

  8. 【Linux】Linux下进程间的通信方式

    本文内容: 1.进程通信的目的 2.介绍Linux下进程间的4种通信方式:管道,消息队列,共享内存,信号量 ps:套接字也可以用于进程间的通信,不过是不同物理机器上的进程通信,本章讨论是是同一台物理机 ...

  9. [转帖]linux下CPU、内存、IO、网络的压力测试,硬盘读写速度测试,Linux三个系统资源监控工具

    linux下CPU.内存.IO.网络的压力测试,硬盘读写速度测试,Linux三个系统资源监控工具 https://blog.51cto.com/hao360/1587165 linux_python关 ...

随机推荐

  1. JavaScript弹出窗口技巧

    window.open ('faces/newPage.jsp','newwindow','height=300,width=700, top=50,left=50,toolbar=no,menuba ...

  2. hadoop1.2.1伪分布模式配置

    1.修改core-site.xml,配置hdfs <configuration> <property> <name>fs.default.name</name ...

  3. Install Visual Studio For Mac Preview

    在Hack News上看到Visual Studio For Mac Preview的链接,上面有许多评论,纪录下尝鲜安装过程. 第一次尝试 VisualStudioforMacPreviewInst ...

  4. [BZOJ1143][CTSC2008]祭祀river(最长反链)

    题目:http://www.lydsy.com:808/JudgeOnline/problem.php?id=1143 分析: 最长反链==最小路径覆盖==n-二分图最大匹配数 某神犇对二分图的总结: ...

  5. 记一次在Eclipse中用Axis生成webservice服务端的过程中出现的问题

    问题一. Unable to find config file.  Creating new servlet engine config file: /WEB-INF/server-config.ws ...

  6. 37-wc 简明笔记

    显示行数.单词数和字节数 wc [options] [file-list] 参数 file-list是wc分析的一个或多个文件的路径名列表.如果省略file-list,wc就从标准输入中读取输入 选项 ...

  7. window php redis扩展下载地址

    redis扩展下载 http://windows.php.net/downloads/pecl/snaps/redis/

  8. android SwipeRefreshLayout google官方下拉刷新控件

    下拉刷新功能之前一直使用的是XlistView很方便我前面的博客有介绍 SwipeRefreshLayout是google官方推出的下拉刷新控件使用方法也比较简单 今天就来使用下SwipeRefres ...

  9. php 实现接收客户端上传的图片

    今天,遇到一个服务端接收客户端上传图片的需求,经过学习.我写了个简单的demo 以备下次学习. 首先服务器接收的发送图片的请求一定要是post请求,而且请求一定要加上 enctype="mu ...

  10. java保留两位小数

    java保留两位小数问题: 方式一: 四舍五入  double   f   =   111231.5585;  BigDecimal   b   =   new   BigDecimal(f);  d ...