Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 3 of 3

Thread: OutOfMemoryError (Java heap space)

  1. #1
    Java kindergarten chronoz13's Avatar
    Join Date
    Mar 2009
    Location
    Philippines
    Posts
    659
    Thanks
    177
    Thanked 30 Times in 28 Posts

    Default OutOfMemoryError (Java heap space)

    whats wrong with this error?


    [COLOR="Red"]Exception in thread "main" java.lang.OutOfMemoryError: Java heap space
            at java.util.Arrays.copyOf(Arrays.java:2760)
            at java.util.Arrays.copyOf(Arrays.java:2734)
            at java.util.ArrayList.ensureCapacity(ArrayList.java:167)
            at java.util.ArrayList.add(ArrayList.java:351)
            at java.awt.Container.addImpl(Container.java:1054)
            at java.awt.Container.add(Container.java:365)
            at xTestx.NewMain.main(NewMain.java:69)[/COLOR]



    this is the code

    package xTestx;
     
    import javax.swing.JFrame;
    import javax.swing.JPanel;
    import java.awt.Color;
    import java.awt.Graphics;
    import java.awt.Container;
     
    /**
     *
     * @author
     */
     
    public class NewMain {
     
        public static void main(String[] args) {
     
            JFrame window;
            Container container;
     
            window = new JFrame();
            window.setSize(400, 400);
            window.setLocation(200, 300);
            window.setVisible(true);
     
            while (true) {
     
                JPanel panel = new JPanel() {
                Color color;
     
                    @Override
                    public void paintComponent(Graphics g) {
     
                        super.paintComponent(g);
     
                        int x = (int) (Math.floor(Math.random() * 3) + 1);
     
                        switch (x) {
     
                            case 1:
     
                                color = Color.BLACK;
                                break;
     
                            case 2:
     
                                color = Color.BLUE;
                                break;
     
                            case 3:
     
                                color = Color.GREEN;
                                break;
     
                            default:
     
                                color = Color.PINK;
                                break;
                        }
     
     
                        g.setColor(color);
                        g.fillRect(50, 50, 50, 50);
                    }
                };
     
                container = window.getContentPane();
                container.add(panel);
            }
        }
    }


    i just to make a simple program that demonstrates a shape that continuesly change color while running..


  2. #2
    Administrator copeg's Avatar
    Join Date
    Oct 2009
    Location
    US
    Posts
    5,318
    Thanks
    181
    Thanked 833 Times in 772 Posts
    Blog Entries
    5

    Default Re: OutOfMemoryError (Java heap space)

    The problem with the code posted is that you are continuously creating JPanels in a while loop and keeping a reference to those JPanels by calling the add, so it'll eventially max out. You should do this another way, such as creating a SwingTimer to update a single JPanel after a certain interval (How to Use Swing Timers (The Java™ Tutorials > Creating a GUI With JFC/Swing > Using Other Swing Features))
    Somwthing along the lines like this:
    public class NewMain extends JPanel implements ActionListener{
     
    	public static void main(String args[]){
    		NewMain nm = new NewMain();
     
    		SwingTimer timer = new SwingTimer( 60, nm );//update every 60 milliseconds
    		timer.start();
    	}
     
    	public void actionPerformed(ActionEvent e){
    		////do the drawing then call repaint()
    	}
    }

  3. The Following User Says Thank You to copeg For This Useful Post:

    chronoz13 (November 21st, 2009)

  4. #3
    Java kindergarten chronoz13's Avatar
    Join Date
    Mar 2009
    Location
    Philippines
    Posts
    659
    Thanks
    177
    Thanked 30 Times in 28 Posts

    Default Re: OutOfMemoryError (Java heap space)

    hmm i like that one.. tnx for that sir... im gonna post any further questions if i encounter some problems again

Similar Threads

  1. Replies: 15
    Last Post: February 28th, 2010, 10:30 PM
  2. how do you leave a line space in java?
    By ss7 in forum Java Theory & Questions
    Replies: 7
    Last Post: November 5th, 2009, 08:20 AM