在多个内部循环中使用大量内存

发布于 2024-10-17 04:16:57 字数 2644 浏览 4 评论 0原文

我编写了一个静态方法,它使用 4 个内部循环设置,如下所示。我一直在对其运行时使用的堆进行大量测试。它似乎生成了成千上万的“int[]”对象,我认为这些对象来自我放置的循环。

我似乎无法找到解决这个内存问题的方法,我需要循环,但我不需要为每个循环创建大量 int[] 对象,这只是浪费!这些循环本身是否导致了这些错误?有什么办法可以减少内存使用量,我正在考虑增强循环,但这可能是同样的问题......

谢谢!

    public static double[] calculateHand(final int card1, final int card2, final int card3,
        final int card4, final int card5)
{
    int ahead = 0, tied = 1, behind = 2;
    int[][] HP = new int[3][3];
    int[] HPTotal = new int[3];

    int ourrank = HandEval.hand5Eval(HandEval.encode(card1, card2, card3, card4, card5));

    int[] remainingCards = filterCardsFromDeck(card1, card2, card3, card4, card5);

    int kM = 1

    for (int i = 0; i < remainingCards.length; i++)
    {

        for (int k = kM; k < remainingCards.length; k++)
        {
            int index = -1;
            int oCard1 = remainingCards[i];
            int oCard2 = remainingCards[k];

            int opprank = HandEval.hand5Eval(HandEval
                    .encode(oCard1, oCard2, card3, card4, card5));

            if (ourrank > opprank)
            {
                index = ahead;
            }
            else if (ourrank == opprank)
            {
                index = tied;
            }

            else
            {
                index = behind;
            }

            HPTotal[index]++;
            int[] newArray = filter2Cards(remainingCards, oCard1, oCard2);

            int riverMinimumIndex = 1;
            for (int turnIndex = 0; turnIndex < newArray.length; turnIndex++)
            {

                for (int riverIndex = riverMinimumIndex; riverIndex < newArray.length; riverIndex++)
                {

                    int turnCard = newArray[turnIndex];
                    int riverCard = newArray[riverIndex];
                    int ourbest = HandEval.hand7Eval(HandEval.encode7(card1, card2, card3, card4,
                            card5, turnCard, riverCard));

                    int oppbest = HandEval.hand7Eval(HandEval.encode7(oCard1, oCard2, card3, card4,
                            card5, turnCard, riverCard));

                    if (ourbest > oppbest)
                    {
                        HP[index][ahead]++;
                    }

                    else if (ourbest == oppbest)
                    {
                        HP[index][tied]++;
                    }

                    else
                    {
                        HP[index][behind]++;
                    }
                }
                riverMinimumIndex++;
            }
        }
        kM++;
    }

    .....

    return result;
}

I wrote a static method that uses 4 inner loops set-up like the ones below. I have been doing lots and lots of testing on the heap used when it runs. It seems to generate thousands and thousands of "int[]" objects, I think that these are coming from the loops I have put in place.

I can't seem to find a way around this memory problem, I need the loops but I don't need lots of int[] objects created for every loop its just a waste! Are these the loops themselves that are causing these errors? And is there anything I can do to reduce the memory usage, I was thinking about enhanced-for-loop, but that might be the same problem...

