2

Following the first comment on this question: What makes this function run much slower?

Does the garbage collector sweep stack memory? From what I've read, usually gc's don't do this.
Following this question, I imagine that there is no physical difference between stack and heap memory; is there a virtual division? What I mean is: what happens when theoretically all stack memory is used without causing an overflow and new memory is allocated to an object after that?

Could someone elaborate on how this actually works? Thanks.

Community
  • 1
  • 1
html_programmer
  • 18,126
  • 18
  • 85
  • 158
  • Stack and heap are only different in the way they are allocated and freed. However, objects are pretty much always allocated from the heap, and a **reference** to the object would be stored in a stack variable (a function-local variable). Upon function return, the stack used by the function is freed, and the **reference count** to the heap object reduced. Then, GC takes care of freeing the object from the heap. – Kenney Jul 29 '15 at 11:23
  • I'm aware that a reference is stored on the stack, but ultimately I imagine that everything (incl. the object itself) gets stored in physical ram. I wondered how the division between stack / heap memory is done by the js engine. As you also mention, I don't see a clear indication that stack memory would be freed by the gc. – html_programmer Jul 29 '15 at 11:28
  • 1
    Stack memory would be handled by the JS engine; how that's implemented depends entire on the JS implementation. – Dave Newton Jul 29 '15 at 11:48
  • 1
    Stack is handled by CPU instructions such as `call`, `ret`, `push` and `pop`, dealing with the stack register `esp` (on intel). Very low-level. It's a region of RAM set up by the OS to handle function calls. A `call` would `push` the offset of the next instruction onto the stack and then `jmp` to the called function. The function terminates with `ret`, which `pop`s the return address and `jmp`s there. Heap on the other hand is a region of memory set up by the OS, usually after the program image, and can be resized with a kernel call. It is managed with `malloc` and `free` library functions. – Kenney Jul 29 '15 at 11:50
  • Thanks Dave and Kenney, that clarifies it. I would accept it as answer as well though. – html_programmer Jul 29 '15 at 11:53

1 Answers1

4

Does the garbage collector sweep stack memory?

No. The garbage collector does only manage heap memory. All values on the stack are expected to be needed again when the program returns to that stack frame, so they must not be collected. The references from the stack into the heap are indeed considered alive.

The stack memory is cleared automatically when a function exits.

Of course, what parts of a program go onto the stack and which go into the heap is not easy to decide in a dynamic language like JavaScript. Some optimisations allow objects to be allocated on the stack, and closures might require that variable environments are allocated in the heap.

I imagine that there is no physical difference between stack and heap memory; is there a virtual division?

That's true. "The stack" is just a (typically fixed-size) region of your computers memory, dedicated to be "the stack" by some process. Indeed there are many stacks living in your memory, one for each thread, and interpreters (e.g. for JS) create their own stacks as well.

Bergi
  • 630,263
  • 148
  • 957
  • 1,375