I have a programthat allocates 4096 bytes (4KB) in a loop, and after 4th iteration, I can see that virtual memory size is increased by 16 KB. So it means block size is 16 KB. What is unix command to find this? I can use getconf PAGE_SIZE to get page size which is 4KB, but need to find block size.
-
1This looks like a detail of your allocator library. The [standard Linux page size](http://stackoverflow.com/q/4888067/596781) is 4kB, but the allocator probably gets larger chunks at a time. – Kerrek SB Oct 21 '13 at 22:13
1 Answers
Assuming that you are using glibc
in normal configuration, I would actually expect that your memory allocation grows by 4KB at a time. But maybe the tool you are using to view the size isn't fine-grained enough to show you such a small difference?
Obviously, it may be that your glibc is configured differently than the source I've got, but it does allocate (through sbrk
, which in turn calls the system call brk
) the size of block you asked for, rounded to 4KB (exact source: size = (size + pagemask) & ~pagemask;
, soon followed by if (size > 0) brk = (char*)(MORECORE(size));
.
Of course, if you do x = malloc(4096);
, the actual allocation will be a small number of bytes larger than 4KB, since malloc
needs some extra data to keep track of the allocation itself (such as what size the current allocation is, what size the previous allocation is). The whole overhead for is at least 2 * sizeof(size_t) + 2 * sizeof(some pointer) = 16 bytes on a 32-bit system, 32 bytes on a 64-bit system. It may be more than that.

- 126,704
- 14
- 140
- 227