用size命令分析linux程序内存段的分布
2016-01-15 11:08
676 查看
用size命令分析linux程序内存段的分布
Size命令的输出不包括stack和heap的部分。只包括文本段(text), 代码段(data),未初始化数据段(bss)三部分。
1、文本段:包含程序的指令,它在程序的执行过程中一般不会改变。
2、数据段:包含了经过初始化的全局变量和静态变量,以及他们的值。
3、BSS段:包含未经初始化的全局变量和静态变量。
4、堆栈段:包含了函数内部声明的局部变量。当然,上面段的作用不仅于此,具体的作用
int main()
{
return 1;
}
[root@pc201 ccodes]# size a.out
text data bss dec hex filename
960 248 8 1216 4c0 a.out
-------------------------------
[root@pc201 ccodes]# cat test.c
int g_data;
int main()
{
return 1;
}
[root@pc201 ccodes]# size a.out [root@pc201 ccodes]# size a.out
text data bss dec hex filename text data bss dec hex
960 248 12 1220 4c4 a.out 960 248 8 1216 4c0
论1:未初始化的全局变量保存在BSS段
-------------------------------
[root@pc201 ccodes]# cat test.c
int g_data=1;
int main()
{
return 1;
}
[root@pc201 ccodes]# size a.out [root@pc201 ccodes]# size a.out
text data bss dec hex filename text data bss dec hex
960 252 8 1220 4c4 a.out 960 248 8 1216 4c0
论2:经过初始化的全局变量保存在数据段中
-------------------------------
[root@pc201 ccodes]# cat test.c
int main()
{
static int g_data;
return 1;
}
[root@pc201 ccodes]# size a.out [root@pc201 ccodes]# size a.out
text data bss dec hex filename text data bss dec hex
960 248 12 1220 4c4 a.out 960 248 8 1216 4c0
论3:未初始化的静态变量保存在BSS段中
--------------------------------
[root@pc201 ccodes]# cat test.c
int main()
{
static int g_data=1;
return 1;
}
[root@pc201 ccodes]# size a.out [root@pc201 ccodes]# size a.out
text data bss dec hex filename text data bss dec hex
960 252 8 1220 4c4 a.out 960 248 8 1216 4c0
论4:经过初始化的静态变量保存在数据段中
-------------------------------
[root@pc201 ccodes]# cat test.c
int main()
{
int i_data=1;
return 1;
}
[root@pc201 ccodes]# size a.out [root@pc201 ccodes]# size a.out
text data bss dec hex filename text data bss dec hex
976 248 8 1232 4d0 a.out 960 248 8 1216 4c0
论5:函数内部声明的局部变量保存在堆栈段(text)
------------------------------
[root@pc201 ccodes]# cat test.c
const int g_data=1;
int main()
{
return 1;
}
[root@pc201 ccodes]# size a.out [root@pc201 ccodes]# size a.out
text data bss dec hex filename text data bss dec hex
964 248 8 1220 4c4 a.out 960 248 8 1216 4c0
论6:const修饰的全局变量保存在文本段
------------------------------
[root@pc201 ccodes]# cat test.c
int main()
{
const int i_data=1;
return 1;
}
[root@pc201 ccodes]# size a.out [root@pc201 ccodes]# size a.out
text data bss dec hex filename text data bss dec hex
976 248 8 1232 4d0 a.out 960 248 8 1216 4c0
结论7:const修饰的局部变量保存在堆栈段
------------------------------
[root@pc201 ccodes]# cat test.c
char *pstr="";
int main()
{
return 1;
}
[root@pc201 ccodes]# size a.out [root@pc201 ccodes]# size a.out
text data bss dec hex filename text data bss dec hex
961 252 8 1221 4c5 a.out 960 248 8 1216 4c0
[root@pc201 ccodes]# cat test.c
char *pstr="123456789";
int main()
{
return 1;
}
[root@pc201 ccodes]# size a.out [root@pc201 ccodes]# size a.out
text data bss dec hex filename text data bss dec hex
970 252 8 1230 4ce a.out 960 248 8 1216 4c0
以发现,前后数据段和BSS段大小均未发生变化,而文本段增加了9个字节。
论8:字符串常量保存在文本段
结论
1、经过初始化的全局变量和静态变量保存在数据段中。
2、未经初始化的全局变量和静态变量保存在BSS段。
3、函数内部声明的局部变量保存在堆栈段中。
4、const修饰的全局变量保存在文本段中,const修饰的局部变量保存在堆栈段中。
5、字符串常量保存在文本段中。
Size命令的输出不包括stack和heap的部分。只包括文本段(text), 代码段(data),未初始化数据段(bss)三部分。
1、文本段:包含程序的指令,它在程序的执行过程中一般不会改变。
2、数据段:包含了经过初始化的全局变量和静态变量,以及他们的值。
3、BSS段:包含未经初始化的全局变量和静态变量。
4、堆栈段:包含了函数内部声明的局部变量。当然,上面段的作用不仅于此,具体的作用
int main()
{
return 1;
}
[root@pc201 ccodes]# size a.out
text data bss dec hex filename
960 248 8 1216 4c0 a.out
-------------------------------
[root@pc201 ccodes]# cat test.c
int g_data;
int main()
{
return 1;
}
[root@pc201 ccodes]# size a.out [root@pc201 ccodes]# size a.out
text data bss dec hex filename text data bss dec hex
960 248 12 1220 4c4 a.out 960 248 8 1216 4c0
论1:未初始化的全局变量保存在BSS段
-------------------------------
[root@pc201 ccodes]# cat test.c
int g_data=1;
int main()
{
return 1;
}
[root@pc201 ccodes]# size a.out [root@pc201 ccodes]# size a.out
text data bss dec hex filename text data bss dec hex
960 252 8 1220 4c4 a.out 960 248 8 1216 4c0
论2:经过初始化的全局变量保存在数据段中
-------------------------------
[root@pc201 ccodes]# cat test.c
int main()
{
static int g_data;
return 1;
}
[root@pc201 ccodes]# size a.out [root@pc201 ccodes]# size a.out
text data bss dec hex filename text data bss dec hex
960 248 12 1220 4c4 a.out 960 248 8 1216 4c0
论3:未初始化的静态变量保存在BSS段中
--------------------------------
[root@pc201 ccodes]# cat test.c
int main()
{
static int g_data=1;
return 1;
}
[root@pc201 ccodes]# size a.out [root@pc201 ccodes]# size a.out
text data bss dec hex filename text data bss dec hex
960 252 8 1220 4c4 a.out 960 248 8 1216 4c0
论4:经过初始化的静态变量保存在数据段中
-------------------------------
[root@pc201 ccodes]# cat test.c
int main()
{
int i_data=1;
return 1;
}
[root@pc201 ccodes]# size a.out [root@pc201 ccodes]# size a.out
text data bss dec hex filename text data bss dec hex
976 248 8 1232 4d0 a.out 960 248 8 1216 4c0
论5:函数内部声明的局部变量保存在堆栈段(text)
------------------------------
[root@pc201 ccodes]# cat test.c
const int g_data=1;
int main()
{
return 1;
}
[root@pc201 ccodes]# size a.out [root@pc201 ccodes]# size a.out
text data bss dec hex filename text data bss dec hex
964 248 8 1220 4c4 a.out 960 248 8 1216 4c0
论6:const修饰的全局变量保存在文本段
------------------------------
[root@pc201 ccodes]# cat test.c
int main()
{
const int i_data=1;
return 1;
}
[root@pc201 ccodes]# size a.out [root@pc201 ccodes]# size a.out
text data bss dec hex filename text data bss dec hex
976 248 8 1232 4d0 a.out 960 248 8 1216 4c0
结论7:const修饰的局部变量保存在堆栈段
------------------------------
[root@pc201 ccodes]# cat test.c
char *pstr="";
int main()
{
return 1;
}
[root@pc201 ccodes]# size a.out [root@pc201 ccodes]# size a.out
text data bss dec hex filename text data bss dec hex
961 252 8 1221 4c5 a.out 960 248 8 1216 4c0
[root@pc201 ccodes]# cat test.c
char *pstr="123456789";
int main()
{
return 1;
}
[root@pc201 ccodes]# size a.out [root@pc201 ccodes]# size a.out
text data bss dec hex filename text data bss dec hex
970 252 8 1230 4ce a.out 960 248 8 1216 4c0
以发现,前后数据段和BSS段大小均未发生变化,而文本段增加了9个字节。
论8:字符串常量保存在文本段
结论
1、经过初始化的全局变量和静态变量保存在数据段中。
2、未经初始化的全局变量和静态变量保存在BSS段。
3、函数内部声明的局部变量保存在堆栈段中。
4、const修饰的全局变量保存在文本段中,const修饰的局部变量保存在堆栈段中。
5、字符串常量保存在文本段中。
相关文章推荐
- Java开发必会的Linux命令
- 【Linux&Unix--open/close/write/read系统调用】
- 和菜鸟一起学linux总线驱动之i2c死锁问题
- linux:基本命令
- Linux RPM命令
- linux下python2.7.x版本安装
- linux和windows路由条目添加方法。
- Linux解压.gz的命令
- linux操作常用命令
- VM下centos设置静态IP
- 每天一个linux命令(1):ls
- linux下如何删除垃圾文件
- NS2安装在centOS7下,运行 ns simple.tcl 有错误
- linux增加虚拟内存
- linux下malloc()和free()的原理及实现
- Makefile学习笔记
- linux 内存使用计算
- linux usb枚举过程分析
- Linux常用操作-压缩与解压缩
- vi编辑器的用法