0

I'm building a web-site using Java Spring and Hibernate and using Tomcat 7 as server. I have a page of this site where, once the user clicks on an image other two images are loaded. The workflow is the following:

Image Clicked -> Calculation(spring method) -> Images saved on the server as jpg -> Images updated from the server and showed to the client.

The images are loaded like follows:

    response.setContentType("image/jpg");
    OutputStream out = response.getOutputStream();  
    FileInputStream in = new FileInputStream(xzCrossUrl);  
    int size = in.available();  
    byte[] content = new byte[size];  
    in.read(content);  
    out.write(content);  
    in.close();  
    out.close();

I know this probably is not the best way to doing it, but I have not much experience yet.

Locally it works fine, but when I put the .war on the tomcat directory and connect to the server, a Java outOfMemory heap space problem is coming out, and the images are loaded much slower than locally.

I tried to increase the memory used by tomcat but it seems not to work; maybe I'm doing something wrong.

Can you please help me with this?

Thank you very much in advance!

2
  • Try these steps told here first: stackoverflow.com/questions/2718786/… Commented Oct 25, 2013 at 13:00
  • Thank you for the link. I tried to increase tomcat heap space previously but it didn't fix the problem. I will try to install jprofile as soon as possibile and let you know Commented Oct 25, 2013 at 14:28

2 Answers 2

1

I can't put this in a comment because I don't have enough cred, so...

While it may be something you can fix with the Tomcat configuration, what code you have will not scale for any image. You should declare the byte[] to be a fixed size and then read and write until you've consumed all of the file bytes:

// as a class scoped constant
private static final int BUFFERSIZE = 1024 << 8;

BufferedOutputStream out = new BufferedOutputStream(response.getOutputStream(), BUFFERSIZE);
BufferedInputStream in = new BufferedInputStream(new FileInputStream(xzCrossUrl));  
int bytesRead = 0;
byte[] content = new byte[BUFFERSIZE];  
while((bytesRead = in.read(content) != -1){
   out.write(content,0,bytesRead);
}
// Don't forget appropriate exception handling with a try/finally!
in.close();  
out.close();

FYI: I wrote this here, not in an IDE and have not compiled it, so my apologies if isn't perfect. Hopefully you get the gist.

Sign up to request clarification or add additional context in comments.

1 Comment

Thank for your answer. You have missed a bracket in the while but it's ok :) Unfortunately this didn't solve my problem. I'm starting to think that the problem is in the computation part concering the images. I don't know if the problem could be in the configuration of the server too.
0

How about using IOUtils.copy() from the Apache Commons IO package - which will copy the input stream to the output stream and will buffer internally so you don't have to.

response.setContentType("image/jpg");
OutputStream out = response.getOutputStream();  
FileInputStream in = new FileInputStream(xzCrossUrl);  
IOUtils.copy(in, out);

IOUtils.closeQuietly(in);
IOUtils.closeQuietly(out);

For larger files you can use IOUtils.copyLarge()

For more info on Commons IO see http://commons.apache.org/proper/commons-io/

1 Comment

Thank you for the tip, but this didn't solve my problem :( Maybe the issue is not in the loading of the images

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.