Thank you!

    public static double[] calculateHand(final int card1, final int card2, final int card3,
        final int card4, final int card5)
{
    int ahead = 0, tied = 1, behind = 2;
    int[][] HP = new int[3][3];
    int[] HPTotal = new int[3];

    int ourrank = HandEval.hand5Eval(HandEval.encode(card1, card2, card3, card4, card5));

    int[] remainingCards = filterCardsFromDeck(card1, card2, card3, card4, card5);

    int kM = 1

    for (int i = 0; i < remainingCards.length; i++)
    {

        for (int k = kM; k < remainingCards.length; k++)
        {
            int index = -1;
            int oCard1 = remainingCards[i];
            int oCard2 = remainingCards[k];

            int opprank = HandEval.hand5Eval(HandEval
                    .encode(oCard1, oCard2, card3, card4, card5));

            if (ourrank > opprank)
            {
                index = ahead;
            }
            else if (ourrank == opprank)
            {
                index = tied;
            }

            else
            {
                index = behind;
            }

            HPTotal[index]++;
            int[] newArray = filter2Cards(remainingCards, oCard1, oCard2);

            int riverMinimumIndex = 1;
            for (int turnIndex = 0; turnIndex < newArray.length; turnIndex++)
            {

                for (int riverIndex = riverMinimumIndex; riverIndex < newArray.length; riverIndex++)
                {

                    int turnCard = newArray[turnIndex];
                    int riverCard = newArray[riverIndex];
                    int ourbest = HandEval.hand7Eval(HandEval.encode7(card1, card2, card3, card4,
                            card5, turnCard, riverCard));

                    int oppbest = HandEval.hand7Eval(HandEval.encode7(oCard1, oCard2, card3, card4,
                            card5, turnCard, riverCard));

                    if (ourbest > oppbest)
                    {
                        HP[index][ahead]++;
                    }

                    else if (ourbest == oppbest)
                    {
                        HP[index][tied]++;
                    }

                    else
                    {
                        HP[index][behind]++;
                    }
                }
                riverMinimumIndex++;
            }
        }
        kM++;
    }

    .....

    return result;
}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

我不是你的备胎 2024-10-24 04:16:57

在该方法中有四个地方创建(或可能创建)数组:(

int[][] HP = new int[3][3];
int[] HPTotal = new int[3];
...
int[] remainingCards = filterCardsFromDeck(card1, card2, card3, card4, card5);
...
int[] newArray = filter2Cards(remainingCards, oCard1, oCard2);

您调用的其他一些方法也可能创建...并丢弃...临时数组。应该很容易发现它们。 )

前三个在每次调用您的方法时都会发生一次,并且可能不是一个重大问题。最后一个发生在第二级循环中,并且(如果我的阅读正确)将被执行 O(N**2) 次,其中 N 是卡片的数量。

该怎么办? (显而易见的答案是不要管它,除非你有明确的证据表明它会影响应用程序性能。但我认为你已经过去了。)

我能想到的最好的方法是重构你的代码,以便 filter2Cards 将现有数组作为参数,并用过滤后的卡片填充它。它需要返回一个 int 给出添加到数组中的值的数量,并且以下代码需要使用该值而不是数组的长度。然后将数组的创建移到最外层循环之前。

显然,这会使您的代码变得更加复杂。这就是你要付出的惩罚...

There are four places where arrays are created (or probably created) in that method:

int[][] HP = new int[3][3];
int[] HPTotal = new int[3];
...
int[] remainingCards = filterCardsFromDeck(card1, card2, card3, card4, card5);
...
int[] newArray = filter2Cards(remainingCards, oCard1, oCard2);

(It is also possible that some of the other methods you call create ... and discard ... temporary arrays. It should be easy to spot them.)

The first three happen once per call of your method and are probably not a significant problem. The last one occurs in a 2nd level loop, and (if my reading is correct) will be executed O(N**2) times where N is number of cards.

What to do about it? (The obvious answer is to just leave it alone, unless you have clear evidence that it effects application performance. But I assume you are past that.)

The best I can think of is to refactor your code so that filter2Cards takes an existing array as an argument, and fills it with the filtered cards. It would need to return an int giving the number of values added to the array, and the following code would need to use this value instead of the array's length. Then move the creation of the array to before the outermost loop.

Obviously, this makes your code more complicated. That's the penalty you pay ...

苦妄 2024-10-24 04:16:57

我的猜测是,像您所说的那样的堆膨胀会作为虚拟机开发的一部分而被捕获。您遇到虚拟机问题并非不可能,但是如果您没有发布其余代码,我的猜测是问题不仅仅是循环,更可能是您在你的其他代码。如果您发布该内容,也许我们可以提供更多帮助。

My guess is a heap inflation like your talking about would have been caught as part of the development of the VM. Its not impossible that you are seeing a VM issue, but without you posting the rest of your code, my guess is that the problem isn't just the loops and is more likely something you've introduced in your other code. If you post that, perhaps we can be of more help.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文