Does a garbage collector collect stack memory, heap memory, or both?

前端 未结 9 2745
独厮守ぢ
独厮守ぢ 2021-02-19 04:13

I read lot of articles about garbage collection and almost all article tells about heap memory. so my question is \"garbage collection collects stack memory or heap memory or bo

相关标签:
9条回答
  • 2021-02-19 04:22

    The stack is called a "stack" precisely because it is a zone of memory which is managed with a "stack policy", aka LIFO (as Last In, First Out). If allocation on the stack was not done in "the stack way" it would not be called a stack but heap.

    Garbage Collection was invented in order to cope with the problem of allocating things on a heap, i.e. such that you cannot predict which parts will be released first. GC is meant for memory allocation problems where stack management is not sufficient.

    0 讨论(0)
  • 2021-02-19 04:34

    Heap memory.

    Garbage collection is a method of deallocating memory that isn't being used anymore. Sometimes the "isn't being used anymore" part is tricky. With the stack, as soon as a function returns, we can be confident (excepting programmer error) that the local variables aren't being used anymore, so they are deallocated automatically at that time in nearly every language/runtime.

    0 讨论(0)
  • 2021-02-19 04:35

    You don't cite any particular technologies, but the use is fairly typical across languages.

    The garbage collector only works on the managed heap. There may be multiple heaps in a single process, some of which are not garbage collected.

    Variables allocated on the stack are deallocated when a method returns. The garbage colletor will use those variables to find live references, but it will not collect the memory.

    0 讨论(0)
  • 2021-02-19 04:36

    the stack is where your method arguments and local variables lie. if you leave the method, the stack pointer is automatically decremented (or incremented depending on the specific implementation). This is true for most programming languages.

    garbage collection in contrast only works on the heap.

    0 讨论(0)
  • 2021-02-19 04:38

    Garbage Collector In Java works only on Heap memory and not on stack memory , because of the main principal that stack works on which is ( Last In First Out).That explains it all in itself.l i.e. when the end of scope of function is reached the stack is automatically empty.

    0 讨论(0)
  • 2021-02-19 04:38

    Stack is last in first out so there is no need to garbage collect.

    --corrected-- duh!

    0 讨论(0)
提交回复
热议问题