C#数组对象的,非常大的,在寻找更好的方法 [英] C# array of objects, very large, looking for a better way

查看:128
本文介绍了C#数组对象的,非常大的,在寻找更好的方法的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

好了,在我的项目之一,即时通讯试图改造它保存某些变量的方式,我有对象的简单数组。这些对象引用的类是:

Ok, so In one of my projects, im trying to remake the way it stores certain variables, i have a simple array of objects. The class that these objects refer to is:

class Blocks
{
    public byte type = Block.Empty;
    byte lastblock = Block.Zero;
}

我计划添加更多了,在目前的类类型的对象当前值是什么,以及lastblock是什么对象曾经是。

I plan to add more to it, in the current class type is what the objects current value is, and lastblock is what the object used to be.

我创建这样的数组:

blocks = new Blocks[width * depth * length];

for (int i = 0; i < ((width * length) * depth); i++)
{
    blocks[i] = new Blocks();
}

这是我遇到的问题是,当我创建一个数组,是非常大的(512512512或​​134217728对于那些你们谁不喜欢数学),则数组被巨大的,向上的3.5演出。

The problem that I'm having, is that when i create an array that's very large (512,512,512 or 134217728 for those of you whom don't like math), the array gets huge, upwards of 3.5 gigs.

这是这个数组的创建是有点简单,但更难扩大旧的方式,简单的创建一个字节数组重新presenting当前块,而且似乎它的实际时只使用RAM的2兆加载(我不明白,因为134217728字节应该在134兆的,对吧?)。它只是令我感到困惑的对象引用可能会产生更多的RAM占用。

The old way that this array was created was a little simpler, but much harder to expand, it simple created an array of bytes representing the current block, and seems to only use 2 megs of ram when its actually loaded (which i dont get since 134217728 bytes should be around 134 megs... right?). It just baffles me that object references could generate that much more ram usage.

我做得不对,或者我应该回到老路上有人做过?我想的对象引用,只是因为它意味着所有的变量都在1阵列而不是在4个不同的阵列,它的似乎的,仿佛这将是该系统更好。

Am i doing something wrong, or should i just go back to the old way it was done? I would like the object references simply because it means that all my variables are in 1 array rather then in 4 separate arrays, which seems as though it would be better for the system.

编辑:

通过这样几个不同的方式工作后,我发现,改变

After working through a few different ways of doing this, i found that changing

class Blocks

struct Blocks

制造一个不同的世界,谢谢社会各界的欢迎提示以备将来使用,遗憾的是我不想只是增加两个字节的结构,并呼吁它做的,这是我在哪里停下来测试我的设计和伤口与原来的问题。在添加任何其他的struct(别的这是我的名单上,至少,这意味着无论是选手对象引用或一个球员的名字字符串)。这会导致内存不足的异常,这意味着我不会要能够毕竟使用这个系统。

Made a world of difference, Thank you community for that welcome tip for future use, unfortunately i didn't want to just add two bytes in a struct and call it done, that was where i stopped to test my design and wound up with the original problem. After adding anything else to the struct (anything else that's on my list at least, meaning either a player object reference or a player name string.) It causes an out-of-memory exception that means I'm not going to be able to use this system after all.

但怎么做,将是在未来相当有用的知识。对于这一点,再次感谢你。

But the knowledge of how to do it will be quite helpful in the future. For that, thank you again.

推荐答案

下面是我的尝试,而不是类类型结构类型

Here's what I tried with a structure type instead of a class type:

public struct Block
{
    public byte _current;
    public byte _last;
}

public static void RunSnippet()
{
    Block[] blocks = new Block[512 * 512 * 512];

    for (int i = 0; i < ((512 * 512) * 512); i++)
    {
        blocks[i] = new Block();
    }
}

该段跑了几乎立即和的周围267 MB的RAM。

The snippet ran almost instantly and ate around 267 Mb of RAM.

所以给结构一试,如果这是可能的。

So give struct a try if that's possible.

这篇关于C#数组对象的,非常大的,在寻找更好的方法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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