问题描述
得到就怀疑到C#静态/ const成员/局部变量。只是想知道发生了什么,以分配给未使用的静态/ const成员/地方在C#中的变量的记忆,是如何在下面的示例场景回收内存?
Got doubts with respect to C# static/const member/local variables. Just want to know what happens to the memory allocated to unused static/const member/local variables in C#, how is the memory reclaimed in the following example scenario?
问题是约相对于静态和常量记忆行为[考虑每个应用程序域的内存块 - 静态存储]这个问题是不是垃圾收集。它是关于内存上发生了什么(也)有记忆
Question is about memory behavior with respect to static and const variables [considering per-App-Domain piece of memory - static storage]? This question is NOT about Garbage Collection. It is about memory and what happens to unused static and const variables that (also) have memory?
意粉代码片段未使用的静态和常量:
Spaghetti Code Snippet:
/// <summary>
/// Skew your data with every-second-and-annoyed updates
/// </summary>
class Skewgle
{
static Skewgle cloneApele = new Skewgle();
const Skewgle patentMoto = default(dynamic);
static int? dontBeEvilMotto = 1998;
const int ditchMotoToBeEvil = 2014;
static void Main()
{
const Skewgle findYourMailsAlreadyReadBetweenSpamTabs = patentMoto;
if (findYourMailsAlreadyReadBetweenSpamTabs == null)
{
System.Console.WriteLine("findYourMailsAlreadyReadBetweenSpamTabs and patentMoto are null");
}
if (cloneApele != null)
{
System.Console.WriteLine("cloneApele is not null");
}
System.Console.WriteLine("What about dontBeEvilMotto? ditchMotoToBeEvil?");
}
}
感谢
Thanks
推荐答案
的所有的一个类的静态字段将在类的任何实例/静态方法第一次使用(在C#规范静态字段初始化)
All static fields of a class will be initialized before first usage of any instance/static method of the class (static field initialization in C# spec ).
静态字段每应用程序域和值将不会被标记为GC到的AppDomain卸载。
Static fields are per Application Domain and value will not be marked for GC till unloading of AppDomain.
这篇关于C#static和const变量内存的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!