进程启动后分配的“堆”有多大?

zzw_happy 2006-10-31 04:55:15
32位机器上逻辑地址有4G,是不是全部都可以用?
那为什么sbr系统调用还可以增加进程的堆呢?
...全文
443 6 打赏 收藏 转发到动态 举报
AI 作业
写回复
用AI写文章
6 条回复
切换为时间正序
请发表友善的回复…
发表回复
x86 2006-11-01
  • 打赏
  • 举报
回复
用户的地址空间是2G,堆栈大小还受ulimit限制:
ulimit -a
可以看到是否限制。

局部变量分配在栈中,堆栈用的是同样地址空间,只不过增长方向不一样。
你不妨写一个小程序测试一下,比如下面这个用递归调用来测试堆栈大小:

#include <stdio.h>

int test(int n) {
char buffer[1024];
printf(">>>>> n=%d\n", n);
if(n<1024*1024*100)
test(n+1);
for(int i=0;i<1024;i++)
buffer[i] = n%i;
printf("n=%d\n", n);
fflush(stdout);
return 0;
}

int main() {
test(0);
}

运行这个程序,直至程序被杀掉:
>>>>> n=938957
>>>>> n=9389Killed

可以看出,有将近1G的堆栈被分配。

我的机器有512M内存和512M swap,显然,如果没有其他限制,堆栈大小只跟内存大小有关。
仰望星空WU 2006-11-01
  • 打赏
  • 举报
回复
heap类似静态存储区,其大小取决于内存与虚拟内存的大小,new的空间放在此存储区
zzw_happy 2006-11-01
  • 打赏
  • 举报
回复
unix系统调用sbr可以修改进程的堆大小,
我就奇怪怎么修改的?难道初始时堆大小不确定?

----------------------------------
不好意思,记错了。是brk系统调用:
Reformatting sbrk(2), please wait...
BRK(2) Linux Programmer's Manual BRK(2)



NAME
brk, sbrk - change data segment size

SYNOPSIS
#include <unistd.h>

int brk(void *end_data_segment);

void *sbrk(intptr_t increment);

DESCRIPTION
brk sets the end of the data segment to the value specified by end_data_segment, when that value is reasonable, the
system does have enough memory and the process does not exceed its max data size (see setrlimit(2)).

sbrk increments the program's data space by increment bytes. sbrk isn't a system call, it is just a C library
wrapper. Calling sbrk with an increment of 0 can be used to find the current location of the program break.

RETURN VALUE
On success, brk returns zero, and sbrk returns a pointer to the start of the new area. On error, -1 is returned,
and errno is set to ENOMEM.

CONFORMING TO
BSD 4.3

brk and sbrk are not defined in the C Standard and are deliberately excluded from the POSIX.1 standard (see para­
graphs B.1.1.1.3 and B.8.3.3).

NOTES
Various systems use various types for the parameter of sbrk(). Common are int, ssize_t, ptrdiff_t, intptr_t.
XPGv6 obsoletes this function.

SEE ALSO
execve(2), getrlimit(2), malloc(3)



Linux 2.4 2003-11-01 BRK(2)
Manual page sbrk(2) line 1/41 (END)
zzw_happy 2006-11-01
  • 打赏
  • 举报
回复
unix系统调用sbr可以修改进程的堆大小,
我就奇怪怎么修改的?难道初始时堆大小不确定?
yqzq 2006-11-01
  • 打赏
  • 举报
回复
heap 和 stack 是一个概念么
仰望星空WU 2006-11-01
  • 打赏
  • 举报
回复
windows下进程默认一个线程stack是1M,linux下是10M,此值可修改,用ulimit修改系统限制

23,217

社区成员

发帖
与我相关
我的任务
社区描述
Linux/Unix社区 应用程序开发区
社区管理员
  • 应用程序开发区社区
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