1
$\begingroup$

I'm having a bit of trouble understanding how less than a page of memory can be used.

From what I understand, memory is given out in page size chunks - say pages are 4096 bytes, and 4 bytes are requested in a program via malloc, can less than a size of a page be allocated? Or is the rest of the memory in the page wasted?

$\endgroup$
1
$\begingroup$

You are confusing paging and memory allocation.

Paging is used to implement virtual memory: Your logical address space is divided into pages, and each page can be mapped to a physical address in RAM, or can be assigned to some location in your backing store. Because the computer has to keep track of how each page is used, we make the pages reasonably large to avoid having an unreasonably large number of pages.

malloc doesn't care about pages at all. It assumes that some portion of your logical address space is available for allocating memory. It doesn't care how this logical address space is mapped to RAM or backing storage. It just takes four bytes from your logical address space and keeps track that this memory is used. If - by coincidence - all but 100 bytes of a page had been used by malloc (which malloc doesn't know about and doesn't care about), and you called malloc to allocate another 200 bytes, you would likely get the last 100 bytes of that page and the next 100 bytes of the next page. malloc doesn't care.

Paging also doesn't care about malloc, it only cares about reading and writing data.

$\endgroup$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.