C++ Malloc 不调用 mmap 或 brk?
C++ Malloc Doesn't call mmap or brk?
在 ubuntu 18.04 我写了下面的 c 程序 运行 它的输入是 100
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
int main() {
int x;
scanf("%d", &x);
usleep(1);
void *tmp = malloc(x);
usleep(1);
free(tmp);
return 0;
}
终端中的输出是:(使用 strace)
strace ./a.out
execve("./a.out", ["./a.out"], 0x7ffd69236c10 /* 50 vars */) = 0
brk(NULL) = 0x55a719717000
access("/etc/ld.so.nohwcap", F_OK) = -1 ENOENT (No such file or directory)
access("/etc/ld.so.preload", R_OK) = -1 ENOENT (No such file or directory)
openat(AT_FDCWD, "/etc/ld.so.cache", O_RDONLY|O_CLOEXEC) = 3
fstat(3, {st_mode=S_IFREG|0644, st_size=96020, ...}) = 0
mmap(NULL, 96020, PROT_READ, MAP_PRIVATE, 3, 0) = 0x7f7acb879000
close(3) = 0
access("/etc/ld.so.nohwcap", F_OK) = -1 ENOENT (No such file or directory)
openat(AT_FDCWD, "/lib/x86_64-linux-gnu/libc.so.6", O_RDONLY|O_CLOEXEC) = 3
read(3, "7ELF[=11=][=11=][=11=][=11=][=11=][=11=][=11=][=11=][=11=]>[=11=][=11=][=11=][=11=][=11=][=11=][=11=][=11=][=11=]"..., 832) = 832
fstat(3, {st_mode=S_IFREG|0755, st_size=2030928, ...}) = 0
mmap(NULL, 8192, PROT_READ|PROT_WRITE, MAP_PRIVATE|MAP_ANONYMOUS, -1, 0) = 0x7f7acb877000
mmap(NULL, 4131552, PROT_READ|PROT_EXEC, MAP_PRIVATE|MAP_DENYWRITE, 3, 0) = 0x7f7acb277000
mprotect(0x7f7acb45e000, 2097152, PROT_NONE) = 0
mmap(0x7f7acb65e000, 24576, PROT_READ|PROT_WRITE, MAP_PRIVATE|MAP_FIXED|MAP_DENYWRITE, 3, 0x1e7000) = 0x7f7acb65e000
mmap(0x7f7acb664000, 15072, PROT_READ|PROT_WRITE, MAP_PRIVATE|MAP_FIXED|MAP_ANONYMOUS, -1, 0) = 0x7f7acb664000
close(3) = 0
arch_prctl(ARCH_SET_FS, 0x7f7acb8784c0) = 0
mprotect(0x7f7acb65e000, 16384, PROT_READ) = 0
mprotect(0x55a718d34000, 4096, PROT_READ) = 0
mprotect(0x7f7acb891000, 4096, PROT_READ) = 0
munmap(0x7f7acb879000, 96020) = 0
fstat(0, {st_mode=S_IFCHR|0620, st_rdev=makedev(136, 0), ...}) = 0
brk(NULL) = 0x55a719717000
brk(0x55a719738000) = 0x55a719738000
read(0, 100
"100\n", 1024) = 4
nanosleep({tv_sec=0, tv_nsec=1000}, NULL) = 0
nanosleep({tv_sec=0, tv_nsec=1000}, NULL) = 0
lseek(0, -1, SEEK_CUR) = -1 ESPIPE (Illegal seek)
exit_group(0) = ?
+++ exited with 0 +++
这不是st运行ge吗?为什么 malloc 没有使用 brk
或 mmap
来分配内存(在两次调用 nanosleep()
之间)。
这种 st运行ge 行为的原因是什么?
如果我将输入更改为 200000,我会在中间看到对 mmap 的调用。
Isn't this strange?
没有
why malloc didn't use brk or mmap for the allocation of memory (between the 2 calls for nanosleep()).
场景 1:您启用了优化,并且优化器发现忽略对 malloc
的调用是不可观察的,因此它从未被调用。
场景 2:malloc
被调用,但它大概不需要在此时调用 brk
或 mmap
。 brk
和 mmap
之前都被调用过,可能是为了 malloc
.
在 ubuntu 18.04 我写了下面的 c 程序 运行 它的输入是 100
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
int main() {
int x;
scanf("%d", &x);
usleep(1);
void *tmp = malloc(x);
usleep(1);
free(tmp);
return 0;
}
终端中的输出是:(使用 strace)
strace ./a.out
execve("./a.out", ["./a.out"], 0x7ffd69236c10 /* 50 vars */) = 0
brk(NULL) = 0x55a719717000
access("/etc/ld.so.nohwcap", F_OK) = -1 ENOENT (No such file or directory)
access("/etc/ld.so.preload", R_OK) = -1 ENOENT (No such file or directory)
openat(AT_FDCWD, "/etc/ld.so.cache", O_RDONLY|O_CLOEXEC) = 3
fstat(3, {st_mode=S_IFREG|0644, st_size=96020, ...}) = 0
mmap(NULL, 96020, PROT_READ, MAP_PRIVATE, 3, 0) = 0x7f7acb879000
close(3) = 0
access("/etc/ld.so.nohwcap", F_OK) = -1 ENOENT (No such file or directory)
openat(AT_FDCWD, "/lib/x86_64-linux-gnu/libc.so.6", O_RDONLY|O_CLOEXEC) = 3
read(3, "7ELF[=11=][=11=][=11=][=11=][=11=][=11=][=11=][=11=][=11=]>[=11=][=11=][=11=][=11=][=11=][=11=][=11=][=11=][=11=]"..., 832) = 832
fstat(3, {st_mode=S_IFREG|0755, st_size=2030928, ...}) = 0
mmap(NULL, 8192, PROT_READ|PROT_WRITE, MAP_PRIVATE|MAP_ANONYMOUS, -1, 0) = 0x7f7acb877000
mmap(NULL, 4131552, PROT_READ|PROT_EXEC, MAP_PRIVATE|MAP_DENYWRITE, 3, 0) = 0x7f7acb277000
mprotect(0x7f7acb45e000, 2097152, PROT_NONE) = 0
mmap(0x7f7acb65e000, 24576, PROT_READ|PROT_WRITE, MAP_PRIVATE|MAP_FIXED|MAP_DENYWRITE, 3, 0x1e7000) = 0x7f7acb65e000
mmap(0x7f7acb664000, 15072, PROT_READ|PROT_WRITE, MAP_PRIVATE|MAP_FIXED|MAP_ANONYMOUS, -1, 0) = 0x7f7acb664000
close(3) = 0
arch_prctl(ARCH_SET_FS, 0x7f7acb8784c0) = 0
mprotect(0x7f7acb65e000, 16384, PROT_READ) = 0
mprotect(0x55a718d34000, 4096, PROT_READ) = 0
mprotect(0x7f7acb891000, 4096, PROT_READ) = 0
munmap(0x7f7acb879000, 96020) = 0
fstat(0, {st_mode=S_IFCHR|0620, st_rdev=makedev(136, 0), ...}) = 0
brk(NULL) = 0x55a719717000
brk(0x55a719738000) = 0x55a719738000
read(0, 100
"100\n", 1024) = 4
nanosleep({tv_sec=0, tv_nsec=1000}, NULL) = 0
nanosleep({tv_sec=0, tv_nsec=1000}, NULL) = 0
lseek(0, -1, SEEK_CUR) = -1 ESPIPE (Illegal seek)
exit_group(0) = ?
+++ exited with 0 +++
这不是st运行ge吗?为什么 malloc 没有使用 brk
或 mmap
来分配内存(在两次调用 nanosleep()
之间)。
这种 st运行ge 行为的原因是什么?
如果我将输入更改为 200000,我会在中间看到对 mmap 的调用。
Isn't this strange?
没有
why malloc didn't use brk or mmap for the allocation of memory (between the 2 calls for nanosleep()).
场景 1:您启用了优化,并且优化器发现忽略对 malloc
的调用是不可观察的,因此它从未被调用。
场景 2:malloc
被调用,但它大概不需要在此时调用 brk
或 mmap
。 brk
和 mmap
之前都被调用过,可能是为了 malloc
.