读书人

获取 Ilist聚合 的内存大小

发布时间: 2012-12-16 12:02:32 作者: rapoo

获取 Ilist集合 的内存大小
请看代码:



参考:http://zhidao.baidu.com/question/73668016.html
[其他解释]
实现不了,c#不像c,可以自己控制类的内存布局,也没有提供计算类占用内存大小的API。
[其他解释]
不好意思,少打了一行

public void test()
{
IList<student> ils = new List<student>();
for (int x = 0; x < 100000; x++)
{
student _s = new student();
_s.Name = x.ToString();
_s.Age = x;
ils.Add(_s);
}
}
[其他解释]


public class student
{
private string _name;
public string Name
{
set { _name = value; }
get { return _name; }
}

private int _age;
public int Age
{
set { _age = value; }
get { return _age; }
}

private int _Size;
public int Size
{
get
{
_Size = 4;

if (!String.IsNullOrEmpty(Name))
{
_Size += System.Text.Encoding.UTF8.GetBytes(Name).Length;
}

return _Size;
}
}


}

public void test()
{
int size = 0;
IList<student> ils = new List<student>();
for (int x = 0; x < 100000; x++)
{
student _s = new student();
_s.Name = x.ToString();
_s.Age = x;

size += _s.Size;
}

MessageBox.Show(size.ToString());
}




[其他解释]
        public int Size
{
get
{
int size = 4;

if (!String.IsNullOrEmpty(Name))
{
size += System.Text.Encoding.UTF8.GetBytes(Name).Length;
}

return size;
}
}

多了一个private int _Size;



[其他解释]
    [StructLayout(LayoutKind.Sequential, Pack = 1)]
public struct student
{
private string _name;
public string Name
{
set { _name = value; }
get { return _name; }
}

private int _age;
public int Age
{
set { _age = value; }


get { return _age; }
}

public int Size
{
get
{
int size = 4;

if (!String.IsNullOrEmpty(Name))
{
size += System.Text.Encoding.UTF8.GetBytes(Name).Length;
}

return size;
}
}
}




[其他解释]
呵呵...从来没关注过这个~~~原来还这么高深..
[其他解释]
谢谢大家的指点!
第二个问题大家比较关注,其实我更关注的是第一个问题。
1.IList<student> ils 在该实例的每一行中,是否都隐藏着 name 和 age,就像字典IList<IDictionary<string,object>> 一样。

如果真的是那样,Ilist<student> 是否比 Ilist<object[]> 更加占用空间?毕竟后者只是object。
[其他解释]
期待……
自己顶一下
[其他解释]
难,除非你先封送到栈上

读书人网 >C#

热点推荐