相关疑难解决方法(0)

数组可以容纳的最大大小是多少?

在C#2008中,数组可以容纳的最大大小是多少?

c# arrays

36
推荐指数
3
解决办法
9万
查看次数

无法创建庞大的数组

像许多其他程序员一样,我进入了素数,而且他们中的很多人,我喜欢的是挑战,所以我不是在寻找评论,比如阿特金比你这么做得快,但只是一个解决方案 - 或者至少是一个暗示 - 对我的问题.

我需要创建数组(如size> int.MaxValue).所以我去了很多网页,发现gcAllowVeryLargeObjects元素一个.我以为我得救了,给我加上以下魔法App.config:

<configuration>
  <runtime>
    <gcAllowVeryLargeObjects enabled="true" />
  </runtime>
</configuration>
Run Code Online (Sandbox Code Playgroud)

但它没有奏效.这是我使用的代码:

void go(object sender, EventArgs eventArgs)
{
    t.Stop();
    ulong maxprime = 10;
    Stopwatch stopwatch = new Stopwatch();
    string s = String.Empty;
    while (maxprime < ulong.MaxValue)
    {
        stopwatch.Restart();
        richTextBox2.Text += Environment.NewLine + ("Max \t= " + maxprime.ToString("N0"));
        try
        {
            richTextBox2.Text += Environment.NewLine + ("Count \t= " + GetAllPrimesLessThan(maxprime).Count);
            richTextBox2.Text += Environment.NewLine + ("Time \t= …
Run Code Online (Sandbox Code Playgroud)

c# arrays

11
推荐指数
2
解决办法
2万
查看次数

字典计数大于89478457时出错

我正在接受这个命令

Dictionary<UInt64, int> myIntDict = new Dictionary<UInt64, int>(89478458);
Run Code Online (Sandbox Code Playgroud)

这个错误:

System.OutOfMemoryException was unhandled  HResult=-2147024882
Message=Array dimensions exceeded supported range.
Source=mscorlib
StackTrace:
   at System.Collections.Generic.Dictionary`2.Initialize(Int32 capacity)
   at System.Collections.Generic.Dictionary`2..ctor(Int32 capacity, IEqualityComparer`1 comparer)
Run Code Online (Sandbox Code Playgroud)

在89478457上没有错误.以下是Dictionary.cs中Initialize的来源:

    private void Initialize(int capacity)
    {
        int size = HashHelpers.GetPrime(capacity);
        ...
        entries = new Entry[size];
        ...
    }
Run Code Online (Sandbox Code Playgroud)

当我重现它时,错误发生在数组创建上.在这种情况下,条目是一个结构,大小为24.当我们得到max int32(0x80000000-1)并除以24 = 89478485时,这个数字在素数89478457和89478503之间.

这是否意味着,结构数组不能像maxInt32/sizeOfThisStruct那样大?

编辑:

是.我实际上超过2 GB.当字典创建struct Entry的内部数组时,会发生这种情况,其中存储了(键,值)对.在我的例子中,sizeof(Entry)是24个字节,并且值类型是内联分配的.

解决方案是使用gcAllowVeryLargeObjects 标志(谢谢Evk).实际上在.net核心中,标志是环境变量 COMPlus_gcAllowVeryLargeObjects(谢谢svick).

是的,狗仔队是对的.我要思考,怎么不浪费记忆.谢谢你们.

c# .net-core .net-core-rc2

0
推荐指数
1
解决办法
2362
查看次数

标签 统计

c# ×3

arrays ×2

.net-core ×1

.net-core-rc2 ×1