简体   繁体   English

从非静态 class Z240AA2CEC4B29C56F3BEE520ADCEE7E 中的 static 方法中的对象释放 memory 块

[英]Releasing memory blocks from objects in static methods inside a non-static class c#

I'm making use of an static method inside a non-static class,each time that static method is called, a new instance of an object is created I'm making use of an static method inside a non-static class,each time that static method is called, a new instance of an object is created

       public  class BaseMethodsExecuter<T> where T:class
        {
            public static T Execute(BaseMethodsParams input, BaseBusinessEnum businessEnum) 
            {
                T data=null; 
                try
                {
                  data = ExecuteMethod(input, businessEnum);
                }
                catch (Exception ex)
                {
    
                }
                return data;
            }
            private static T ExecuteMethod(BaseMethodsParams input, BaseBusinessEnum businessEnum)
            {
              // this is the line that fell me to thinking 
                var TypeMethod = typeof(BaseDataAbstract<T>).Assembly.GetTypes().Single(t => t.BaseType==(typeof(BaseDataAbstract<T>)) && !t.IsAbstract && ((BaseDataAbstract<T>)Activator.CreateInstance(t)).BaseBusinessMethod== businessEnum);
                var BaseMethod=(BaseDataAbstract<T>)Activator.CreateInstance(TypeMethod);
                var data=BaseMethod.GetBaseData(input);
                return data;
            }
        }

Following piece of code creates object in static method以下代码在 static 方法中创建 object

((BaseDataAbstract<T>)Activator.CreateInstance(t))

As far as i know, Static objects hold their memory blocks in stack part of the memory.据我所知, Static objects将其 memory 块保存在 memory 的stack部分中。 Does it means that each time this method is called an extra space for instantiated object is occupied and never would be released anymore?是不是意味着每次调用这个方法都会有一个额外的空间用于实例化的 object 被占用,再也不会被释放了?

How objects inside static methods are cleaned from memory?如何从 memory 中清除 static 方法中的对象?

First: Reference Object are stored in the heap.第一:参考Object存放在堆中。 All reference pointers to this object are stored in the stack.指向此 object 的所有引用指针都存储在堆栈中。 When there are no more pointers in the stack, the object are removed from the heap (See garbage collector).当堆栈中没有更多指针时,object 将从堆中移除(参见垃圾收集器)。

That being said: A static object is static reference pointer in the heap to an object in the stack, for that reason the object never gets disposed. That being said: A static object is static reference pointer in the heap to an object in the stack, for that reason the object never gets disposed. In your example (((BaseDataAbstract<T>)Activator.CreateInstance(t)).BaseBusinessMethod== businessEnum);在您的示例(((BaseDataAbstract<T>)Activator.CreateInstance(t)).BaseBusinessMethod== businessEnum); creates an instance but the instance is not assigned to static variable.创建一个实例,但该实例未分配给 static 变量。 Same for var BaseMethod=(BaseDataAbstract<T>)Activator.CreateInstance(TypeMethod);同样适用于var BaseMethod=(BaseDataAbstract<T>)Activator.CreateInstance(TypeMethod); , they are assigned to a scoped variable inside ExecuteMethod . ,它们被分配给ExecuteMethod内的范围变量。 Once we jump out of the method, these variables (stack) disappear and the reference objects in the heap disappear when the GC disposes them.一旦我们跳出方法,这些变量(栈)就会消失,而堆中的引用对象也会在 GC 处理它们时消失。

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

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