I have a small piece of code that takes a screenshot of my desktop every five minutes.
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 at all?
/**
* 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 > screenBounds.x) {
allScreenBounds.x = screenBounds.x;
}
if (allScreenBounds.y > screenBounds.y) {
allScreenBounds.y = screenBounds.y;
}
// finding the other corner
if (farx < (screenBounds.x + screenBounds.width)) {
farx = screenBounds.x + screenBounds.width;
}
if (fary < (screenBounds.y + screenBounds.height)) {
fary = screenBounds.y + screenBounds.height;
}
allScreenBounds.width = farx - allScreenBounds.x;
allScreenBounds.height = fary - allScreenBounds.y;
}
return allScreenBounds;
}
}
The other answers are right that Java will use as much memory as it is allowed to, at which point it will garbage collect. To work around this, you can specify a smaller max heap size in the JVM settings. You do this with the -Xmx setting. For example, if you think you only need 32MB, run it as:
java -Xmx32M [your main class or jar here]
The heap of your program (the non-stack memory) will never take more than 32MB, but it will crash if it needs more than that at once (and that's where you'll need to profile). I don't see any obvious leaks in your program (assuming ImageIO doesn't require any cleanup), though, so I think you'll be fine.
JVM garbage collector will eventually clear your memory heap. For manually clearing that heap call Runtime.getRuntime().gc();, but I don't advise doing that for every 5 minutes.
For a modern computer, 200MB is not an excessive amount of memory. The JVM will let the heap grow for a while if you're creating and discarding lots of objects so that your program doesn't get bogged down with garbage collection. Let your program run for several hours and then check back if you think there's a problem.
Related
For a project I am working on, I was tasked with creating a way of converting an image into a non-cryptographic hash so it could be easily compared with similar images, however I ran into an issue where the JVM would begin to recklessly consume memory, despite the Java Monitoring & Management Console not reporting any increase in memory consumption.
When I first ran the application, the Task Manager would report values like this:
However after only about 30 seconds, those values would have doubled or tripled.
I used the JMMC to create a dump of the process, but it only reported around 1.3MB of usage:
The strangest part to me is that the application performs an operation which lasts for about 15 seconds, then it waits for 100 seconds (debug), and it is during the 100 seconds of thread sleeping that the memory used doubles.
Here are my two classes:
ImageHashGenerator.java
package com.arkazex.srcbot;
import java.awt.Color;
import java.awt.Image;
import java.awt.image.BufferedImage;
public class ImageHashGenerator {
public static byte[] generateHash(Image image, int resolution) {
//Resize the image
Image rscaled = image.getScaledInstance(resolution, resolution, Image.SCALE_SMOOTH);
//Convert the scaled image into a buffered image
BufferedImage scaled = convert(rscaled);
//Create the hash array
byte[] hash = new byte[resolution*resolution*3];
//Variables
Color color;
int index = 0;
//Generate the hash
for(int x = 0; x < resolution; x++) {
for(int y = 0; y < resolution; y++) {
//Get the color
color = new Color(scaled.getRGB(x, y));
//Save the colors
hash[index++] = (byte) color.getRed();
hash[index++] = (byte) color.getGreen();
hash[index++] = (byte) color.getBlue();
}
}
//Return the generated hash
return hash;
}
//Convert Image to BufferedImage
private static BufferedImage convert(Image img) {
//Create a new bufferedImage
BufferedImage image = new BufferedImage(img.getWidth(null), img.getHeight(null), BufferedImage.TYPE_3BYTE_BGR);
//Get the graphics
image.getGraphics().drawImage(img, 0, 0, null);
//Return the image
return image;
}
}
Test.java
package com.arkazex.srcbot;
import java.io.File;
import java.io.IOException;
import javax.imageio.ImageIO;
public class Test {
public static void main(String[] args) throws IOException {
//Create a hash
byte[] hash = ImageHashGenerator.generateHash(ImageIO.read(new File("img1.JPG")), 8); //Memory grows to around 150MB here
System.out.println(new String(hash));
try{ Thread.sleep(100000); } catch(Exception e) {} //Memory grows to around 300MB here
}
}
EDIT: The program stopped growing to 300MB after a few seconds for no apparent reason. I had not changed anything in the code, it just stopped doing it.
I think that what you missing here is that some of the image classes use off-heap memory. This is (presumable) invisible to the JMMC because it only gets told about on-heap usage. The OS-level memory usage monitoring sees it ... because it is looking at the total resource consumption of the JVM running your application.
The problem is that the off-heap memory blocks are only reclaimed when the corresponding on-heap image objects are finalized. That only happens when they are garbage collected.
The program stopped growing to 300MB after a few seconds for no apparent reason. I had not changed anything in the code, it just stopped doing it.
I expect that the JVM decided it was time to do a full GC (or something like that) and that caused it to free up lots of space in the off-heap memory pool. That meant the JVM no longer needed to keep growing the pool.
(I am being deliberately vague because I don't actually know how off-heap memory allocation works under the covers in a modern JVM. But if you want to investigate, the JVM source code can be downloaded ...)
See the explanation in the /** comments */
public class Test {
public static void main(String[] args) throws IOException {
//Create a hash
/** Here it allocates (3 * resolution^2 )bytes of memory to a byte array */
byte[] hash = ImageHashGenerator.generateHash(ImageIO.read(new File("img1.JPG")), 8); //Memory grows to around 150MB here
/** And here it again allocates the same memory to a String
Why print a String of 150 million chars? */
System.out.println(new String(hash));
try{ Thread.sleep(100000); } catch(Exception e) {} //Memory grows to around 300MB here
}
}
I am trying to create a plugin on ImageJ that creates a 10 pixels wide frame inside of the ROI. I am having trouble doing this, the code I have so far simply fills in the the entire space start 10 pixels in the ROI. The code I have is below.
import ij.*;
import ij.plugin.filter.PlugInFilter;
import ij.process.*;
import java.awt.*;
public class Frame implements PlugInFilter {
public int setup(String arg, ImagePlus imp) {
if (arg.equals("about"))
{showAbout(); return DONE;}
return DOES_8G+DOES_STACKS+SUPPORTS_MASKING;
}
public void run(ImageProcessor ip) {
byte[] pixels = (byte[])ip.getPixels();
int width = ip.getWidth();
Rectangle r = ip.getRoi();
int offset, i;
for (int y=r.y+10; y<(r.y+r.height)-10; y++) {
offset = y*width;
for (int x=r.x+10; x<(r.x+r.width)-10; x++) {
i = offset + x ;
pixels[i] = (byte)(255);
}
}
}
Why do you try it the hard (i.e. low level API) way?
Using ImageJ's command recorder (Plugins > Macros > Record... and setting "Record:" to Java), you can quickly get the required Java code after
creating a selection,
adding it to the ROI manager,
running Edit > Selection > Enlarge...,
adding the new ROI to the manager,
combining the two ROIs using XOR, and
filling the resulting selection with the foreground color.
The code would look like this:
import ij.IJ;
import ij.ImagePlus;
import ij.plugin.frame.RoiManager;
// [...]
RoiManager rm = RoiManager.getInstance();
if (rm==null) rm = new RoiManager();
rm.runCommand("Deselect");
if (rm.getCount() > 0) rm.runCommand("Delete");
ImagePlus imp = IJ.getImage();
// imp.setRoi(30, 50, 150, 100);
rm.addRoi(imp.getRoi());
IJ.run(imp, "Enlarge...", "enlarge=-10");
rm.addRoi(imp.getRoi());
rm.setSelectedIndexes(new int[]{0,1});
rm.runCommand("XOR");
IJ.run(imp, "Fill", "slice");
imp.show();
I have question how to check how many pages will be printed when i use my own Printable class.
I need it, because i want to have "page_number/total_pages" in footer of each page.
In my case problem with estimation of rows is that rows are wrapped. Additionaly the are empty rows beetwen some of the lines of text and there are some other cases which can prevent regular spreading of text. Generally this is not uniform printing.
As you know, rendering process is done after calling all print dialog windows.
Is any way to deal with this issue or should i somehow launch printing simulation , to receieve real number of pages? Or maybe should i implement some other class?
Regards
You can use following to get no. of pages:
int linesPerPage; // No. of lines per Page to be drawn.
static int numPages; // No. of pages too be rendered.
public int getNumberOfPages() // Override method to get Number Of Pages.
{
return numPages;
}
in main..
linesPerPage = (int)Math.floor(format.getImageableHeight()/linespacing);
numPages = (DATA_to_Print.length - 1)/linesPerPage + 1;
in override print method..
if ((pagenum < 0) | (pagenum >= numPages))
{
return NO_SUCH_PAGE;
}
Each time when print method will be call, value of the numPages will increment.
I figured out how to do it. Maybe this is no elegant, but it works (i tested estimation with document with 1200 pages and estimation is accurate). I will show you rather concept supported with couple line of code because my Printable classes are complex.
This is class which calls all operations connected with printing:
package print_manager;
import icd_searcher.ResultContainer;
import java.awt.Point;
import java.awt.print.PageFormat;
import java.awt.print.Paper;
import java.awt.print.Printable;
import java.awt.print.PrinterException;
import java.awt.print.PrinterJob;
import others.MeasuredBox;
public class PrintManager
{
private PrinterJob printerJob;
private SimpleResultPrinter srp;
/**
* Print printable object.
*
* #param toPrint
*/
public void initPrint(ResultContainer resultToPrint)
{
printerJob = PrinterJob.getPrinterJob();
PageFormat selectedArea = printerJob.pageDialog(printerJob.defaultPage());
MeasuredBox margin = new MeasuredBox(new Point((int) selectedArea.getImageableX(), (int) selectedArea.getImageableY()), (int) selectedArea.getImageableWidth(), (int) selectedArea.getImageableHeight() - 72);
srp = new SimpleResultPrinter(resultToPrint, margin);
SimpleResultPrinterSimulator srpSimulation = new SimpleResultPrinterSimulator(resultToPrint, margin);
// total pages simulation
int totalPages = 0;
try
{
while (srpSimulation.print(selectedArea, totalPages) != Printable.NO_SUCH_PAGE)
{
totalPages++;
}
}
catch (PrinterException e)
{
e.printStackTrace();
}
printerJob.setPrintable(srp, widenedPage);
if (printerJob.printDialog())
{
try
{
printerJob.print();
}
catch (PrinterException exc)
{
System.out.println(exc);
}
}
}
}
Method print in SimpleResultPrinter
public int print(Graphics g, PageFormat page, int pageIndex) throws
PrinterException {...}
Method print in SimpleResultPrinterSimulator
public int print(PageFormat page, int pageIndex) throws PrinterException
{...}
You need to know about this:
MeasuredBox and ResultContainer are my custom classes that have no meaning in this conception - treat them as a "some classes".
Class SimpleResultPrinter which is Printable has method print(...) which is called by printerJob.print().
SimpleResultPrinterSimulator has the same method print(...) like SimpleResultPrinter except there is no draw actions in it for better performance and of course SimpleResultPrinterSimulator has only this method. Also SimpleResultPrinterSimulator extends Component because i need Graphics object to measure font height.
My solution is just to launch simulation of printing and print(...) makes the same actions like will be done soon during printing except of real drawing to any Graphics object. Finally i recieves total number of pages.
I know this is not elegant and i make the same action twice (lost of performance), but i don't see any other way to estimate number of pages in complex printing.
I am using JasperReports to print from my Java application. All the prints were OK for all costumers for quite a bit of time, but recently we have installed the software for couple of costumers that all have Konica Minolta PagePro 1200 or 1350W printers. And all those costumers get the same result - the printing is "stretched" - as if I printed A4 report on A5 paper. Except that I am printing A4 on A4 and on all other printers it works fine.
Does anyone have any idea what might be the cause?
This questions seems similar to https://stackoverflow.com/questions/15854722/jasper-report-printing-stretched but the workaround presented there (messing with printer paper size etc.) did not lead to desired result.
Here is an example of the print:
And this is what the print should look like (there are slighlty different data, but I guess the problem is clear)
Thanks for any suggestions.
EDIT:
The issue can be reproduced with a simple direct call to printing API:
import java.awt.*;
import java.awt.print.Book;
import java.awt.print.PageFormat;
import java.awt.print.Paper;
import java.awt.print.Printable;
import java.awt.print.PrinterException;
import java.awt.print.PrinterJob;
/**
*
* #author MaCe
*/
public class PrintTest {
static Printable printable = new Printable() {
#Override
public int print(Graphics graphics, PageFormat pageFormat, int pageIndex) throws PrinterException {
Graphics2D grx = (Graphics2D)graphics;
grx.drawLine(20,20, getPageWidth() / 2, 20);
return Printable.PAGE_EXISTS;
}
};
protected static int getPageWidth() {
//width of A4 in 1/72 of inches
return 595;
}
protected static int getPageHeight() {
//height of A4 in 1/72 of inches
return 842;
}
public static void main(String args[]) throws PrinterException {
Frame f = new Frame();
f.show();
//Build a buggy print job using PrinterJob class
PrinterJob printJob = PrinterJob.getPrinterJob();
/**
* Fix for bug ID 6255588 from Sun bug database
*/
try {
printJob.setPrintService(printJob.getPrintService());
} catch (PrinterException e) {
}
PageFormat pageFormat = printJob.defaultPage();
Paper paper = pageFormat.getPaper();
printJob.setJobName("Buggy output");
pageFormat.setOrientation(PageFormat.PORTRAIT);
paper.setSize(getPageWidth() , getPageHeight());
paper.setImageableArea(
0,
0, getPageWidth(), getPageHeight());
pageFormat.setPaper(paper);
Book book = new Book();
book.append(printable, pageFormat, 1);
printJob.setPageable(book);
if (printJob.printDialog()) {
printJob.print();
}
//Build a good print job using PrintJob class
PrintJob pjob = f.getToolkit().getPrintJob(f, "Good output", null);
if (pjob!=null) {
Graphics g = pjob.getGraphics();
g.drawLine(20, 20, pjob.getPageDimension().width / 2, 20);
pjob.end();
}
System.exit(0);
}
}
Now this code produces two prints that on most printers both print a line from the top left corner of the paper to the middle of the paper. However on the aforementioned printers the first print creates a thick line across the whole paper (200% scale) - the second variant is however OK even on Minolta printers.
Seems like a Java bug, since all other programs print normally with the printer. The bug was accepted by Sun, but closed as they could not get their hands on the correct printer (https://bugs.openjdk.java.net/browse/JDK-804159)
Is your platform windows/mac/linux?
I've had trouble with printing with label printers when the print job's media hint did not communicate fully through to the driver. Its actually very similar to your problem, stretched prints, skipped pages etc,.
I dont know if you have code to modify, but we had to switch to System Printing instead of Java Printing. Then with the System Printing settings, (windows spooler in my case), I had to create a printer with the exact driver settings needed to print properly. Its rarely anything you can really do in JasperReports other than specify the width/height/orientation.
I assume your report prints fine when saved to PDF, then print from a PDF Viewer.
I have found a workaround to the problem. The solution is to scale the graphics to half before printing. This is not easy to do with JasperReports, as the graphics object is not exposed anywhere. However, my app uses #AspectJ so I managed to fiddle with the graphics through a custom aspect:
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.print.PageFormat;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Before;
#Aspect
public class JRMinoltaAspect {
#Before("execution(int net.sf.jasperreports.engine.print.JRPrinterAWT.print(java.awt.Graphics, java.awt.print.PageFormat , int )) && args(g, pf, ..)")
public void scalePrint(Graphics g, PageFormat pf){
if(pf.getOrientation() == PageFormat.LANDSCAPE){
g.translate(0, (int)pf.getHeight() / 2 );
}
((Graphics2D)g).scale(0.5, 0.5);
}
}
This is a rather tricky question as I have found no information online. Basically, I wish to know how to check if a computer is idle in Java. I wish a program to only work if the computer is in active use but if it is idle then to not.
The only way i can think of doing this is hooking into the mouse/keyboard and having a timer.
MSN Messenger has that "away" feature, I wish for something similar to this.
Java has no way of interacting with the Keyboard, or Mouse at the system level outside of your application.
That being said here are several ways to do it in Windows. The easiest is probably to set up JNI and poll
GetLastInputInfo
for keyboard and mouse activity.
Im not a professional, but i have an idea:
you can use the java's mouse info class to check mouse position at certian intervals say like:
import java.awt.MouseInfo;
public class Mouse {
public static void main(String[] args) throws InterruptedException{
while(true){
Thread.sleep(100);
System.out.println("("+MouseInfo.getPointerInfo().getLocation().x+", "+MouseInfo.getPointerInfo().getLocation().y+")");
}
}
}
replace the print statement with your logic, like if for some interval say 1 min the past position of mouse is the same as new position (you can simply compare only the x-coordinates), that means the system is idle, and you can proceed with your action as you want (Hopefully it is a legal activity that you want to implement :-)
Besure to implement this in a new thread, otherwise your main program will hang in order to check the idle state.
You can solve this with the help of Java's robot class.
Use the robot class to take a screenshot, then wait for lets say 60 seconds and take another screenshot. Compare the screenshots with each other to see if any changes
has happened, but don't just compare the screenshots pixel by pixel. Check for the percentage of the pixels that has changed. The reason is that you don't want small differences like Windows clock to interfere with the result. If the percentage is less that 0.005% (or whatever), then the computer is probably idling.
import java.awt.AWTException;
import java.awt.DisplayMode;
import java.awt.GraphicsDevice;
import java.awt.GraphicsEnvironment;
import java.awt.Rectangle;
import java.awt.Robot;
import java.awt.Toolkit;
import java.awt.image.BufferedImage;
public class CheckIdle extends Thread {
private Robot robot;
private double threshHold = 0.05;
private int activeTime;
private int idleTime;
private boolean idle;
private Rectangle screenDimenstions;
public CheckIdle(int activeTime, int idleTime) {
this.activeTime = activeTime;
this.idleTime = idleTime;
// Get the screen dimensions
// MultiMonitor support.
int screenWidth = 0;
int screenHeight = 0;
GraphicsEnvironment graphicsEnv = GraphicsEnvironment
.getLocalGraphicsEnvironment();
GraphicsDevice[] graphicsDevices = graphicsEnv.getScreenDevices();
for (GraphicsDevice screens : graphicsDevices) {
DisplayMode mode = screens.getDisplayMode();
screenWidth += mode.getWidth();
if (mode.getHeight() > screenHeight) {
screenHeight = mode.getHeight();
}
}
screenDimenstions = new Rectangle(0, 0, screenWidth, screenHeight);
// setup the robot.
robot = null;
try {
robot = new Robot();
} catch (AWTException e1) {
e1.printStackTrace();
}
idle = false;
}
public void run() {
while (true) {
BufferedImage screenShot = robot
.createScreenCapture(screenDimenstions);
try {
Thread.sleep(idle ? idleTime : activeTime);
} catch (InterruptedException e) {
e.printStackTrace();
}
BufferedImage screenShot2 = robot
.createScreenCapture(screenDimenstions);
if (compareScreens(screenShot, screenShot2) < threshHold) {
idle = true;
System.out.println("idle");
} else {
idle = false;
System.out.println("active");
}
}
}
private double compareScreens(BufferedImage screen1, BufferedImage screen2) {
int counter = 0;
boolean changed = false;
// Count the amount of change.
for (int i = 0; i < screen1.getWidth() && !changed; i++) {
for (int j = 0; j < screen1.getHeight(); j++) {
if (screen1.getRGB(i, j) != screen2.getRGB(i, j)) {
counter++;
}
}
}
return (double) counter
/ (double) (screen1.getHeight() * screen1.getWidth()) * 100;
}
public static void main(String[] args) {
CheckIdle idleChecker = new CheckIdle(20000, 1000);
idleChecker.run();
}
}
Nothing in the platform-independent JRE will answer this question. You might be able to guess by measuring clock time for a calculation, but it wouldn't be reliable. On specific platforms, there might be vendor APIs that might help you.
1) Make a new thread.
2) Give it a super super low priority (the lowest you can)
3) Every second or two, have the thread do some simple task. If super fast, at least 1 CPU is prolly idle. If it does it slow, then at least 1 core is prolly not idle.
Or
Just run your program at a low priority. That will let the OS deal with letting other programs run over your program.