0

EDIT: I tried changing -Xms and -Xmx on the eclipse.ini already but it is still the same. I have a Java GUI program that runs a heavy computation in the background upon an event trigger. If the user input in the program is fairly not that big (less than 200, I think), everything runs smoothly - no lags, and the heatmaps (which are my output) are given and displayed by the program. But if it is indeed a large data, it starts to lag and sometimes, halfway through the process, the program freezes (e.g. somewhere 300~ over 724 data). Take note that if for example, I say 724 data, it means that I am creating 724 heatmaps.

My main Thread from the GUI triggered upon by a button click is as follows...

class ComponentVisualization implements Runnable{
    JScrollPane scrollPane;
    JPanel panelScroll;
    JPanel componentComboBoxPanel;
    @Override
    public void run(){
        scrollPane = new JScrollPane();
        panelScroll = new JPanel();
        panelScroll.setBorder(new EmptyBorder(10, 10, 10, 10));
        panelScroll.setLayout(new BoxLayout(panelScroll, BoxLayout.Y_AXIS));
        componentComboBoxPanel = new JPanel();
        componentComboBoxPanel.setMaximumSize(new Dimension(600, 25));
        componentComboBoxPanel.setLayout(new BoxLayout(componentComboBoxPanel, BoxLayout.X_AXIS));
        JLabel componentLabel = new JLabel("Component Plane Option: ");
        componentComboBoxPanel.add(componentLabel);
        JComboBox<String> componentComboBox = new JComboBox<String>();          
        componentComboBoxPanel.add(componentComboBox);
        for(int i = 0; i < optimalMaps.length; i++){
            componentComboBox.addItem(optimalMaps[i].getTypeOfData());
            optimalMaps[i].initComponentPlanes();   
        }

        JPanel outerPan = new JPanel();
        outerPan.setLayout(null);
        java.awt.event.ItemListener itemListener = new java.awt.event.ItemListener() {
          public void itemStateChanged(java.awt.event.ItemEvent itemEvent) {
              if (itemEvent.getStateChange() == java.awt.event.ItemEvent.SELECTED) {
                  int indexSelected = componentComboBox.getSelectedIndex();
                  System.out.println("ind:" + indexSelected);
                  outerPan.removeAll();
                  for(int i = 0; i < optimalMaps[indexSelected].getComponentPlanes().length; i++){
                    JPanel pan = new JPanel () ; 
                    pan.setLayout(null);
                    pan.add(optimalMaps[indexSelected].getComponentPlanes()[i]);
                    pan.setBorder(javax.swing.BorderFactory.createTitledBorder(
                            optimalMaps[indexSelected].getInputData().getVariableLabels()[i]));
                    pan.setBounds(240*(i%4),(280*(i/4)),240,280);
                    pan.setToolTipText(optimalMaps[indexSelected].getInputData().getVariableLabels()[i]);
                    outerPan.add(pan);
                  }
                  outerPan.setPreferredSize(new Dimension(240*4, 280*(optimalMaps[indexSelected].getSOMTrainer()
                          .getLattice().getNumberOfNodeElements()/4 + 1)));

                  outerPan.revalidate();
                  outerPan.repaint();
               }
          }
        };
        componentComboBox.addItemListener(itemListener);
        panelScroll.add(componentComboBoxPanel);
        panelScroll.add(outerPan);
        componentComboBox.setSelectedIndex(-1);
        componentComboBox.setSelectedIndex(0);  
        //return null;
        done();
    }
    protected void done(){
        scrollPane.setViewportView(panelScroll);
        for(int i = 0; i < finalVisualizationTabPane.getTabCount(); i++){
            if(finalVisualizationTabPane.getTitleAt(i).equals("Component Planes"))
                finalVisualizationTabPane.remove(i);
        }
        finalVisualizationTabPane.add("Component Planes", scrollPane);
        finalVisualizationTabPane.setSelectedIndex(finalVisualizationTabPane.getTabCount() - 1);
        finalVisualizationPanel.revalidate();
        finalVisualizationPanel.repaint();
        componentDone = true;
        visLogger.throwSuccessMessage("Component Planes Visualization completed! \n");
    }   
}

I have tried using SwingWorker instead of traditional Thread but it is almost the same. It LAGS and FREEZES on a large N halfway through.

This is the code triggered/called by "optimalMaps[i].initComponentPlanes();" from the above code

public void initComponentPlanes(){
    componentPlanes = new ComponentPlane[somtrainer.getLattice().getNumberOfNodeElements()];
    int size = somtrainer.getLattice().getNumberOfNodeElements();
    for(int i = 0; i < size; i++){
        System.out.println(i + ": " + inputData.getVariableLabels()[i] + " size : " + size);
        componentPlanes[i] = new ComponentPlane(somtrainer.getLattice(), i);
        componentPlanes[i].setBounds((240 - 225)/2, (280-240)/2, 225, 240);
        componentPlanes[i].setOrigMaxMin(maxMin[i][0], maxMin[i][1]);
        //if(i % 100 == 0) System.gc();
    }
}

I would also show you the ComponentPlane.class. I have also tried making ComponentPlane.class a runnable class and try to use componentPlanes[i].run() in the code above but yields almost the same result. Here is the code for the Component Class

public class ComponentPlane extends JPanel{
private Lattice lattice;
private int componentNumber;
private double minValue;
private double maxValue;
private double origMinValue;
private double origMaxValue;

public ComponentPlane(Lattice lattice, int componentNumber){
    this.lattice = new Lattice();
    this.componentNumber = componentNumber;
    initLattice(lattice);
    initComponentPlane();
}

private void initLattice(Lattice lattice){
    this.lattice.setLatticeHeight(lattice.getLatticeHeight());
    this.lattice.setLatticeWidth(lattice.getLatticeWidth());
    this.lattice.setNumberOfNodeElements(lattice.getNumberOfNodeElements());
    this.lattice.initializeValues();
    for(int i = 0; i < this.lattice.getTotalNumberOfNodes(); i++){
        this.lattice.getLatticeNode()[i] = new Node(lattice.getLatticeNode()[i]);
    }

    this.lattice.setNodeHeight(lattice.getNodeHeight());
    this.lattice.setNodeWidth(lattice.getNodeWidth());
    this.lattice.setTotalNumberOfNodes(lattice.getTotalNumberOfNodes());

}

public Lattice getLattice(){
    return lattice;
}

private void initComponentPlane(){
    maxValue = lattice.getLatticeNode()[0].getDoubleElementAt(componentNumber);
    minValue = lattice.getLatticeNode()[0].getDoubleElementAt(componentNumber);

    for(int i = 1; i < lattice.getTotalNumberOfNodes(); i++){
        if(lattice.getLatticeNode()[i].getDoubleElementAt(componentNumber) < 
                minValue){
            minValue = lattice.getLatticeNode()[i].getDoubleElementAt(componentNumber); 
        }
        if(lattice.getLatticeNode()[i].getDoubleElementAt(componentNumber) > maxValue){
            maxValue = lattice.getLatticeNode()[i].getDoubleElementAt(componentNumber);
        }
    }
    for(int i = 0; i < lattice.getTotalNumberOfNodes(); i++){
        Node currNode = lattice.getLatticeNode()[i];
        int colorValue = (int)Math.round((currNode.getDoubleElementAt(componentNumber)
                - minValue) / (maxValue - minValue) * 1020);
        Color nodeColor = new Color(0);
        int caseValue = colorValue/256;

        if(caseValue == 0)
            nodeColor = new Color(0, (colorValue % 256), 255);
        else if(caseValue == 1)
            nodeColor = new Color(0, 255, 255 - (colorValue % 256));
        else if (caseValue == 2)
            nodeColor = new Color((colorValue % 256), 255, 0);
        else nodeColor = new Color(255, 255 - (colorValue % 256), 0);

        lattice.getLatticeNode()[i].setNodeColor(nodeColor);
    }
}

public double getMaxValue(){
    return maxValue;
}

public double getMinValue(){
    return minValue;
}

public void setOrigMaxMin(double maxValue, double minValue){
    this.origMaxValue = this.maxValue * (maxValue - minValue) + minValue;
    this.origMinValue = this.minValue * (maxValue - minValue) + minValue;
}

public void paintComponent(Graphics g){
    super.paintComponent(g);
    Graphics2D g2 = (Graphics2D)g;
    g2.scale(0.5, 0.5);

    for(int i = 0; i < lattice.getTotalNumberOfNodes(); i++){
        g.setColor(lattice.getLatticeNode()[i].getNodeColor());
        g.fillRect(lattice.getLatticeNode()[i].getxPos() - lattice.getNodeWidth() /2, 
                lattice.getLatticeNode()[i].getyPos() - lattice.getNodeHeight() /2
                , lattice.getNodeWidth(), lattice.getNodeHeight());
        g.setColor(Color.BLACK);
        g.drawRect(lattice.getLatticeNode()[i].getxPos() - lattice.getNodeWidth() /2, 
                lattice.getLatticeNode()[i].getyPos() - lattice.getNodeHeight() /2
                , lattice.getNodeWidth(), lattice.getNodeHeight());
    }

    g.setColor(Color.BLACK);
    double hund = 100;
    int intMaxValue = (int) (origMaxValue*hund);
    int intMinValue = (int) (origMinValue*hund);
    g.setFont(new Font("Arial", Font.PLAIN, 18));
    g.drawString(Double.toString(intMinValue/hund), 0, 470);
    g.drawString(Double.toString(intMaxValue/hund), 405, 470);

    for(int i = 0; i < 1024; i++){
        if(i < 256) g.setColor(new Color(0, (i % 256), 255));
        else if(i < 512) g.setColor(new Color(0, 255, 255 - (i % 256)));
        else if(i < 768) g.setColor(new Color((i % 256), 255, 0));
        else g.setColor(new Color(255, 255 - (i % 256), 0));
        double width = 350;
        g.fillRect((int)Math.ceil(i * width/1024) + 50, 455, (int)Math.ceil(width/1024), 15);
    }

    g.setColor(Color.BLACK);
    g.drawRect(50, 455, 350, 15);
}

}

