有关GC的基本问题 [英] Basic question on GC

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

问题描述

using System;

class Calci
{

    public int Add(int a, int b)
    {

        return (a + b);

    }

    public int Sub(int a, int b)
    {

        return (a - b);

    }

    public int Multi(int a, int b)
    {

        return (a * b);

    }

    public int Divide(int a, int b)
    {

        return (a / b);

    }

}



class GCExample3
{

    public static void Main(string[] args)
    {

        Calci oCalci = new Calci();

        Console.WriteLine("Calci object is now on " + GC.GetGeneration(oCalci) + " Generation");
        Console.WriteLine("call to GC.Collect(0):");
        GC.Collect(0);
        Console.WriteLine("Garbage Collection Occured in 0th Generation:" + GC.CollectionCount(0));
        Console.WriteLine(oCalci.Add(9, 0));
        Console.ReadLine();
    }

}



输出:



output :

Calci object is now on 0 Generation
 call to GC.Collect(0):
Garbage Collection Occured in 0th Generation:1
9


我的问题是:
如果我的calci对象在第0代中被垃圾回收,那么之后它如何调用calci类的方法?


My question is that:
If my calci object is garbage collected in 0th generation then how after that it calls to methods of calci class?

推荐答案

关注:
http://msdn.microsoft.com/en-us/library/xe0c2357.aspx [ ^ ]
Follow:
http://msdn.microsoft.com/en-us/library/xe0c2357.aspx[^]


因为您的oCalci对象不是,所以垃圾收集器无法收集oCalci对象.而且我认为收集的对象是在后台仍未收集的对象,并且我认为收集的对象不是您的oCalci对象,因为如果我不声明oCalci对象,如果我进行垃圾收集,GC也会收集1个对象.
Because your oCalci object is not null, the garbage collector cann''t collect the oCalci object. And I think the collected object is an object in the background, that''s not still collected, and I think the collected object isn''t your oCalci object because if I don''t declare the oCalci object, and if I do a garbace collection, the GC collected 1 object also.


这篇关于有关GC的基本问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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