Note that there are some explanatory texts on larger screens.

plurals
  1. POWhy is this Java program taking up so much memory?
    primarykey
    data
    text
    <p>I have a small piece of code that takes a screenshot of my desktop every five minutes - it's the work of a moment to flick thought and work out how many screenshots are of, say, facebook... </p> <p>However I'm a little confused by the amount of memory it takes up - often it will creep up to 200mb of RAM, which I'm sure is excessive... Can anyone tell me a) sensible ways to reduce the memory footprint or b) why it's going up <em>at all</em>? </p> <pre><code>/** * Code modified from code given in http://whileonefork.blogspot.co.uk/2011/02/java-multi-monitor-screenshots.html following a SE question at * http://stackoverflow.com/questions/10042086/screen-capture-in-java-not-capturing-whole-screen and then modified by a code review at http://codereview.stackexchange.com/questions/10783/java-screengrab */ package com.tmc.personal; import java.awt.AWTException; import java.awt.GraphicsDevice; import java.awt.GraphicsEnvironment; import java.awt.Rectangle; import java.awt.Robot; import java.awt.image.BufferedImage; import java.io.File; import java.io.IOException; import java.text.DateFormat; import java.text.SimpleDateFormat; import java.util.Date; import java.util.TimeZone; import java.util.concurrent.TimeUnit; import javax.imageio.ImageIO; class ScreenCapture { static int minsBetweenScreenshots = 5; public static void main(String args[]) { int indexOfPicture = 1000;// should be only used for naming file... while (true) { takeScreenshot("ScreenCapture" + indexOfPicture++); try { TimeUnit.MINUTES.sleep(minsBetweenScreenshots); } catch (Exception e) { e.printStackTrace(); } } } //from http://www.coderanch.com/t/409980/java/java/append-file-timestamp private final static String getDateTime() { DateFormat df = new SimpleDateFormat("yyyy-MM-dd_hh:mm:ss"); df.setTimeZone(TimeZone.getTimeZone("PST")); return df.format(new Date()); } public static void takeScreenshot(String filename) { Rectangle allScreenBounds = getAllScreenBounds(); Robot robot; try { robot = new Robot(); BufferedImage screenShot = robot.createScreenCapture(allScreenBounds); ImageIO.write(screenShot, "jpg", new File(filename + getDateTime()+ ".jpg")); } catch (AWTException e) { System.err.println("Something went wrong starting the robot"); e.printStackTrace(); } catch (IOException e) { System.err.println("Something went wrong writing files"); e.printStackTrace(); } } /** * Okay so all we have to do here is find the screen with the lowest x, the * screen with the lowest y, the screen with the higtest value of X+ width * and the screen with the highest value of Y+height * * @return A rectangle that covers the all screens that might be nearby... */ private static Rectangle getAllScreenBounds() { Rectangle allScreenBounds = new Rectangle(); GraphicsEnvironment ge = GraphicsEnvironment.getLocalGraphicsEnvironment(); GraphicsDevice[] screens = ge.getScreenDevices(); int farx = 0; int fary = 0; for (GraphicsDevice screen : screens) { Rectangle screenBounds = screen.getDefaultConfiguration().getBounds(); // finding the one corner if (allScreenBounds.x &gt; screenBounds.x) { allScreenBounds.x = screenBounds.x; } if (allScreenBounds.y &gt; screenBounds.y) { allScreenBounds.y = screenBounds.y; } // finding the other corner if (farx &lt; (screenBounds.x + screenBounds.width)) { farx = screenBounds.x + screenBounds.width; } if (fary &lt; (screenBounds.y + screenBounds.height)) { fary = screenBounds.y + screenBounds.height; } allScreenBounds.width = farx - allScreenBounds.x; allScreenBounds.height = fary - allScreenBounds.y; } return allScreenBounds; } } </code></pre>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload