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

前端 未结 9 2774
独厮守ぢ
独厮守ぢ 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:41

    It collects heap memory. Usually, stack memory is collected automatically when the execution path reaches the end of the scope. e.g.:

    void fun()
    {
      int n; // reservation on the stack as part of the activation record
      ...
    } // returning the stack pointer to where it was before entering the scope
    

    In fact, in a language like C++, stack allocated variables are called auto variables.

提交回复
热议问题