[英]VS2012 OutOfMemoryException vs. VS2010 working w/o Exception
我在Visual Studio 2010中有一个正常运行的项目。 我将该项目转移到VS2012,并且出现OutOfMemoryException
。 我知道为什么会遇到例外,因为我在2010年遇到了同样的情况。发生这种情况的原因是,该应用程序运行的是一种遗传算法,该算法会产生多个后代,每个后代都由一个大对象组成(int数组[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 };
}
VS中有一个(任何CPU)配置,然后有一个构建目标下拉列表(2012年)中的Project-> Properties-> Buid选项卡。 通常,“构建目标”的时间将设置为x86,并且当仍有大量可用内存时,您会看到抛出“内存不足”异常。 例如,您可以设置任何cpu配置,并且仍然可以通过build target选项为x86构建。 当您使用2GB的RAM时,这将导致发生内存不足异常。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.