Results 1 to 5 of 5
  1. #1
    Addez is offline Senior Member
    Join Date
    Aug 2009
    Posts
    294
    Rep Power
    0

    Question Java.lang.ExceptionOutOfMemory: Java heap space. lulzwut?!

    Hi!
    Im trying to make like moving objects and stuff..
    So I got this simple picture movig around on the screen.
    But the problem is i get the error shown in topic after running it for a while.
    I'v read about this error and that it would occure since things are piling up due to lack of threading.

    So I tried to thread it but im not good at it so I cant see where Im suppose to thread and how...
    I would really appriciate help on this ty =)

    Heres my code if needed:
    PHP Code:
    /**
     * You must use Threading!
     */
    import javax.swing.*;
    import javax.imageio.*;
    import java.awt.*; 
    import java.io.*;
    import java.awt.image.*;
    import java.awt.image.BufferedImage;
    import java.util.*;
     
    public class TestingArea {
        static Vector <Image> list = new Vector<Image>();
        static int X = 0;
        static int Y = 0;
        public static void main(String[] args) {
        	
        	// TODO, add your application code
        	gameFrame gf = new gameFrame();
        	while (true){
        		X += 10;
        		Y += 2;
        		if (X>400){
        			X = 0;
        			Y = 0;
        		}
    
        		paintIt obj = new paintIt();
        		new Thread(obj).start();
        		gf.add(obj);
        		Graphics g;
        		gf.repaint();
        		try{
     				 //do what you want to do before sleeping
      				Thread.currentThread().sleep(100);//sleep for 1000 ms
      				//do what you want to do after sleeptig
    			} catch(Exception ie){
    				//If this thread was intrrupted by nother thread 
    			}
        	}
        }
        
    }
    
    class gameFrame extends JFrame{
    	public gameFrame(){
    		
    		
    		
    		paintIt p = new paintIt();
    		add(p);
    		setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    		setSize(600,400);
    		setTitle("Image test");
    		setVisible(true);
    		
    	}
    }
    
    class paintIt extends JPanel implements Runnable{
    	BufferedImage pic;
    	
    	public void paintComponent(Graphics g){
    		super.paintComponent(g);
    		try{
    		BufferedImage pic = ImageIO.read(new File("C:\\Users\\Andreas\\Bilder\\CNDF.png"));
    		TestingArea.list.add(pic);
    		g.drawImage(pic,TestingArea.X,TestingArea.Y,null);
    		}catch (Exception e){
    			e.printStackTrace();
    		}
    	}
    	public void run(){
    	}
    }

  2. #2
    r035198x is offline Senior Member
    Join Date
    Aug 2009
    Posts
    2,388
    Rep Power
    7

  3. #3
    Addez is offline Senior Member
    Join Date
    Aug 2009
    Posts
    294
    Rep Power
    0

    Default

    Okej now I'v read that.
    But I still dont get it..
    Whats wrong with my code that makes something pile up uppon eachother?
    I meen, I use a thread got the paint method and all..
    I cant find what is causeing it..

  4. #4
    JosAH's Avatar
    JosAH is online now Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,386
    Blog Entries
    7
    Rep Power
    20

    Default

    You keep on adding paintIt objects to your gameFrame object without limit. btw, lots of your code doesn't make any sense and forget about Threads for now, you don't need them.

    kind regards,

    Jos

  5. #5
    Addez is offline Senior Member
    Join Date
    Aug 2009
    Posts
    294
    Rep Power
    0

    Default

    OK, ahhh well to hear, messed up threading in python too XD
    Well, okej, so what I wana do is move the picture..
    But I cant move it since it gets deleted eachtime I render again...
    So I have to add a new picture with new position..
    Or can I move a picture in a better way?

Similar Threads

  1. java.lang.OutOfMemoryError: Java heap space
    By paul in forum Advanced Java
    Replies: 11
    Last Post: 06-12-2010, 05:30 PM
  2. Replies: 10
    Last Post: 05-11-2009, 11:56 AM
  3. Replies: 3
    Last Post: 04-22-2009, 08:25 PM
  4. java.lang.OutOfMemoryError: Java heap space
    By vidjogamer in forum New To Java
    Replies: 3
    Last Post: 02-06-2009, 06:52 AM
  5. Replies: 14
    Last Post: 06-12-2008, 08:36 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •