Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Out of Memory Concepts

I'm currently learning about low level arduino stuff including memory. My question is since heap and stack grow at the opposite side of the memory, does out of memory only happens if both meet somewhere in the middle, or does it happen when one of them hit the middle.

like image 797
Joey Arnold Andres Avatar asked Oct 06 '22 00:10

Joey Arnold Andres


1 Answers

  • there's no notion of "middle"
  • depending on how the underlying operating system (if any at all!) manages the processes (if there is such thing on the platform!), the stack may have a limited size, much smaller than "the rest of whole memory"
  • depending on how the underlying operating system (if any at all!) manages the virtual memory space (if there is such thing on the platform!), the heap may have a limited size, much smaller than "the rest of whole memory"

If any of those limits are in place, then there is a very tiny probability of those regions meeting each other. Actually, most probably they will sooner simply run out of space, and the guarding mechanisms will raise relevant hardware of software exceptions.

This means that the 'out of memory' event is not a one thing. There's OOM in stack, and OOM in heap. In my parts of world, traditionally, that related to the stack is called StackOverflow :) and the one related to heap is OutOf[Heap]Memory.

Notably, if your platform has a notion of virtual memory, then the stack most probably is still a singular block of space, but heap - will probably be a sparse construct, and will consist of multiple scattered blocks of space, not necessarily physically ordered in some ascending or descending way. In such case, there's hard to speak about anything meeting anything. The StackOverflow happens when stack hits the size limit, and OutOfMemory happens when the memory manager cannot find a suitable free hole in the memory space.

If to ignore virtual memory, and if talking only about 'raw hardware', then it depends on .. where the stack is really located. IIRC(!), on some (really old) platforms the stack was preallocated at some predefined space situated at the beginning of the physical memory, let's say first ten pages, and grew 'to the bottom'. Then were a few pages of hardware mapped tables and port state images, and then the remaining tail was the heap. With such setup, the StackOverflow is risen when the stack pointer reaches zero. It was quite 'smart' because detecting zero is easy with processor's status flags.. Also, for those old processors it was far easier to reach lower adresses than higher ones, so putting the often-accessed stack in the lower adresses was a big plus.

IIRC(!), newer platforms do not have those problems (usually, let's ignore NUMA and similar things), and the current traditional 'raw hardware' setup putting special areas (tables, portmaps, etc) at the beginning of space, then Heap, then 'free area' then the Stack, and StackOverflow happens when the free area is gone, that is when the stack meets any block already by the heap. Note, that StackOverflow still does not mean OutOfMemory: the stack might have overflown, but the heap may have some gaps and still might have lots of free space!

Please be aware that those 'IIRC's are important. I'm not an expert on Arduino and current processor architectures, and also the 'historical' blargh above might be quite off in terms what was newer/older. I give it probability of 80% to be true.

like image 94
quetzalcoatl Avatar answered Oct 10 '22 01:10

quetzalcoatl