2

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?

1

1 Answer 1

5

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.

2
  • 1
    … which is why some paranoid programs reserve a bit of memory on startup which can be used during cleanup.
    – amon
    May 2, 2014 at 18:10
  • Another strategy is to design your program so that it has a known max memory usage and manage memory in pools. For example console games.
    – BlamKiwi
    Jan 19, 2015 at 5:35

Your Answer

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

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