0

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.

1
  • 1
    This looks like a detail of your allocator library. The standard Linux page size is 4kB, but the allocator probably gets larger chunks at a time. Commented Oct 21, 2013 at 22:13

1 Answer 1

0

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.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.