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

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

    At least in java, stack will be automatically de-allocated as you leave that stack frame, so there is no need to garbage collect.

    I'm a java programmer so I don't have this problem, but in fact in C++, (I heard that) you will have to be careful with this because you can allocate objects on stack, and you can leave that stack frame, the object will be de-allocated and you can not use it anymore, etc.

提交回复
热议问题