簡體   English   中英

VS2012 OutOfMemoryException與VS2010沒有異常的工作

[英]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.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM