如果增加堆的大小不起作用,如何处理 OOME 异常?

How to treat OOME exception, if increasing the size of heap doesnt work?

我正在尝试在网格上随机单击的点和点列表中的给定点之间绘制垂直线和水平线。我的新手风格编码似乎对应用程序造成了影响,在我单击网格上的随机点后该应用程序崩溃了。然后它抛出以下 OOME。到目前为止,我已经尝试增加堆大小。那根本没有帮助.. 有任何想法吗?? 这是代码片段,导致 it.i 到目前为止只编写了列表中某个点以东的点击事件的代码

@Override
public void mouseClicked(MouseEvent e) {
    boolean crossingLines = false;
    Line l;
    Point p = new Point(e.getX(), e.getY());
    for (int i = 0; i < pointList.size(); i++) {
        Rectangle rec = new Rectangle(pointList.get(i).x
                * GAPBETWEENPOINTS
                + GAP
                - TOLERANCE, pointList.y
                * GAPBETWEENPOINTS
            + GAP
                - TOLERANCE,TOLERANCE * 2,
                CLICKTOLERANCE * 2);
        if (rec.contains(p)) {
            if (clickedEast(p, pointList.get(i)) == true) {
                for (int j = i + 1; j < pointList.size(); j++) {
                    if (pointList.get(j).y
                            * GAPBETWEENPOINTS
                            + GAP == pointList.get(i).y
                                    * GAPBETWEENPOINTS
                                    + GAP
                            && pointList.get(j).x
                            * GAPBETWEENPOINTS
                            + GAP > pointList.get(i).x
                            * GAPBETWEENPOINTS
                            + GAP) {
                        l = new Line(pointList.get(i),
                                pointList.get(j));
                        if (!lineList.contains(b)) {

                            for (int k = 0; k < lineList.size(); k++) {
                                if (lineList.get(k).getHorizontal() == false) {
                                    if (pointList.get(i).x
                                            * GAPBETWEENPOINTS
                                            + GAP < lineList
                                            .get(k).getStartPoint().x
                                            && lineList.get(k)
                                                    .getStartPoint().x < pointList.get(j).x
                                                    * GAPBETWEENPOINTS
                                                    + GAP
                                            && lineList.get(k)
                                                    .getStartPoint().y < pointList.get(i)).y
                                                    * GAPBETWEENPOINTS
                                                    + GAP
                                            && pointList.get(k).y
                                            * GAPBETWEENPOINTS
                                            + GAP < lineList
                                                    .get(k).getEndPoint().y) {
                                        crossinglines = true;
                                    }
                                }

                                if (crossingLines == false) {
                                    lineList.add(b);
                                }
                            }
                        }
                        validate();
                        repaint();
                    }
                }
            }
        }
    }
}

这里是个例外:

Exception in thread "AWT-EventQueue-0" java.lang.OutOfMemoryError: Java 
heap space
at java.util.Arrays.copyOf(Arrays.java:2245)
at java.util.Arrays.copyOf(Arrays.java:2219)
at java.util.ArrayList.grow(ArrayList.java:242)
at java.util.ArrayList.ensureExplicitCapacity(ArrayList.java:216)
at java.util.ArrayList.ensureCapacityInternal(ArrayList.java:208)
at java.util.ArrayList.add(ArrayList.java:440)
at GUIs.GameField.mouseClicked(GameField.java:326)
at java.awt.Component.processMouseEvent(Component.java:6519)
at javax.swing.JComponent.processMouseEvent(JComponent.java:3321)
at java.awt.Component.processEvent(Component.java:6281)
at java.awt.Container.processEvent(Container.java:2229)
at java.awt.Component.dispatchEventImpl(Component.java:4872)
at java.awt.Container.dispatchEventImpl(Container.java:2287)
at java.awt.Component.dispatchEvent(Component.java:4698)
at java.awt.LightweightDispatcher.retargetMouseEvent(Container.java:4832)
at java.awt.LightweightDispatcher.processMouseEvent(Container.java:4501)
at java.awt.LightweightDispatcher.dispatchEvent(Container.java:4422)
at java.awt.Container.dispatchEventImpl(Container.java:2273)
at java.awt.Window.dispatchEventImpl(Window.java:2719)
at java.awt.Component.dispatchEvent(Component.java:4698)
at java.awt.EventQueue.dispatchEventImpl(EventQueue.java:747)
at java.awt.EventQueue.access0(EventQueue.java:103)
at java.awt.EventQueue.run(EventQueue.java:706)
at java.awt.EventQueue.run(EventQueue.java:704)
at java.security.AccessController.doPrivileged(Native Method)
at
java.security.ProtectionDomain.doIntersectionPrivilege(ProtectionDomain.
java:76)
at java.security.ProtectionDomain.doIntersectionPrivilege(ProtectionDomain.
java:87)
at java.awt.EventQueue.run(EventQueue.java:720)
at java.awt.EventQueue.run(EventQueue.java:718)
at java.security.AccessController.doPrivileged(Native Method)
at java.security.ProtectionDomain.doIntersectionPrivilege(ProtectionDomain.
java:76)
at java.awt.EventQueue.dispatchEvent(EventQueue.java:717)

问题似乎出在 GameField.mouseClicked() 内的最内层循环中。当 crossingLines 的计算结果为 false 时,您可能会向 lineList 添加很多项,因为 lineList.add()lineList 本身的循环中。因此,似乎 crossingLines 目前从未评估为 true 并且列表不断扩展,直到您 运行 内存不足。

要解决此问题,您应该以某种方式确保 lineList.add() 不会被连续调用。此外,我建议您重新考虑 mouseClicked() 的结构。目前,您在方法中有三个(!)嵌套的 for 循环,并且还在中间循环中调用 repaint(),这可能会导致一些不必要的闪烁,具体取决于 pointList 的大小。