A
Animanera
Hi to all.
I developed a small servlet that give me the possibility to insert news
with photo. Each inserted images is resized to two resolutions (640x480
and 100x133) and saved to disk, discarding the original image.
The problem is that, after a certain number of upload of images files of
just 350kb, the Tomcat give me error of OutOfMemory and the upload of
files of such dimension come impossible (but it allows resizing of small
images). Restarting the server solve the problem, but only for few
uploads since after inserting again a certain number of file of the
aforesaid dimensions the problem come back.
You should know that:
- my ISP doesn't give to me the possibility to increase the maximum RAM
dedicated to the servlet container;
- the problem also occurs with sequential upload (therefore NOT
concurrent upload) alternated each other for a certain time (from 1 to 5
minutes);
- after some tests results that the problem appears after around 7
insertions of an image of 329kb;
Is there a way I can force the cleaning of images in memory immediately
(since I think that doing that resolves the probelm)?
The incriminated code is (as you will see I've tried to assign null to
image vars and call System.gc()...but without solving the problem):
public static void resizeImage(File imageFile, int width, int height,
float quality, int hints) throws IOException {
// Get input image
Image inputImage =
Toolkit.getDefaultToolkit().getImage(imageFile.getPath());
waitForImage(inputImage);
int inputWidth = inputImage.getWidth(null);
int inputHeight = inputImage.getHeight(null);
if ((width<=0) && (height<=0)) {
width=inputWidth;
height=inputHeight;
} else {
if (width==0)
width=inputWidth;
else if (width<0)
width=inputWidth*height/inputHeight;
if (height==0)
height=inputWidth;
else if (height<0)
height=inputHeight*width/inputWidth;
}
// Create output image.
Image outputImage = inputImage.getScaledInstance(width, height, hints);
waitForImage(outputImage);
// Encode JPEG file.
writeJPEG(outputImage, imageFile, quality);
inputImage = null;
outputImage = null;
System.gc();
}
public static int[] imageSize(File imageFile) throws IOException {
Image inputImage =
Toolkit.getDefaultToolkit().getImage(imageFile.getPath());
waitForImage(inputImage);
int[] result = new int[2];
result[0] = inputImage.getWidth(null);
result[1] = inputImage.getHeight(null);
inputImage = null;
System.gc();
return result;
}
public static void waitForImage( Image image ) {
try {
tracker.addImage(image, 0);
tracker.waitForID(0);
tracker.removeImage(image, 0);
} catch(InterruptedException exc) {
exc.printStackTrace();
} catch(NullPointerException exc) {
// Sometime appens. Why?
}
}
public static void writeJPEG(Image outputImage, File imageFile,
float outputQuality) throws java.io.IOException {
FileOutputStream os = new FileOutputStream(imageFile);
int outputWidth = outputImage.getWidth(null);
int outputHeight = outputImage.getHeight(null);
BufferedImage bi = new BufferedImage(outputWidth, outputHeight,
PJABufferedImage.TYPE_INT_RGB);
Graphics2D biContext = bi.createGraphics();
biContext.drawImage(outputImage, 0, 0, null);
JPEGImageEncoder encoder = JPEGCodec.createJPEGEncoder(os);
JPEGEncodeParam jep = JPEGCodec.getDefaultJPEGEncodeParam(bi);
jep.setQuality(outputQuality, true);
encoder.encode(bi, jep);
os.flush();
os.close();
}
Thank you very much in advance.
Bye, Animanera.
I developed a small servlet that give me the possibility to insert news
with photo. Each inserted images is resized to two resolutions (640x480
and 100x133) and saved to disk, discarding the original image.
The problem is that, after a certain number of upload of images files of
just 350kb, the Tomcat give me error of OutOfMemory and the upload of
files of such dimension come impossible (but it allows resizing of small
images). Restarting the server solve the problem, but only for few
uploads since after inserting again a certain number of file of the
aforesaid dimensions the problem come back.
You should know that:
- my ISP doesn't give to me the possibility to increase the maximum RAM
dedicated to the servlet container;
- the problem also occurs with sequential upload (therefore NOT
concurrent upload) alternated each other for a certain time (from 1 to 5
minutes);
- after some tests results that the problem appears after around 7
insertions of an image of 329kb;
Is there a way I can force the cleaning of images in memory immediately
(since I think that doing that resolves the probelm)?
The incriminated code is (as you will see I've tried to assign null to
image vars and call System.gc()...but without solving the problem):
public static void resizeImage(File imageFile, int width, int height,
float quality, int hints) throws IOException {
// Get input image
Image inputImage =
Toolkit.getDefaultToolkit().getImage(imageFile.getPath());
waitForImage(inputImage);
int inputWidth = inputImage.getWidth(null);
int inputHeight = inputImage.getHeight(null);
if ((width<=0) && (height<=0)) {
width=inputWidth;
height=inputHeight;
} else {
if (width==0)
width=inputWidth;
else if (width<0)
width=inputWidth*height/inputHeight;
if (height==0)
height=inputWidth;
else if (height<0)
height=inputHeight*width/inputWidth;
}
// Create output image.
Image outputImage = inputImage.getScaledInstance(width, height, hints);
waitForImage(outputImage);
// Encode JPEG file.
writeJPEG(outputImage, imageFile, quality);
inputImage = null;
outputImage = null;
System.gc();
}
public static int[] imageSize(File imageFile) throws IOException {
Image inputImage =
Toolkit.getDefaultToolkit().getImage(imageFile.getPath());
waitForImage(inputImage);
int[] result = new int[2];
result[0] = inputImage.getWidth(null);
result[1] = inputImage.getHeight(null);
inputImage = null;
System.gc();
return result;
}
public static void waitForImage( Image image ) {
try {
tracker.addImage(image, 0);
tracker.waitForID(0);
tracker.removeImage(image, 0);
} catch(InterruptedException exc) {
exc.printStackTrace();
} catch(NullPointerException exc) {
// Sometime appens. Why?
}
}
public static void writeJPEG(Image outputImage, File imageFile,
float outputQuality) throws java.io.IOException {
FileOutputStream os = new FileOutputStream(imageFile);
int outputWidth = outputImage.getWidth(null);
int outputHeight = outputImage.getHeight(null);
BufferedImage bi = new BufferedImage(outputWidth, outputHeight,
PJABufferedImage.TYPE_INT_RGB);
Graphics2D biContext = bi.createGraphics();
biContext.drawImage(outputImage, 0, 0, null);
JPEGImageEncoder encoder = JPEGCodec.createJPEGEncoder(os);
JPEGEncodeParam jep = JPEGCodec.getDefaultJPEGEncodeParam(bi);
jep.setQuality(outputQuality, true);
encoder.encode(bi, jep);
os.flush();
os.close();
}
Thank you very much in advance.
Bye, Animanera.