Is the garbage collector in Java?

Heap memory is garbage collected in Java.

Is the stack collector going to?

How is stack memory recovered?

+48
java garbage-collection gc-roots
Mar 15 '10 at 13:42
source share
9 answers

The memory in the stack contains the method parameters and local variables (more precisely: object references and the variables themselves for primitive types). It will be automatically deleted if you leave this method. If the variables are references (objects), the objects themselves are on the heap and processed by the garbage collector.

Thus, the stack is not garbage collected just like a heap, but the stack is a form of automatic memory management in it (which precedes garbage collection).

A more detailed answer is given by Thomas Pornin , look at this in more detail.

+33
Mar 15 '10 at 13:46
source share

The stack is not garbage collected in Java.

The stack allocated for this method call is freed when the method returns. Since this is a very simple LIFO structure, there is no need for garbage collection.

One place where the stack and garbage interact is that the links in the stack are the roots of the GC (which means that they are the root links from which reachability is decided).

+28
Mar 15 '10 at 13:46
source share

The stack can be garbage collected. However, in most JVM implementations it is treated as, for example, a "stack", which by definition excludes garbage collection.

What we call the stack is the accumulation of method activation contexts: for each method called, this is a conceptual structure that contains the method arguments, local variables, a hidden context pointer for the calling method and a slot for save the instruction pointer. The activation context is not available as such from the Java language itself. The context becomes useless when the method exits (using return or due to an exception being thrown). It so happens that when method A calls method B, it is guaranteed that when A regains control, the context of B becomes useless. This means that the context lifetime for B is a sub-range of the context lifetime for A. Therefore, activation contexts (for a given stream) can be allocated using the LIFO ("Last In, First Out") discipline. In simpler words, a stack: a new activation context is placed on top of the context stack, and the first context will be deleted.

In practice, activation contexts (also called stack frames) are concatenated in stack order in the selected area. This area is obtained from the operating system when the thread starts, and the operating system returns it when the thread terminates. The top of the stack is indicated by a specific pointer, which is often contained in the CPU register (this depends on whether the JVM interprets or compiles the code). The "caller context pointer" is virtual; the caller context is necessarily located just below in stack order. GC does not intervene: the stack area is created and regenerated synchronously, from the activity of the stream itself. It also works in many languages, such as C , which do not have a GC at all.

Now, nothing prevents the implementation of the JVM otherwise, for example. highlighting activation contexts on the heap and collecting them with GC. This is usually not done in Java virtual machines, since stack allocation is faster. But some other languages ​​should do such things, especially those that play with continuations , still using the GC (like Scheme and its call-with-current-continuation ), because such games violate the LIFO rule above.

+14
Mar 16 '10 at 11:38
source share

The batch portion of the memory works the same as the "stack". I know this sounds bad, but that's exactly how it works. The data is added on top, one on top of the other ( pushed onto the stack ) and automatically deleted from the top ( popped off the stack ) as your program starts. This is not garbage collection - and it is not necessary, since this memory is automatically restored after the data is deleted from the stack. And when I talk about the fix, I don’t mean that it is de-allocated - it’s just that the location in the memory of the stack where the next data will be stored is reduced as the data is deleted.

Of course, not to say that you do not need to worry about the stack at all. If you run a recursive function many times, it will eventually use the entire stack space. The same thing if you call many functions, especially if they have many parameters and / or local variables.

But the bottom line is that the stack memory is used and fixed, as functions enter and leave the area automatically. Thus, at the end of your program, all of the stack memory will be free, and then released back to the operating system.

+8
Mar 15 '10 at 13:50
source share

If you reference the memory used on the stack, this is not garbage collection.
The Java virtual machine uses explicit bytecode instructions to reserve and free memory on the stack, these instructions are generated by the compiler and control the lifetime of primitives such as int, boolean, double, and object-references on the stack. It is planned to implement the so-called tail call optimization, which will remove some entries from the stack as soon as it becomes known that they are no longer in use, but I do not know any jvm that already supports this.
Therefore, there is no garbage collection for the stack itself, only the compiler generates push and pop commands to control memory usage.

The stack itself is part of the thread. The stack is allocated when creating the stream object and garbage collection after the termination of the stream, and the stream object is no longer referenced.

+4
Mar 18 '10 at 15:43
source share

All objects in Java are allocated on the heap. (At least as far as the specification goes, the actual implementation may allocate them on the stack if they transparently behave as if they were on the heap.)

What is being collected is a little subtle. If a single reference to an object is on the same stack stack, and it can be shown that the link will not be used again, then the object can be assembled. If the object is used only for reading a field, then this field can be optimized forward, and the object is assembled earlier than you might expect.

This usually doesn't matter unless you use finalists (or supposedly Reference s). In this case, you should be careful and use / volatile locks to ensure happens-before relationships.

When threads stop, then usually the whole stack will be freed.

+1
Mar 15 '10 at 16:14
source share

Everything on the stack is considered global roots by the garbage collector. So yes, you can definitely say that the stack is “garbage collection”.

+1
Mar 15 '10 at 16:23
source share

No one, data is pushed and pushed from the stack, since you have internal variables in methods, during method calls, etc. You do not need to care about this.

0
Mar 15 '10 at 13:48
source share

No. The stack is not garbage collected in Java. Each thread has its own stack and contains:

  • Method-specific values ​​(which are short-lived) and
  • References to objects created on the heap and reference the method

These values ​​are pushed onto the stack of frames on the stack for each method call. Since the stack follows the "Last-in First-Out" order, at the end of each method call, each stack stack containing all the data specific to the method and object references, if any, are unloaded.

Therefore, the data in the stack is automatically cleared after the method / program goes beyond.

0
Feb 23 '16 at 10:44
source share



All Articles