Byte array to buffered image conversion slow

前端 未结 3 1759
独厮守ぢ
独厮守ぢ 2020-12-19 07:03

I have a simple server-side code that receives a byte array representing an image in JPEG format and returns the dimensions of the image.

public String proce         


        
3条回答
  •  没有蜡笔的小新
    2020-12-19 07:36

    I think the problem might be related to the fact that ImageIO by default uses disk caching (to a temp file), even if your source is a ByteArrayInputStream. So if your file system is slow, reading will also be slow, regardless of input source.

    You can disable the disk caching (at the cost of using more memory) with ImageIO.setUseCache(false). This will still cache your streams (to allow backwards seeking), but only in memory.

    It's also possible to set the cache directory to a specific path using ImageIO.setCacheDirectory(cacheDirectory), if you have a faster disk/ramdisk or similar to store your temp files.

    That said, your reported read times seems unreasonably high, even for disk cached reads. If the problem persists, I suggest using a profiler to find out where the time is spent, and look at possible optimizations.

    PS: I also have a custom ByteArrayImageInputStream that might help reduce both disk access and memory consumption, should this really be the problem.

提交回复
热议问题