flat assembler
Message board for the users of flat assembler.
Index
> Linux > How to free memory from sys_brk (64-bit)? |
Author |
|
JohnFound 23 Feb 2016, 04:49
After the exit of the application, all memory is returned to the system. No leaks at all.
But you can free the memory allocated by sys_brk, for example for long living application, when you don't need this memory anymore. Simply call again sys_brk with EBX smaller than on the first call. Notice that the value in EBX is not the size of the memory, but the upper address of the uninitialized data segment of the program. Also, you must know that after the call to sys_brk, the memory is not actually allocated. It will be allocated on page basis only after the first memory access operation. |
|||
23 Feb 2016, 04:49 |
|
fasmnewbie 23 Feb 2016, 05:01
JohnFound wrote: Simply call again sys_brk with EBX smaller than on the first call. Notice that the value in EBX is not the size of the memory, but the upper address of the uninitialized data segment of the program. |
|||
23 Feb 2016, 05:01 |
|
fasmnewbie 23 Feb 2016, 05:08
JohnFound wrote: It will be allocated on page basis only after the first memory access operation. |
|||
23 Feb 2016, 05:08 |
|
fasmnewbie 23 Feb 2016, 05:14
John you are from bulgaria? Wasn't it you who hacked the Linux Mint download site? Run Forrest, ruunn!!
|
|||
23 Feb 2016, 05:14 |
|
JohnFound 23 Feb 2016, 06:42
I don't like where Linux Mint goes and migrated to Manjaro Linux. But, no, I didn't hacked Linux Mint servers.
sys_brk does not actually allocate memory. It allocates address space for your program. Once allocated, you can access this address space. On first access to some address in this space, exception will arise and the exception handler in the kernel will allocate one page of memory where this address belongs and will return the control to your program. This all process is fully transparent for the application. Only small delay will happen on the first access to every page (regardless of where inside the page you try to read/write). |
|||
23 Feb 2016, 06:42 |
|
redsock 23 Feb 2016, 22:10
FWIW, deallocation via brk works too, though I particularly like the Mac OS X manpage for brk:
Quote: The brk and sbrk functions are historical curiosities left over from earlier days before the advent of virtual memory management. Of course I agree with JohnFound re: memory always being returned to the OS, I wrote a quick demonstration of returning memory via brk included below. All of my linux goods use mmap with MAP_PRIVATE which accomplishes memory management (similar to how all of the malloc libraries do it anyway, though some will still use brk for small allocations early-on). AFAIK the program break will always initially be aligned 16 on x86_64, but this is not something I have verified. Cheers Code: format ELF64 public _start _start: ; get current value of the program break: mov eax, 12 ; syscall brk xor edi, edi ; 0 syscall mov rbx, rax ; save it ; increase by 32MB mov eax, 12 lea rdi, [rbx+33554432] syscall int3 ; breakpoint to observe return value in rax nop ; lower it by 16MB mov eax, 12 lea rdi, [rbx+16777216] syscall int3 ; observe again nop mov eax, 60 ; exit xor edi, edi ; return code syscall |
|||
23 Feb 2016, 22:10 |
|
fasmnewbie 08 Mar 2016, 05:25
redsock
thanks for the answer. I been reading comments discouraging people from using brk and to use mmap or malloc instead. The reason is because of it being used by malloc and to some extend, mmap, giving me this scary impression that sys_brk is too fragile to handle naked. Any idea how and under what circumstances that sys_brk would in anyway be affected by malloc and mmap? AFAIK, whether it is used by malloc, mmap or multiple sys_brk, it will always issue a valid pointer (or none if fail) as per request and I don't see any reason they should interfere with each other. Appreciate your answers. |
|||
08 Mar 2016, 05:25 |
|
fasmnewbie 08 Mar 2016, 05:36
Quote:
I tested on both Win64 (malloc) and Linux64 (sys_brk). The pointers always returned align to 16.... so far. |
|||
08 Mar 2016, 05:36 |
|
redsock 09 Mar 2016, 00:34
fasmnewbie wrote: Any idea how and under what circumstances that sys_brk would in anyway be affected by malloc and mmap? AFAIK, whether it is used by malloc, mmap or multiple sys_brk, it will always issue a valid pointer (or none if fail) as per request and I don't see any reason they should interfere with each other. Appreciate your answers. Allocating memory via mmap only requires extra arguments and is a "cleaner" way to do it IMO. |
|||
09 Mar 2016, 00:34 |
|
fasmnewbie 11 Mar 2016, 14:29
redsock wrote:
Thanks for the explanation. That means I'd be in trouble if I mix my primitive library with C's. |
|||
11 Mar 2016, 14:29 |
|
< Last Thread | Next Thread > |
Forum Rules:
|
Copyright © 1999-2024, Tomasz Grysztar. Also on GitHub, YouTube.
Website powered by rwasa.