垃圾收集 [英] Garbage Collection

查看:128
本文介绍了垃圾收集的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我无法理解垃圾收集中的几件事。

首先,数据如何分配空间?即堆栈或堆栈(根据我的知识,所有静态或全局变量都被分配了堆栈空间,局部变量被分配给堆空间)。其次,GC运行关于堆栈或堆的数据?即像Mark / Sweep这样的GC算法会将堆栈上的数据作为根集合对吗?然后通过检查堆上的哪些变量引用根集来映射堆中所有可访问的变量。



如果程序没有全局变量会怎么样?算法如何工作呢?



问候,
darkie

解决方案

这可能有助于澄清你所问的平台的GC - JVM,CLR,Lisp等。这就是说:首先退一步,某些局部变量通常在堆栈上分配。然而,具体细节可能因语言而异。以C#为例,只有本地的值类型和方法参数是存储在堆栈上。因此,在C#中, foo 将被分配到堆栈中:

 公共功能栏(){
int foo = 2;
...
}

另外,动态分配的变量使用内存堆。这应该是直观有意义的,否则每次调用 new 时,堆栈都必须动态增长。而且,这意味着这些变量只能用作分配它们的局部函数中的局部变量,这当然不是真的,因为我们可以有(例如)类成员变量。因此,从C#开始另一个例子,在下面的例子中, result 被分配在堆上:

  public class MyInt 
{
public int MyValue;
}

...
MyInt result = new MyInt();
result.MyValue = foo + 40;
...

现在考虑到这种背景,堆上的内存是垃圾收集。堆栈中的内存不需要GC,因为当前函数返回时内存将被回收。在高层次上,GC算法通过跟踪在堆上动态分配的所有对象来工作。一旦通过 new 分配后,该对象将由GC跟踪,并在其不在范围内时收集,并且不再有引用。


I am not able to understand few things on the Garbage collection.

Firstly, how is data allocated space ? i.e. on stack or heap( As per my knowledge, all static or global variables are assigned space on stack and local variables are assigned space on heap).

Second, GC runs on data on stacks or heaps ? i.e a GC algorithm like Mark/Sweep would refer to data on stack as root set right? And then map all the reachable variables on heap by checking which variables on heap refer to the root set.

What if a program does not have a global variable? How does the algorithm work then?

Regards, darkie

解决方案

It might help to clarify what platform's GC you are asking about - JVM, CLR, Lisp, etc. That said:

First to take a step back, certain local variables of are generally allocated on the stack. The specifics can vary by language, however. To take C# as an example, only local Value Types and method parameters are stored on the stack. So, in C#, foo would be allocated on the stack:

public function bar() { 
    int foo = 2;
    ...
}

Alternatively, dynamically-allocated variables use memory from the heap. This should intuitively make sense, as otherwise the stack would have to grow dynamically each time a new is called. Also, it would mean that such variables could only be used as locals within the local function that allocated them, which is of course not true because we can have (for example) class member variables. So to take another example from C#, in the following case result is allocated on the heap:

public class MyInt
{         
    public int MyValue;
}

...
MyInt result = new MyInt();
result.MyValue = foo + 40;
...

Now with that background in mind, memory on the heap is garbage-collected. Memory on the stack has no need for GC as the memory will be reclaimed when the current function returns. At a high level, a GC algorithm works by keeping track of all objects that are dynamically allocated on the heap. Once allocated via new, the object will be tracked by GC, and collected when it is no longer in scope and there are no more references to it.

这篇关于垃圾收集的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