垃圾收集器收集堆栈内存、堆内存还是两者都收集?
我读了很多关于垃圾收集的文章,几乎所有文章都讲述了堆内存。所以我的问题是“垃圾收集收集堆栈内存或堆内存或两者”。
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".
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(9)
堆栈之所以被称为“堆栈”,正是因为它是一个使用“堆栈策略”管理的内存区域,即 LIFO(后进先出)。如果堆栈上的分配不是以“堆栈方式”完成的,那么它就不会被称为堆栈,而是称为堆。
垃圾收集的发明是为了解决在堆上分配东西的问题,即您无法预测哪些部分将首先被释放。 GC 适用于堆栈管理不充分的内存分配问题。
The stack is called a "stack" precisely because it is a zone of memory which is managed with a "stack policy", aka LIFO (as Last In, First Out). If allocation on the stack was not done in "the stack way" it would not be called a stack but heap.
Garbage Collection was invented in order to cope with the problem of allocating things on a heap, i.e. such that you cannot predict which parts will be released first. GC is meant for memory allocation problems where stack management is not sufficient.
堆栈是后进先出的,因此不需要垃圾收集。
——更正——呃!
Stack is last in first out so there is no need to garbage collect.
--corrected-- duh!
堆栈是方法参数和局部变量所在的位置。如果离开该方法,堆栈指针会自动递减(或根据具体实现递增)。对于大多数编程语言来说都是如此。
相反,垃圾收集仅适用于堆。
the stack is where your method arguments and local variables lie. if you leave the method, the stack pointer is automatically decremented (or incremented depending on the specific implementation). This is true for most programming languages.
garbage collection in contrast only works on the heap.
至少在java中,当您离开该堆栈帧时,堆栈将自动取消分配,因此不需要垃圾收集。
我是一名java程序员,所以我没有这个问题,但实际上在C++中,(我听说)你必须小心这一点,因为你可以在堆栈上分配对象,并且可以保留该堆栈帧,该对象将被取消分配,您不能再使用它,等等。
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.
除非您使用的是我们都不知道的分析器,否则堆是主要问题。这是因为大多数人只是对广受好评的工具告诉他们的任何内容做出反应。请读到本文的结尾,看看大多数指出静态分配内存错误的分析工具通常都是正确的。分析不应只是简单的泄漏和垃圾收集。
让我给你举一个C语言的例子:
如果运行这个程序的操作系统没有自动回收堆,我就造成了一个问题。我想不出有哪个现代操作系统不执行实际使用的操作。
现在让我们看一下:
您的编译器如何分配,这取决于您的编译器。如果您使用它并且可以修改结果,则您的编译器可能已损坏。然而,如果我有 100 个线程同时进入该函数,那么结果肯定是垃圾,除非我的编译器神奇地弄清楚我的意思并引入互斥或动态分配,而我不必担心它,此时我们回到分析堆。
简而言之,在我们的一生中,垃圾收集与堆有关。指望未来对堆栈进行一些尝试并尝试“优化”事物,然后指望这种努力成为一群首席执行官将需要的解释语言。
这并不意味着忽略内存错误是一件好事,无论存储是什么。
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:
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:
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.
Java中的垃圾收集器仅在堆内存上工作,而不在堆栈内存上工作,因为堆栈工作的主要原理是(后进先出)。这本身就解释了一切。即当函数范围结束时到达时堆栈自动清空。
Garbage Collector In Java works only on Heap memory and not on stack memory , because of the main principal that stack works on which is ( Last In First Out).That explains it all in itself.l i.e. when the end of scope of function is reached the stack is automatically empty.
您没有引用任何特定技术,但跨语言的使用相当典型。
垃圾收集器仅在托管堆上工作。单个进程中可能有多个堆,其中一些堆没有被垃圾收集。
当方法返回时,在堆栈上分配的变量将被释放。垃圾收集器将使用这些变量来查找活动引用,但它不会收集内存。
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.
它收集堆内存。通常,当执行路径到达作用域末尾时,堆栈内存会自动收集。例如:
事实上,在像C++这样的语言中,堆栈分配的变量被称为
auto
变量。It collects heap memory. Usually, stack memory is collected automatically when the execution path reaches the end of the scope. e.g.:
In fact, in a language like C++, stack allocated variables are called
auto
variables.堆内存。
垃圾收集是一种释放不再使用的内存的方法。有时“不再使用”部分很棘手。有了堆栈,一旦函数返回,我们就可以确信(除了程序员错误)局部变量不再被使用,因此它们在几乎每种语言/运行时都会自动释放。
Heap memory.
Garbage collection is a method of deallocating memory that isn't being used anymore. Sometimes the "isn't being used anymore" part is tricky. With the stack, as soon as a function returns, we can be confident (excepting programmer error) that the local variables aren't being used anymore, so they are deallocated automatically at that time in nearly every language/runtime.