Results 1 to 4 of 4
- 02-04-2009, 06:34 AM #1
Member
- Join Date
- Feb 2009
- Posts
- 2
- Rep Power
- 0
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();
}
}
- 02-04-2009, 07:11 AM #2
Senior Member
- Join Date
- Jan 2009
- Posts
- 671
- Rep Power
- 5
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.
- 02-04-2009, 05:33 PM #3
Member
- Join Date
- Feb 2009
- Posts
- 2
- Rep Power
- 0
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 06:12 PM.
- 02-06-2009, 06:52 AM #4
Senior Member
- Join Date
- Jan 2009
- Posts
- 671
- Rep Power
- 5
Similar Threads
-
java.lang.OutOfMemoryError: Java heap space
By paul in forum Advanced JavaReplies: 11Last Post: 06-12-2010, 05:30 PM -
OutOfMemoryError: heap space (SJSAS PE 8.2 u4)
By mikamj in forum Advanced JavaReplies: 0Last Post: 11-21-2008, 07:37 AM -
Wanted help on "java.lang.OutOfMemoryError: Java heap space"
By itmani2020 in forum EclipseReplies: 16Last Post: 07-24-2008, 11:45 AM -
Java heap space?
By javanewbie in forum New To JavaReplies: 1Last Post: 06-24-2008, 06:55 PM -
Exception in thread "main" java.lang.OutOfMemoryError: Java heap space
By Eku in forum NetBeansReplies: 14Last Post: 06-12-2008, 08:36 AM


LinkBack URL
About LinkBacks
Reply With Quote
Bookmarks