Memory Management – What Happens When Both Heap and Stack Are Full?

heapstackoverflow

Suppose I am using a very heavy applications(or you can say that it is a very bad code which keeps on allocating memory on heap and won't free it and also uses recursion) Now after some point of time both heap and stack will have no space to expand.

Now In this situation heap and stack don't have any free space in between them and if I try to allocate some memory on heap then what will happen? will it overflow over stack?

And also what happens if we make a recursive call and stack needs to expand then will it overflow over heap?

Best Answer

The system will detect this and cause an error. Older languages just simply crash the program. Newer languages generally throw an exception. But handling those exceptions might be problematic, because there is not enough memory or stack for it.

Related Topic