Question

I'm new to object oriented programming. I have made a class with a static method that captures various different sized rectangles of my screen at various intervals. The captures are stored in a static BufferedImage using a static Robot instance:

myStaticBufferedImage = myStaticRobot.createScreenCapture(arbitrarySizeRectangle);

Some operations are performed on the data (including occasionally writing the image to a bmp file). Upon the next capture, none of the image data from the previous capture is needed.

Since the bufferedImage will contain variable size data up to the full size of my screen resolution, should I declare it as so before my Application starts using it?

private static BufferedImage myStaticBufferedImage = new BufferedImage(RESOLUTION_X, RESOLUTION_Y, APPROPRIATE_IMAGE_TYPE);

Does this allocate a reusable chunk in memory without any leaks or inefficiencies as I repeatedly capture images? Or is the createScreenCapture method filling more and more memory each time I use it and simply assigning a new pointer to myStaticBufferedImage?

I'm running this app on OSX Lion alongside some CPU-intensive software. What is the best practice for memory management? Thanks!

Was it helpful?

Solution

You should not initialize myStaticBufferedImage to the largest resolution. createScreenCapture() will create its own BufferedImage in memory, and then myStaticBufferedImage will be referred to that. In no case will createScreenCapture() use the buffer from myStaticBufferedImage.

That said, if you'll only be creating a few BufferedImages, it probably won't make a difference next to the very CPU-intensive (memory-intensive?) software.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top