简体   繁体   English

关于内存大小,清理对象

[英]Regarding memory size, cleaning up objects

Consider an example as below 考虑如下示例

class B
{
   int x;
}

class A
{
     int a;
     int b;
     B b1 = new B();

     void testMethod()
     {
        for (int i = 1; i < 10; i++)
            b1 = new B();
     }
}

class MainClass //Will never close this application
{
    static void Main(object[] args)
    {
        A a1 = new A();
        a1.testMethod();
    }
}

In this above example am creating a new Class B object everytime in the loop. 在上面的示例中,每次在循环中都创建一个新的B类对象。 So here my question is when i assign a new object to b1, what will happen to the previous object will that be cleaned up or will it remain up memory. 因此,这里的问题是,当我将新对象分配给b1时,将清除前一个对象或保留它的内存。

If it is not cleaning up and increasing the memory, how to clean up? 如果没有清理并增加内存,该如何清理?

Each time you create a new object of B 每次创建B的新对象

b1 = new B();

the original object stored in b1 becomes inaccessible. 存储在b1的原始对象变得不可访问。 This means that it is eligible for garbage collection. 这意味着它有资格进行垃圾回收。 This does not mean that it will be cleaned from memory immediately though! 并不意味着它会从内存虽然立即清理!

Objects that are eligible for GC will be actually GC'ed at "some time in the future". 符合GC条件的对象实际上将在“将来的某个时候”进行GC处理。 We don't know and don't need to know what time exactly. 我们不知道,也不需要知道确切的时间。 Just trust the GC. 只要相信GC。

In other words, after you run your testMethod , 9 objects of B will be eligible for GC or has already been collected, while the tenth object will remain stored in the variable b1 . 换句话说,在运行testMethodB 9个对象将有资格使用GC或已被收集,而第十个对象将保留存储在变量b1

Each time you do this: b1= new B(); 每次您这样做: b1= new B(); , you are assigning to b1 a new reference to a new object ( b1 is not a ValueType ), so, the previous reference is now unused, thus, the Garbage Collector is going to dispose it. ,您正在向b1分配对新对象的新引用( b1 不是 ValueType ),因此,先前的引用现在未使用,因此,垃圾回收器将对其进行处理。

Summarizing: is not increasing the memory, because it is a Managed Resource 总结:没有增加内存,因为它是托管资源

Life of any value type and reference type is depend on access specifier used to defined it and scope of that functionality. 任何值类型和引用类型的寿命取决于用于定义它的访问说明符和该功能的范围。 Functionality can be any class, function or loop where you defined your object. 功能可以是定义对象的任何类,函数或循环。

In your case you defined reference type B b1 = new B() with out any access specifier (default access specifier) so it will be considered as internal . 在您的情况下,您定义的引用类型B b1 = new B()没有任何访问说明符(默认访问说明符),因此将其视为internal variable defined with internal can not be accessible to outside world. 内部定义的变量不能被外界访问。

Scope of b1 is limited to for loop which you defined your code. b1范围仅限于您定义代码的for循环。

for (int i = 1; i < 10; i++)
            b1 = new B();

If you notice above two conditions, you will understand object b1 is disposed by garbage collector after each iteration of for loop . 如果您注意到以上两个条件,则将理解在每次for循环迭代之后, 垃圾回收器都会处理对象b1

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM