What I am doing is making a program that constantly takes screenshots of the users desktop and saves them as long as the user wants. I was initially placed a call to the method that captures in a while method but that was too slow and I need as many images take as soon as possible. What I decided to do was use threading.
While the program is running my PC get's slow (ie mouse flashes, moves slow, ect) and when I stop the program I get the following error.
Exception in thread "Thread-294" java.lang.OutOfMemoryError: Java heap space
at sun.awt.windows.WRobotPeer.getRGBPixels(Unknown Source)
at java.awt.Robot.createScreenCapture(Unknown Source)
at maple.Record.run(Record.java:29)
Here is the line that produces the error.
img = r.createScreenCapture(new Rectangle (0, 0, width, height));
Here is my class that contains the thread.
import java.awt.AWTException;
import java.awt.Rectangle;
import java.awt.Robot;
import java.awt.Toolkit;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;
import java.net.MalformedURLException;
import java.util.Vector;
import javax.imageio.ImageIO;
import javax.media.MediaLocator;
public class Record implements Runnable {
Robot r;
static int width = (int) Toolkit.getDefaultToolkit().getScreenSize().getWidth();
static int height = (int) Toolkit.getDefaultToolkit().getScreenSize().getHeight();
BufferedImage img;
public static boolean stop = false;
public void run() {
try {
r = new Robot();
img = r.createScreenCapture(new Rectangle (0, 0, width, height));
ImageIO.write(img, "png", new File(JavCapture.tmpLocation + "\\tmp\\" + System.currentTimeMillis() + ".png"));
} catch (IOException | AWTException e) { e.printStackTrace(); }
}
}
The error is produced when I am calling the JpegImagesToMovies that has been edited to work with .png files.
Please check out this link for more information on that.
How can I solve the problem, and how can I make the thread use less memory so that it doesn't slow the PC down.
Here is the call in the main method.
do {
(new Thread(new Record())).start();
} while (!Record.stop);