Also, sometimes, I get this error when I wait too long after it FREEZES halfway... Exception in thread "Image Fetcher 0" java.lang.OutOfMemoryError: Java heap space at java.awt.image.DataBufferInt.(DataBufferInt.java:75) at java.awt.image.Raster.createPackedRaster(Raster.java:467) at java.awt.image.DirectColorModel.createCompatibleWritableRaster(DirectColorModel.java:1032) at sun.awt.image.ImageRepresentation.createBufferedImage(ImageRepresentation.java:253) at sun.awt.image.ImageRepresentation.setPixels(ImageRepresentation.java:559) at sun.awt.image.ImageDecoder.setPixels(ImageDecoder.java:138) at sun.awt.image.JPEGImageDecoder.sendPixels(JPEGImageDecoder.java:119) at sun.awt.image.JPEGImageDecoder.readImage(Native Method) at sun.awt.image.JPEGImageDecoder.produceImage(JPEGImageDecoder.java:141) at sun.awt.image.InputStreamImageSource.doFetch(InputStreamImageSource.java:269) at sun.awt.image.ImageFetcher.fetchloop(ImageFetcher.java:205) at sun.awt.image.ImageFetcher.run(ImageFetcher.java:169)

Sorry for the lengthy post. I am in dire need of your help. Thank you.

  • 1
    I was just going to say that if it's freezing halfway through, it's probably because of an OutOfmemoryError... I was right! – ControlAltDel Mar 23 '17 at 16:04
  • have you tried launching the jvm with -Xmx BIGGER HEAP ? – efekctive Mar 23 '17 at 16:05
  • @ControlAltDel - I tried increasing -xms and -xmx by 1024 from 256 in the eclipse.ini but it is still the same. – James-Andrew Sarmiento Mar 23 '17 at 16:13
  • @efekctive i tried launching eclipse with 1024 -xms and -xmx from the previous 256 – James-Andrew Sarmiento Mar 23 '17 at 16:14
  • @ControlAltDel sir, could you not mark it as duplicate? i do think it is not a duplicate question. thank you sir – James-Andrew Sarmiento Mar 23 '17 at 16:15
  • You are sharing the heap with eclipse. Try to run it outside eclipse. Busy now – efekctive Mar 23 '17 at 16:24
  • @efekctive is there another way? i mean, what if i wish to present it in eclipse? or is just my threading that is problematic? there is no significant change for me after i changed the -xms and -xmx to 1024 from 256. it still freezes halfway (300~ out of 700). A good observation shows that the for loop pauses for a while every now and then up until it totally freezes - and it is DECREMENTING. for example the pauses are, 1-60, 61-100, 101-130, 131-150, 151-170, and so on – James-Andrew Sarmiento Mar 23 '17 at 16:44
  • JAS: Explain why you do not think this is a duplicate question and explain that after increasing heap size that you are no longer getting OutOfMemoryError and I will rescind my vote. But like I said in my first comment, OutOfMemoryError happens commonly when you are doing serious data processing and its chugging along and then it stops because it's out of memory and nothing more can be processed. – ControlAltDel Mar 23 '17 at 18:38
  • @ControlAltDel, I did try to increase the heap size into 4 times its original, also tried manual Garbage collection and still the same result. – James-Andrew Sarmiento Mar 23 '17 at 20:14
  • I will try to debug this when I get some things done. Short of time – efekctive Mar 23 '17 at 23:20
  • @efekctive. Thank you. Im really in dire need of help – James-Andrew Sarmiento Mar 24 '17 at 04:30
  • @James-AndrewSarmiento did you get this to work? If I copy the code to Eclipse, can I reproduce/debug the problem? – efekctive Mar 27 '17 at 14:11
  • @efekctive hi. i have fixed the problem already. thank you for the help btw. – James-Andrew Sarmiento Mar 27 '17 at 16:15

0 Answers0