我在Visual Studio 2010中有一个功能正常的项目。我将该项目转移到了VS2012,我得到了OutOfMemoryException
。我知道为什么我会得到例外,因为我在2010年得到了同样的例子。它发生的原因是因为应用程序运行一个遗传算法,产生几个后代,其中每个子项生成的都由一个大对象组成(int array [500,200, 7])。我减少了在每个交叉上生成了多少后代,并且能够摆脱异常,我还必须添加offspring.clear()
,即使它是一个局部变量。在VS2012中,垃圾收集似乎永远不会占用应该释放的offspring
数组。我甚至在GC.Collect()
中添加它应该发生的地方。我很困惑为什么使用相同的代码,程序只会继续消耗VS2012中的内存。
/// <summary>
/// Runs the genetic algorithm
/// </summary>
public void RunAlgorithm()
{
for (int i = 0; i < progeny; i++)
schedules.Add(new Schedule(true));
schedules.Sort();
best = schedules[0];
while (best.unscheduled.Count > 15)
{
List<Scheduling.Schedule> offspring = GetOffspring();
offspring.Sort();
if (offspring[0].unscheduled.Count < best.unscheduled.Count)
best = offspring[0];
schedules.Clear();
for (int s = 0; s < progeny; s++)
schedules.Add(offspring[s]);
offspring.Clear();
}
}
public List<Scheduling.Schedule> GetOffspring()
{
List<Scheduling.Schedule> offspring = new List<Schedule>();
int parentSize = (int)(schedules.Count * poolSizePcnt);
for (int p1 = 0; p1 < parentSize; p1++)
{
for (int p2 = p1 + 1; p2 < parentSize; p2++)
{
Schedule[] children = Breed(schedules[p1], schedules[p2]);
offspring.Add(children[0]);
offspring.Add(children[1]);
}
}
return offspring;
}
/// <summary>
/// Returns an array of 2 children from the breeding of two parents with a single crossover point and potential mutation on the alleles.
/// </summary>
/// <param name="p1">Parent One</param>
/// <param name="p2">Parent two</param>
/// <returns></returns>
public Schedule[] Breed(Schedule p1, Schedule p2)
{
Schedule c1 = new Schedule();
c1.schedule = new int[p1.schedule.GetUpperBound(0) + 1, p1.schedule.GetUpperBound(1) + 1, p1.schedule.GetUpperBound(2) + 1];
Schedule c2 = new Schedule();
c2.schedule = new int[p1.schedule.GetUpperBound(0) + 1, p1.schedule.GetUpperBound(1) + 1, p1.schedule.GetUpperBound(2) + 1];
//randomized crossover point from min to max
int crssPnt = (int)(Schedule.rand.Next(min, max) / (10.0) * Schedule.courseIDdict.Count);
for (int c = 0; c < crssPnt; c++)
{
int cID = Schedule.courseIDdict[c].id;
//TODO: if cID is contained within a list, prevent from certain moves, e.g. to D
for (int i = 1; i <= 6; i++)
{
int random = Schedule.rand.Next(0, mutationRate);
if (p1.schedule[0, cID, i] >= 1)
{
if (rand1 == random)//introduce mutation if the random number is hit
c1.schedule[0, cID, Schedule.rand.Next(1, 7)] = 1;
else
c1.schedule[0, cID, i] = 1;
}
if (p2.schedule[0, cID, i] >= 1)
{
if (rand2 == random)//introduce mutation if the random number is hit
c2.schedule[0, cID, Schedule.rand.Next(1, 7)] = 1;
else
c2.schedule[0, cID, i] = 1;
}
}
}
for (int c = crssPnt; c < Schedule.courseIDdict.Count; c++)
{
int cID = Schedule.courseIDdict[c].id;
for (int i = 1; i <= 6; i++)
{
int random = Schedule.rand.Next(0, mutationRate);
if (p1.schedule[0, cID, i] >= 1)
{
if (rand2 == random)//introduce mutation if the random number is hit
c2.schedule[0, cID, Schedule.rand.Next(1, 7)] = 1;
else
c2.schedule[0, cID, i] = 1;
}
if (p2.schedule[0, cID, i] >= 1)
{
if (rand1 == random)//introduce mutation if the random number is hit
c1.schedule[0, cID, Schedule.rand.Next(1, 7)] = 1;
else
c1.schedule[0, cID, i] = 1;
}
}
}
c1.AddStudentsToClasses();
c2.AddStudentsToClasses();
return new[] { c1, c2 };
}
答案 0 :(得分:0)
VS中有(任何CPU)配置,然后有一个构建目标下拉列表(在2012年)Project-&gt; Properties-&gt; Buid选项卡。 Build Target经常被设置为x86,当有大量ram可用时,你会看到Out of Memory异常被抛出。例如,您可以通过构建目标选项设置任何cpu配置并仍在为x86构建。当您达到2GB的RAM使用量时,这将导致内存不足异常。