Questions tagged [sbrk]

**sbrk** is a basic memory management system call used in Unix and Unix-like operating systems to control the amount of memory allocated to the data segment[clarification needed] of the process.

sbrk is a basic memory management system call used in Unix and Unix-like operating systems to control the amount of memory allocated to the data segment[clarification needed] of the process. These calls are typically made from a higher-level memory management library such as malloc. In the original Unix system, brk and sbrk were the only ways in which applications could acquire additional data space; later versions allowed this to also be done using the mmap call

88 questions
118
votes
3 answers

How is malloc() implemented internally?

Can anyone explain how malloc() works internally? I have sometimes done strace program and I see a lot of sbrk system calls, doing man sbrk talks about it being used in malloc() but not much more.
bodacydo
  • 63,809
  • 83
  • 206
  • 303
28
votes
3 answers

Does malloc() use brk() or mmap()?

c code: // program break mechanism // TLPI exercise 7-1 #include #include void program_break_test() { printf("%10p\n", sbrk(0)); char *bl = malloc(1024 * 1024); printf("%x\n", sbrk(0)); free(bl); …
user218867
  • 16,252
  • 12
  • 112
  • 156
20
votes
4 answers

How are sbrk/brk implemented in Linux?

I was thinking about how the Linux kernel implements system calls and I was wondering if someone could give me a high level view of how sbrk/brk work? I've reviewed the kernel code, but there is just so much of it and I don't understand it. I was…
samoz
  • 51,592
  • 52
  • 138
  • 190
16
votes
6 answers

Undefined reference to _sbrk

I am having a problem with _sbrk. In a link phase of compilation i use below comand to link my objects and i get undefined reference to _sbrk. arm-none-eabi-ld -static -T linkerscript.ld -o exe timer_example.o…
71GA
  • 1,666
  • 5
  • 30
  • 53
16
votes
1 answer

Why does calling sbrk(0) twice give a different value?

I'm trying to understand the sbrk() function. From what I know: sbrk(0) returns the current address of the break and doesn't increment it. sbrk(size) increments the address of the break by size bytes and returns the previous address of the…
Bibas
  • 434
  • 3
  • 13
16
votes
2 answers

How do I free memory obtained by sbrk()?

I have a custom allocator function which uses sbrk() to obtain memory. How do I release this memory when it's no longer needed? Is there a function equivalent to free() for malloc() ? or do I have to use brk() to set the end of the data segment ?
user238707
  • 195
  • 1
  • 1
  • 6
16
votes
2 answers

what does "malloc_trim(0)" really mean?

The manual page told me so much and through it I know lots of the background knowledge of memory management of "glibc". But I still get confused. does "malloc_trim(0)"(note zero as the parameter) mean (1.)all the memory in the "heap" section will be…
lookof
  • 313
  • 1
  • 3
  • 8
9
votes
1 answer

What's unsafe/legacy about brk/sbrk?

I've heard in a lot of places (musl mailing list, macOS forums, etc.) that brk() and sbrk() are unsafe. Many of these places either don't give explanations at all, or give very vague explanations. For example, this link states that "these functions…
S.S. Anne
  • 13,819
  • 7
  • 31
  • 62
9
votes
5 answers

How to return memory from process to the OS

I have an issue with memory management in various operating systems. My program is a server that does some processing that could take a few GB of memory. After that, it releases most of the memory while it waits for a few hours until another request…
ModdyFire
  • 618
  • 2
  • 9
  • 18
8
votes
2 answers

What do brk and sbrk stand for?

While I know what the Unix system call brk and function sbrk do, I have no idea what they stand for. Can anyone enlighten me?
Thomas Eding
  • 31,027
  • 10
  • 64
  • 101
8
votes
5 answers

How does sbrk() work in C++?

Where can I read about sbrk() in some detail? How does it exactly work? In what situations would I want to use sbrk() instead of the cumbersome malloc() and new()? btw, what is the expansion for sbrk()?
Lazer
  • 79,569
  • 109
  • 264
  • 349
6
votes
1 answer

How/where is sbrk used within malloc.c?

I have read in Advanced Unix Programming (and also in a few other books) that Linux malloc() uses the Linux system call sbrk() to request memory from the operating system. I am looking at the glibc malloc.c code and I can see many mentions of sbrk()…
user997112
  • 25,084
  • 34
  • 143
  • 278
5
votes
1 answer

mmap vs sbrk, performance comparison

Which of these calls is faster on average? I've heard that mmap is faster for smaller allocations but I haven't heard a comparison of either. Any information on performance for these would be nice.
Jesus Ramos
  • 22,120
  • 9
  • 53
  • 82
5
votes
2 answers

Why malloc(1) gives more than one page size?

I have tried in my machine using sbrk(1) and then deliberately write out of bound to test page size, which is 4096 bytes. But when I call malloc(1), I get SEGV after accessing 135152 bytes, which is way more than one page size. I know that malloc is…
Louis Kuang
  • 677
  • 12
  • 27
5
votes
2 answers

Linux - why is the program break pointer (brk/sbrk) different each time a program is run?

I understand that the program break is the highest virtual memory address that the Linux OS has allocated for a process, and therefore marks the highest address of the heap. You can get the address of the program break by calling sbrk( 0 ). When I…
Chris Vig
  • 7,629
  • 2
  • 22
  • 31
1
2 3 4 5 6