Results 1 to 4 of 4
  1. #1
    vidjogamer is offline Member
    Join Date
    Feb 2009
    Posts
    2
    Rep Power
    0

    Default java.lang.OutOfMemoryError: Java heap space

    Hi I keep getting this error.

    java.lang.OutOfMemoryError: Java heap space

    Im trying to make a basic remote desktop application, but I keep getting this error when I repeatedly create desktop images. Here is my code that causes the error. Any suggestions?

    public void run()
    {
    Toolkit toolkit = Toolkit.getDefaultToolkit();
    Robot robot;
    Dimension dim = toolkit.getScreenSize();
    Rectangle rect = new Rectangle((int)dim.getWidth(),(int)dim.getHeight() );
    BufferedImage buff;
    int counter = 0;
    try
    {
    robot = new Robot();

    while(true)
    {

    System.out.println(++counter);
    buff = robot.createScreenCapture(rect);
    icon = new ImageIcon(buff);
    toClient.writeObject(icon);



    Thread.sleep(20);


    }
    }
    catch(Exception ex)
    {
    ex.printStackTrace();
    }
    }

  2. #2
    toadaly is offline Senior Member
    Join Date
    Jan 2009
    Posts
    671
    Rep Power
    6

    Default

    My guess is that you are creating objects faster than they are being garbage collected. To avoid this, increase your heap size using the -Xmx command line option, and add a gc cleanup request after each 'writeObject'.

    Also, you may be duplicating pixels by creating an ImageIcon (unsure of it's implementation). You might try instead getRaster().getPixels() on the BufferedImage returned by the robot, and send a byte array instead of an ImageIcon.

    If the problem persists, your only hope may be to gracefully deal with the memory errors.

  3. #3
    vidjogamer is offline Member
    Join Date
    Feb 2009
    Posts
    2
    Rep Power
    0

    Default

    Yes, that was my guess as well. I tried increasing the heap size but after a while my machine began to lag significantly. How can I add a gc cleanup request?

    Edit:
    I put a System.gc() after the write object still with no luck.
    I am running both the client and the server on the same machine. Do you think that could be the problem?
    Last edited by vidjogamer; 02-04-2009 at 07:12 PM.

  4. #4
    toadaly is offline Senior Member
    Join Date
    Jan 2009
    Posts
    671
    Rep Power
    6

    Default

    Hmm. If System.gc isn't working, I'm wondering if there might be something in the "toClient" method that is causing references to persist. Are you by chance keeping some kind of a list of everything it sends?

Similar Threads

  1. java.lang.OutOfMemoryError: Java heap space
    By paul in forum Advanced Java
    Replies: 11
    Last Post: 06-12-2010, 06:30 PM
  2. OutOfMemoryError: heap space (SJSAS PE 8.2 u4)
    By mikamj in forum Advanced Java
    Replies: 0
    Last Post: 11-21-2008, 08:37 AM
  3. Replies: 16
    Last Post: 07-24-2008, 12:45 PM
  4. Java heap space?
    By javanewbie in forum New To Java
    Replies: 1
    Last Post: 06-24-2008, 07:55 PM
  5. Replies: 14
    Last Post: 06-12-2008, 09: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
  •