小编Wol*_*ade的帖子

使用数组字段而不是大量的对象

根据这篇文章,我想知道人们使用数组存储大量数据集(比如说> 10,000,000个对象)来存储数据字段而不是实例化数百万个对象并增加内存开销(例如,12-每个对象24个字节,具体取决于您阅读的文章).每个属性的数据因项目而异,因此我不能使用严格的Flyweight模式,但可以设想类似的东西.

我对这种表示的想法是,有一个'模板对象'......

class Thing
{
  double A;
  double B;
  int    C;
  string D;
}
Run Code Online (Sandbox Code Playgroud)

然后是一个容器对象,其中包含一个根据请求创建对象的方法...

class ContainerOfThings
{
  double[] ContainerA;
  double[] ContainerB;
  int[]    ContainerC;
  string[] ContainerD;

  ContainerOfThings(int total)
  {
    //create arrays
  }

  IThing GetThingAtPosition(int position)
  {
     IThing thing = new Thing(); //probably best done as a factory instead
     thing.A = ContainerA[position];
     thing.B = ContainerB[position];
     thing.C = ContainerC[position];
     thing.D = ContainerD[position];

     return thing;
  }
}
Run Code Online (Sandbox Code Playgroud)

所以这是一个简单的策略,但不是非常通用,例如,不能创建'Thing'的子集(作为List)而不重复数据并且无法实现阵列字段存储的目的.我一直无法找到好的例子,所以我会欣赏更好的方法的链接或代码片段来处理这个场景的人,或者是一个更好的想法.

c# memory storage field object

7
推荐指数
2
解决办法
1417
查看次数

C# 不安全的指针字段

这是要断了吗?它编译得很好,但根据读数,我不确定它是否保证 _ptRef 将始终指向构造函数中引用的结构。

我猜“中断”是指……GC 会移动指针 (_ptRef) 指向的结构吗?

public unsafe class CPointType0
{
    private PointType0* _ptRef = null;

    public CPointType0(ref PointType0 spt)
    {
        fixed (PointType0 * pt = &spt)
        {
            _ptRef = pt;
        }
    }

...a bunch of property accessors to fields of _ptRef (accessed as return _ptRef->Thing) }
Run Code Online (Sandbox Code Playgroud)

场景是

-PointType0 是一个结构体。

- 数据结构中内存中的数百万个 PointType0。这些曾经是引用类型,但是内存开销太大了。

-A List 只有当搜索操作找到相关的 PointType0 时才返回,并且这个 List 被传递并在很多上操作。

c# unsafe field

2
推荐指数
1
解决办法
733
查看次数

标签 统计

c# ×2

field ×2

memory ×1

object ×1

storage ×1

unsafe ×1