简体   繁体   English

程序的堆内存的初始大小

[英]Initial size of heap memory for a program

Program: 程序:

#include<stdio.h>
#include<sys/types.h>
#include<malloc.h>
main()
{
    int *i1, *i2;
    char *s;
    printf("sbrk(0) before malloc(4): %x\n", sbrk(0));
    i1 = (int *) malloc(sizeof(int));
    printf("sbrk(0) after `i1 = (int *) malloc(4)': %x\n", sbrk(0));
    i2 = (int *) malloc(sizeof(int));
    printf("sbrk(0) after `i2 = (int *) malloc(4)': %x\n", sbrk(0));
}

Output: 输出:

sbrk(0) before malloc(4): a027000
sbrk(0) after `i1 = (int *) malloc(4)': a048000
sbrk(0) after `i2 = (int *) malloc(4)': a048000

what is the initial size of the heap memory for the above program. 上面程序的堆内存的初始大小是多少? I think, at the initial stage of the program, the starting and ending address of the heap are same. 我认为,在程序的初始阶段,堆的开始和结束地址是相同的。 Once, we call the malloc, it allocate memory using sbrk. 一次,我们调用malloc,它使用sbrk分配内存。

The above program shows that, at first sbrk(0) returns a027000 as program break. 上面的程序显示,首先sbrk(0)返回a027000作为程序中断。 After the malloc statement is executed, the program break is changed to a048000. 执行malloc语句后,程序中断将更改为a048000。 It shows that, before calling malloc, the heap does not have enough memory. 它表明,在调用malloc之前,堆没有足够的内存。 So, only the program break is changed after calling malloc. 因此,仅在调用malloc之后才更改程序中断。 At initial state, if the heap had the enough memory, there is no need to change the program break. 在初始状态下,如果堆具有足够的内存,则无需更改程序中断。 So at first, the size of heap is zero. 因此,起初,堆的大小为零。 Is it right? 这样对吗?

What happens depends on what your O/S does before your main() is called. 发生什么取决于在调用main()之前操作系统的操作。

On Mac OS X, there's a huge amount of memory allocation that occurs before your main() function gets called. 在Mac OS X上,在调用main()函数之前,会发生大量内存分配。 I currently have a list of 18 or so suppressed 'memory abuses' that are all from things that happen before the system-provided startup code calls main() . 我目前有18种左右的抑制“内存滥用”列表,这些都是由于系统提供的启动代码调用main()之前发生的事情。

Running a simple program that prints its arguments one per line, but giving it no arguments so there is no output, yields a usage report from valgrind like this: 运行一个简单的程序,每行打印一个参数,但是不给它参数,所以就没有输出,它会从valgrind生成使用情况报告,如下所示:

==59405== Memcheck, a memory error detector
==59405== Copyright (C) 2002-2013, and GNU GPL'd, by Julian Seward et al.
==59405== Using Valgrind-3.11.0.SVN and LibVEX; rerun with -h for copyright info
==59405== Command: /Users/jleffler/bin/al
==59405== 
--59405-- /Users/jleffler/bin/al:
--59405-- dSYM directory is missing; consider using --dsymutil=yes
--59405-- UNKNOWN mach_msg unhandled MACH_SEND_TRAILER option
--59405-- UNKNOWN mach_msg unhandled MACH_SEND_TRAILER option (repeated 2 times)
--59405-- UNKNOWN mach_msg unhandled MACH_SEND_TRAILER option (repeated 4 times)
==59405== 
==59405== HEAP SUMMARY:
==59405==     in use at exit: 34,808 bytes in 417 blocks
==59405==   total heap usage: 517 allocs, 100 frees, 41,784 bytes allocated
==59405== 
==59405== LEAK SUMMARY:
==59405==    definitely lost: 16 bytes in 1 blocks
==59405==    indirectly lost: 0 bytes in 0 blocks
==59405==      possibly lost: 13,002 bytes in 109 blocks
==59405==    still reachable: 21,790 bytes in 307 blocks
==59405==         suppressed: 0 bytes in 0 blocks
==59405== Rerun with --leak-check=full to see details of leaked memory
==59405== 
==59405== For counts of detected and suppressed errors, rerun with: -v
==59405== ERROR SUMMARY: 0 errors from 0 contexts (suppressed: 0 from 0)

