How to read many images in java without memory leak -
i need read many images process them 1 after another. @ first used io library read each image:
file outputfile = new file(uri); bufferedimage imgbuff = imageio.read(outputfile); imgbuff.flush(); imgbuff = null; outputfile = null;
however takes lot of memory , process crashes. after doing research found there many issues reading many images using java io library. used simple progam verify memory leak caused reading images using image http://tinyurl.com/ku3ff7w:
import java.awt.image.bufferedimage; import java.io.file; import java.io.ioexception; import javax.imageio.imageio; import org.junit.test; public class memoryleaktest { static file outputfile = null; static bufferedimage imgbuff = null; public static void main(string args[]) { string uri = "/home/user/pictures/image.jpg"; outputfile = new file(uri); (int = 0; < 15000; i++) { outputfile = new file(uri); try { imgbuff = imageio.read(outputfile); } catch (ioexception e) { e.printstacktrace(); } { if (imgbuff != null) { imgbuff.flush(); imgbuff = null; } outputfile = null; } } } }
i have tried using imagej library, same problem occured converting image bufferedimage:
imageplus bb = op.openimage(uri); imgbuff = bb.getbufferedimage(); bb.killstack(); bb.flush(); bb.close();
i guess read images byte arrays , solve problem, solution not ideal. know if there library or method read many images in java without runing out of memory?
my solutions use img4java ( java interface imagemagick commandline).
in way delegate image manipulation external process freeing jvm go out fo memory.
see http://im4java.sourceforge.net/ , http://www.imagemagick.org/
Comments
Post a Comment