I have very simple program which saves screenshots to ArrayList - for example I take 100 screenshots, add them to the list and then I clear the list. Like this:
public static void main(String[] args) {
JFrame frame = new JFrame(); // creating simple JFrame
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setVisible(true);
List<BufferedImage> list = new ArrayList<>();
for (int i = 1; i <= 100; i++) {
BufferedImage capture = makeScreenShot();
list.add(capture);
System.out.println("capture saved to arraylist " + i);
}
list.clear();
}
and the method makeScreenShot:
private static BufferedImage makeScreenShot() {
Robot robot = null;
try {
robot = new Robot();
} catch (AWTException e) {
e.printStackTrace();
}
Toolkit toolkit = Toolkit.getDefaultToolkit();
Rectangle screenBounds = new Rectangle(toolkit.getScreenSize());
BufferedImage capture = robot.createScreenCapture(screenBounds);
return capture;
}
If I run this simple program, my Task Manager (Windows) shows that this program uses about 1GB of RAM. Why the Java garbage colllector doesn't remove all the BufferedImages from RAM memory? There are no references to these BufferedImages, Am I right?