难道一个变量本身消耗的内存? [英] Does a variable itself consume memory?

查看:110
本文介绍了难道一个变量本身消耗的内存?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

当我们声明一个变量,不变量本身占用的内存?



 类IHaveNothing 
{
}

类IHaveNullFields
{
字符串@string = NULL;
StringBuilder的@StringBuilder = NULL;
}

类节目
{
静态无效的主要(字串[] args)
{
IHaveNothing什么=新IHaveNothing();
IHaveNullFields nullFields =新IHaveNullFields();
}
}



请问实例nullFields消耗比实例罢了回忆?



编辑:
如何空局部变量,而不是类的空字段,做自己消耗内存太


< DIV CLASS =h2_lin>解决方案

一个变量是的定义的作为存储位置。所以,问题是:一个存储位置的消耗内存的?



当你说这听起来很明显,答案是肯定的方式。还有什么将一个存储位置做多消耗内存等?



这不是这么简单。局部变量可以消耗完全没有记忆;一个局部变量的可能被抖动的enregistered。在这种情况下,将既不消耗,也不叠堆内存。



为什么这么在意?在CLR管理内存,使存储位置变量的方式是一个实现细节。除非你写不安全的代码,你不必担心它。


When we declare a variable, does the variable itself consume memory?

 class IHaveNothing
{
}

class IHaveNullFields
{
    string @string = null;
    StringBuilder @StringBuilder = null;
}

class Program
{      
    static void Main(string[] args)
    {
        IHaveNothing nothing = new IHaveNothing();
        IHaveNullFields nullFields = new IHaveNullFields();
    }
}

Does the instance nullFields consume more memories than the instance nothing ?

EDIT: How about null local variables as opposed to class' null fields, do they consume memory too?

解决方案

A variable is defined as a storage location. So the question is: does a storage location consume memory?

When you say it that way it sounds obvious that the answer is yes. What else would a storage location do other than consume memory?

It's not as simple as that. A local variable can consume no memory at all; a local variable might be enregistered by the jitter. In that case it would consume neither stack nor heap memory.

Why do you care? The way that the CLR manages memory to make storage locations for variables is an implementation detail. Unless you're writing unsafe code, you don't have to worry about it.

这篇关于难道一个变量本身消耗的内存?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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