简体   繁体   English

具有静态方法的C#类实例与静态类内存使用情况

[英]C# class instance with static method vs static class memory usage

How does C#, or other languages for that matter, handle memory allocation (and memory de-allocation) between these two scenarios: 在这两种情况之间,C#或其他语言如何处理内存分配(和内存解除分配):

1.) A method on a static class is invoked. 1.)调用静态类的方法。

public Program {
    Foo foo = Loader.load();
}

public static Loader {
    public static Foo load() {
        return new Foo();
    }
}

2.) A method is invoked on an instance, which then falls out of scope. 2.)在一个实例上调用一个方法,然后该方法超出范围。

public Program {
    Foo foo = new Loader().load();
}

public Loader {
    public Foo load() {
        return new Foo();
    }
}

I suppose the static class is loaded, and remains, in memory; 我想静态类在内存中被加载并保留; whereas the class instance succumbs to garbage collection at C#'s leisure. 而C#的闲暇时,类实例屈服于垃圾收集。 Are there any pros or cons to these two paradigms? 这两种范式是否有任何利弊? Is there ever a time when you have a class that never needs to be instantiated (ie some sort of resource loader or factory), but you use the second methodology anyway to take advantage of garbage collection? 有没有时间你有一个永远不需要实例化的类(即某种资源加载器或工厂),但你还是使用第二种方法来利用垃圾收集?

The important part of my question is whether or not the first paradigm, while being conceptually correct in some circumstances, may suffer from holding on to memory unnecessarily. 我的问题的重要部分是,第一范式是否在某些情况下在概念上是正确的,可能会遭受不必要的记忆。

Your second example doesn't work, so let's explore the real options: 你的第二个例子不起作用,所以让我们探索真正的选择:

1.) A method on a static class is invoked. 1.)调用静态类的方法。

public Program {
   Foo foo = Loader.Load();
}

public static Loader {
   public static Foo Load() {
      return new Foo();
   }
}

2.) A static method in a non-static class is invoked. 2.)调用非静态类中的静态方法。

public Program {
   Foo foo = Loader.Load();
}

public Loader {
   public static Foo Load() {
      return new Foo();
   }
}

3.) An instance method is invoked on an instance 3.)在实例上调用实例方法

public Program {
   Foo foo = new Loader().Load();
}

public Loader {
   public Foo Load() {
      return new Foo();
   }
}

The two first are the same. 两者首先是相同的。 Calling a static method is the same regardless if the class is static or not. 无论该类是否为静态,调用静态方法都是相同的。

The third option will create an instance of the class on the heap. 第三个选项将在堆上创建类的实例。 As the class has no data members, it will only be something like 16 bytes. 由于该类没有数据成员,因此它只有16个字节。 It will be garbage collected eventually, but due to the small size it doesn't matter much when that happens. 它最终将被垃圾收集,但由于体积小,发生这种情况时并不重要。

Calling an instance method is also slightly different from a static method. 调用实例方法与静态方法略有不同。 A reference to the class instance is sent along, that you can access through the this keyword. 将发送对类实例的引用,您可以通过this关键字进行访问。 It makes little difference in this case as there is no real data in the object to access. 在这种情况下几乎没有区别,因为要访问的对象中没有真实数据。

A static method, field, property, or event is callable on a class even when no instance of the class has been created. 即使没有创建类的实例,静态方法,字段,属性或事件也可在类上调用。

http://msdn.microsoft.com/en-us/library/79b3xss3(VS.80).aspx http://msdn.microsoft.com/en-us/library/79b3xss3(VS.80).aspx

So in that sense your static methods behaves just as it would if you used it from within a class instance: it is scoped to the type. 因此,从这个意义上讲,静态方法的行为与在类实例中使用它时的行为一样:它的作用域是类型。

The second form creates a temporary Loader object (which is very cheap). 第二种形式创建一个临时的Loader对象(非常便宜)。 You will always have to load the Loader class, no matter which approach you choose. 无论您选择哪种方法,您都必须加载Loader类。

There is very little performance (memory saving) to gain here. 这里获得的性能(内存节省)非常少。 You would normally choose for a static member in a static class if there is no 'state' needed outside the methods local vars. 如果在本地变量方法之外不需要“状态”,则通常会选择静态类中的静态成员。

I cannot find any sources for this, but from my knowledge of programming, when you refernce a class(non static), it's structure is loaded into memory 我找不到任何这方面的资源,但是根据我的编程知识,当你引用一个类(非静态)时,它的结构被加载到内存中

Creating an instance of a class just to call a method, would waste a lot of processing power(due to creating an instance, assigning it memory, and the garbage collecting). 创建类的实例只是为了调用方法,会浪费大量的处理能力(由于创建实例,分配内存和垃圾收集)。

Instead of keeping the definition, and then on top of it, an instance. 不是保留定义,而是保持定义,而不是保持定义。 Why not just keep the definition(static). 为什么不保持定义(静态)。

As long as you don't store any data in static variables, your static method should take up the same amount of memory as your non static method definition. 只要您不在静态变量中存储任何数据,静态方法就应该占用与非静态方法定义相同的内存量。 But using a static method, only the method will be kept in memory and be ready to be called whenever you need without creating instances. 但是使用静态方法时,只有方法将保留在内存中,并且随时可以在不创建实例的情况下调用。 Where as, if the method is non static, it will need to be instantiated(using up memory and processing power) and the garbage collected(freeing memory and using up cpu) therefore it is definitely better using a static member. 其中,如果方法是非静态的,则需要实例化(使用内存和处理能力)并收集垃圾(释放内存并使用cpu),因此使用静态成员肯定更好。 Thats what they are there for. 那就是他们在那里。

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

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