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

前端 未结 9 2747
独厮守ぢ
独厮守ぢ 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.

    0 讨论(0)
  • 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.

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

    Unless you are using a profiler that none of us know about, heap is the major concern. This is because most people just react to whatever a highly acclaimed tool tells them. Please get to the end of this post to see that most profiling tools that point out statically allocated memory errors are usually correct. Profiling should go beyond simple leaks and garbage collection.

    Let me give you an example in C:

    #include <stdlib.h>
    #include <string.h>
    
    int main(void)
    {
       char *am_i_leaking;
    
       am_i_leaking = strdup("Now, that's subjective!");
    
       return 0;
    }
    

    If the OS that runs this program does not automatically reclaim heap, I've caused a problem. I can't think of a modern OS that does not do that which is actually in use.

    Now lets look at this:

    char *foo(void)
    {
        static char bar[1024];
        memset(bar, 0, sizeof(bar));
        snprintf(bar, sizeof(bar -1), "Do wa diddy diddy dum diddy do");
    
        return bar;
    }
    

    How your compiler allocates that is, well, up to your compiler. If you use it and can fiddle with the result, you probably have a broken compiler. Yet, if I have 100 threads entering that function at once, surely, the result is rubbish, unless my compiler magically figures out what I meant and introduces mutual exclusion or dynamic allocation without me having to worry about it, at which point we're back to profiling heap.

    In short, in our lifetime, garbage collection pertains to heap. Count on some taking stabs at the stack in the future and trying to 'optimize' things, then count on that effort becoming an interpreted language that a bunch of CEO's will demand.

    That doesn't mean ignoring memory errors is a good thing, no matter what the storage happens to be.

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