内存如何分配给静态方法 [英] How memory is allocated to static methods

查看:89
本文介绍了内存如何分配给静态方法的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在开发一个C#windows应用程序。为此,我创建了许多类,在这些类中,大多数方法都是静态的。我想知道何时为这些静态方法分配内存以及何时将由GC清除。与基于实例的方法调用的方法相比,将方法创建为静态是一种好习惯。当我启动我的应用程序,耗费大量内存时,我认为这是因为所有方法都是静态的。

I am working on a C# windows application. For this I have created many classes, in those classes most of the methods are static. I want to know when memory will be allocated for these static methods and when that will be cleared by GC. Is it good practice to create methods as static compared to method invoked on an instance based. When I launch my application, its consuming lot of memory, I assume that it’s because all the methods are static.

推荐答案

static 方法不会消耗比实例更多的内存(你必须看看其他...)

你应该使用静态方法来实现类功能,这些功能不依赖于该类的特定实例。
static methods not consume more memory than instance ones (you have to look elsewere...)
You should use static methods for 'class functionalities' that is for functionalities that don't depend on a particular instance of the class.


这篇关于内存如何分配给静态方法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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