call stack vs memory heap
in c++ / java - can either be allocated on stack or heap
stack allocation:
- memory is allocated on the stack whenever a function is called
- the variables in that function get assigned memory in contiguous blocks of memory
- when it’s over the variables are de-allocated
^ called temporary memory allocation because as soon as the method finishes its execution, all data from thatfn is flushed out from the stack automatically
means that any memory in the function is accessible until the method has completed its execution and is in a running state
stack memory has a limit and you get “STACK OVERFLOW” error when the memory is filled
stack memory is considered safer than heap bcoz data stored can only be accessed by the owner thread
allocation and deallocation are faster using stack memory
stack memory has less storage space compared to heap memory
heap memory:
heap is a pile of memory space available to programmers to allocate and deallocate
all objects are created in heap space and references stack memory
not as safe as stack bcoz data saved in heap is available to all threads on the system and can result in memory leak if not handled safely
3 categories of heap:
- young generation
- where new objects are alloacted space and when filled, rest of the data is moved to garbage collection
- old or tenured generation
- part of heap memory to store older data objects not in frequest use / not in use at all
- permanent generation
- ( java ) contains jvm metadata for runtime classes and application methods