Does a garbage collector collect stack memory, hea

2019-04-04 07:25发布

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 both".

8条回答
家丑人穷心不美
2楼-- · 2019-04-04 08:15

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.

查看更多
Fickle 薄情
3楼-- · 2019-04-04 08:21

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.

查看更多
登录 后发表回答