# Swap and Memory: Stack, Heap, or Both?

I understand what stack and heap refer to. However, I struggle with understanding exactly swap can do.

For example, let's take Linux as the OS. I connect another hard disk to my machine, and I make additional swap space available (using space from the said disk) via mkswap and swapon commands. It is known that swap space can accommodate dynamically-allocated variables on the heap. So with a newly increased swap space, malloc can allocate larger variables.

Question: Is swap space also used by the OS (any OS) to augment the stack portion of memory?

• In general the type of use of a memory page is not considered when the OS is choosing a page to swap (page table pages and OS code are typically exceptions) but rather the estimated likelihood that the page will be referenced in the near future. – Paul A. Clayton Feb 18 '16 at 11:47
• I'm not sure this is a conceptual computer science question. It seems to be about implementation details of specific OSs. I'll let OS experts decide if there are underlying principles worth drawing upon. Community votes, please! – Raphael Feb 18 '16 at 13:56
• There is no restriction, but you should hope it does not land there. Think about one underlying trivia that explains a lot - malloc returns continous memory. If you can convince your OS that memory from more than one disk is continous-there are no limits. Stack due to usage should not be vey big, from pressure it often lands in CPU cache for execution. But if you use huge stack on separate disk - this is not what you were wishing for. By common usage the stack has its space at lower adresses bound by RLIMIT_STACK to 8MV. A few GB gap in stack... Please don't. – Evil Feb 18 '16 at 17:06