The code for the program? 该程序的代码?

#include <stdio.h>
#include <stdlib.h>

int main(int argc, char **argv)  
{ 
    if (argc > 1)
    {
        while (*++argv) 
            puts(*argv);
    }
    return(EXIT_SUCCESS); 
}

Lots of memory activity, but none of it in the code I wrote. 很多内存活动,但是我编写的代码中没有。 The leak is one of a number that I have documented in a suppressions file. 泄漏是我在抑制文件中记录的众多错误之一。

When glibc debuginfo is installed (eg on Fedora 22: dnf debuginfo-install glibc-2.21-5.fc22.x86_64 ) one can print out the main_arena and mp_ . 当安装了glibc debuginfo时(例如在Fedora 22: dnf debuginfo-install glibc-2.21-5.fc22.x86_64 ),可以打印main_arenamp_

Debugging a program with an empty int main(void) : 用一个空的int main(void)调试程序:

mp_.sbrk_base is the base of the heap. mp_.sbrk_base是堆的基础。 On my Linux when main was called heap doesn't exist. 在我的Linux上,所谓的main不存在堆。 After calling malloc(1000) mp_.sbrk_base is 0x602000 . 调用malloc(1000) mp_.sbrk_base0x602000 main_arena.top is the free memory chunk at the top of heap. main_arena.top是堆顶部的空闲内存块。 It is 0x6023f0 0x6023f0

(gdb) b main
Breakpoint 1 at 0x4004fa: file test.c, line 3.
(gdb) r 
Starting program: /home/m/a.out 

Breakpoint 1, main () at test.c:3
3       return 0;
(gdb) p mp_
$1 = {trim_threshold = 131072, top_pad = 131072, mmap_threshold = 131072, arena_test = 8, arena_max = 0, n_mmaps = 0, 
  n_mmaps_max = 65536, max_n_mmaps = 0, no_dyn_threshold = 0, mmapped_mem = 0, max_mmapped_mem = 0, max_total_mem = 0, 
  sbrk_base = 0x0}
(gdb) p main_arena 
$2 = {mutex = 0, flags = 0, fastbinsY = {0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0}, top = 0x0, 
  last_remainder = 0x0, bins = {0x0 <repeats 254 times>}, binmap = {0, 0, 0, 0}, next = 0x7ffff7dd6b20 <main_arena>, 
  next_free = 0x0, system_mem = 0, max_system_mem = 0}
(gdb) call malloc(1000)
$3 = (void *) 0x602010
(gdb) p mp_
$4 = {trim_threshold = 131072, top_pad = 131072, mmap_threshold = 131072, arena_test = 8, arena_max = 0, n_mmaps = 0, 
  n_mmaps_max = 65536, max_n_mmaps = 0, no_dyn_threshold = 0, mmapped_mem = 0, max_mmapped_mem = 0, max_total_mem = 0, 
  sbrk_base = 0x602000 ""}
    (gdb) p main_arena
$5 = {mutex = 0, flags = 1, fastbinsY = {0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0}, top = 0x6023f0, 
  last_remainder = 0x0, bins = {0x7ffff7dd6b78 <main_arena+88>, 0x7ffff7dd6b78 <main_arena+88>, [...]
0x7ffff7dd7198 <main_arena+1656>, 0x7ffff7dd71a8 <main_arena+1672>, 0x7ffff7dd71a8 <main_arena+1672>...}, binmap = {0, 
0, 0, 0}, next = 0x7ffff7dd6b20 <main_arena>, next_free = 0x0, system_mem = 135168, max_system_mem = 135168}

(gdb) call sbrk(0)
$6 = 6434816
(gdb) p (void*)$6
$7 = (void *) 0x623000
(gdb) 

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM