# why do we run out of heap space even if we have virtual memory?

Whenever we try to get space in heap(using malloc or new) we can get out of heap space error if it exceeds the limit. But, we have virtual memory so why can't OS allocate that much space and then do swapping when I need some part of it or not?

• The OS enforces (settable) limits on the amount of resources a process may use. For example on linux, try man getrlimit. – JeanPierre Aug 6 '18 at 11:20