1

I am writing an web application where I need to send an image from servlet to client. Image is generated dynamically and is quite big(+-2MB). It might be jpeg, png, or gif.

Now, I am using ImageIO.write() to write the image to output stream, but its veeeery slow. It takes up to 6 seconds till the client see the image. I need to speed it up.

Any suggestions?

btw. I am aware of Looking for a faster alternative to ImageIO topic. But it didn't help me. Since it's slow with PNG ImageMagick is not a solution and I have tested JAI and it was even worse.

Thanks in advance

Edit:

To show you some code:

BufferedImage bi = [code to generate Image];
response.setContentType(mime);
ServletOutputStream out = response.getOutputStream();
ImageIO.write(bi,"png",out);

I stripped down Exception handling for readability.

Community
  • 1
  • 1
Voooza
  • 749
  • 4
  • 8
  • Could network speed be the culprit here? – darioo Oct 11 '11 at 13:15
  • What happens if you write the image to a ByteArrayOutputStream? Is it fast or as slow? If it's fast, then you just have a bandwidth problem between the client and the server. – JB Nizet Oct 11 '11 at 13:16
  • Are you using BufferedOutputStream, or some other form of buffering? Otherwise it's possible you're sending a lot of small packets. – Christoffer Hammarström Oct 11 '11 at 13:26
  • Network speed is not the issue. Writing to ByteArrayOutputStream takes even longer(9s). I really don't understand it at all. – Voooza Oct 11 '11 at 13:26
  • 1
    @Voooza: You might need to show us code that reproduces the problem. Boil it down to an [SSCCE](http://sscce.org) and edit it into the question. – Mark Peters Oct 11 '11 at 13:30
  • Try changing the output format from png to jpeg. If that speeds up output then the png writer that your JDK is using is the culprit. – Usman Saleem Oct 11 '11 at 14:25

3 Answers3

3

Image encoding in java is pretty slow in general but you may also want to ensure you have the native libraries installed as they make quite a noticeable difference in performance.

http://download.java.net/media/jai-imageio/builds/release/1.1/INSTALL-jai_imageio.html

Matt MacLean
  • 19,410
  • 7
  • 50
  • 53
  • I have followed that exact install guide(the Linux part) when installing JAI. Do I have native libraries? Should I install more? Thanks – Voooza Oct 11 '11 at 13:31
  • If you followed that instruction they "should" be installed. When you boot up your app, watch you log files and look out for messages about being unable to find native libraries. If you don't see any the libraries should be installed. – Matt MacLean Oct 11 '11 at 14:03
2

Be aware that ImageIO by default uses temporary files as cache when creating ImageInputStreams and ImageOutputStreams. This can be switched off by calling ImageIO.setUseCache(false).

For a more detailed explanation see this answer.

ltlBeBoy
  • 1,242
  • 16
  • 23
0

Are you sure that the

ImageIO

takes so long - maybe there is another problem, e.g.

  • slow (network) connection to the client
  • the generation (calculation) of the image takes a lot of time
ben_muc
  • 211
  • 3
  • 6
  • Yes I am sure. I tried it with static image saved on disk. If I just read from FileInputStream and send to ServletOutputStream it's OK. On the same server and client I am able to get under 200 ms. But when I load It to BufferedImage and ImageIO.write() it out it takes 6s. I am logging timestamps after each function call and it really is in ImageIO. Checked several times by me and 2 of my colleagues – Voooza Oct 12 '11 at 05:54