【发布时间】:2017-10-17 04:53:25
【问题描述】:
您好,当我尝试计算超过 24 个元素的组合时,我正在使用此函数来计算对象“Ricerca”列表的所有可能组合,我得到 System.OutOfMemory 异常。有什么办法可以解决这个问题吗?
这是我使用的功能:
private static List<List<Ricerca>> GetAllCombos(List<Ricerca> list)
{
int comboCount = (int)Math.Pow(2, list.Count) - 1;
List<List<Ricerca>> result = new List<List<Ricerca>>();
for (int i = 1; i < comboCount + 1; i++)
{
// make each combo here
result.Add(new List<Ricerca>());
for (int j = 0; j < list.Count; j++)
{
if ((i >> j) % 2 != 0)
result.Last().Add(list[j]);
}
}
return result;
}
感谢您的帮助
【问题讨论】:
-
什么包含类“Ricerca”?此外,您的代码将重复组合,您确定这是您想要的吗?如果您真的想这样做,您可以大大简化代码,只需克隆基本列表并从克隆中删除当前索引。
-
2^24 = 16777216- 这很大,你知道吗?如果您认为这 1600 万个元素中的每一个都可以包含多达 24 个元素,那么堆中大约有 4 亿个引用。我不相信你的OutOfMemoryException一点也不奇怪。 -
您将 (x * 2**x)) 个项目放入列表中。 List.Count 有多大? x = 24 即 4026353184。
-
@FedericoDipuma 我知道它很大:-(
-
@jdweng 列表的维度不是固定的,所以它可以是任意的
标签: c# visual-studio exception memory out-of-memory