Memory Leak(?) when displaying images in java -
Memory Leak(?) when displaying images in java -
today kinda fiddled around image opening/scaling/displaying in java , wrote bit of code open image file, scale randomly , display short time.
the problem is: after displaying 100-1000 times, used memory of "javaw.exe" grows , grows, reached 1 gb of memory space.
i dont know memory leak in code since memory eating things picures , there 2 (the original image , 1 getting scaled, assigned same variable(temp) "older" ones should picked off gc), maybe guys have on it, pretty simple.
1) take image hard drive
2) gets scaled randomly
3) displayed short amount of time , disappears
4) go 2)
to scale image used library: http://www.thebuzzmedia.com/software/imgscalr-java-image-scaling-library/
import java.awt.image.bufferedimage; import java.io.ioexception; import javax.imageio.imageio; import javax.swing.imageicon; import javax.swing.jfilechooser; import javax.swing.jframe; import javax.swing.jlabel; import org.imgscalr.scalr; public static void main(string[] args) throws ioexception, interruptedexception { jfilechooser chooser = new jfilechooser(); chooser.showopendialog(null); bufferedimage originalimage = imageio.read(chooser.getselectedfile()); bufferedimage temp; while(true){ int width = (int) ((math.random()*1000)+1); int height = (int) ((math.random()*1000)+1); thread.sleep(1000); temp = scalr.resize(originalimage,scalr.mode.fit_exact, width, height); showimage(temp, 800); } } static void showimage(bufferedimage v,long length) throws interruptedexception { jframe frame = new jframe(); frame.add(new jlabel(new imageicon(v))); frame.setsize(v.getwidth(), v.getheight()); frame.setvisible(true); thread.sleep(length); frame.setvisible(false); }
this first time posting here, please inquire questions if unclear
thanks in advance!
edit: monitored memory javaw.exe needing
1 image displayed: 75m 100 pictures displayed: 330m 1000 pictures displayed: 2,4g
edit 2:
i have applied helpful advice still have growing amount of memory , images arent displayed anymore.. jframes empty.
import java.awt.image.bufferedimage; import java.io.file; import java.io.ioexception; import javax.imageio.imageio; import javax.swing.imageicon; import javax.swing.jfilechooser; import javax.swing.jframe; import javax.swing.jlabel; import javax.swing.swingutilities; import org.imgscalr.scalr; public class app { public static void main(string[] args) throws ioexception, interruptedexception { jfilechooser chooser = new jfilechooser(); chooser.showopendialog(null); bufferedimage originalimage = imageio.read(chooser.getselectedfile()); bufferedimage temp; jframe frame = new jframe(); while(true){ int width = (int) ((math.random()*1000)+1); int height = (int) ((math.random()*1000)+1); thread.sleep(1000); temp = scalr.resize(originalimage,scalr.mode.fit_exact, width, height); showimage(temp, 500, frame); } } static void showimage(bufferedimage v,long length, jframe frame) throws interruptedexception { swingutilities.invokelater( () -> { frame.removeall(); frame.revalidate(); frame.repaint(); frame.add(new jlabel(new imageicon(v))); frame.setsize(v.getwidth(), v.getheight()); frame.setvisible(true); seek { thread.sleep(length); } grab (exception e) {} frame.setvisible(false); frame.dispose(); }); } }
maybe set advice in wrong places in code.
the code below should want. used timer instead of thread.sleep. you're tying edt. draw image in container. should utilize jpanel instead (add jframe , override paintcomponent method). cleaned methods little.
import java.awt.image.bufferedimage; import java.awt.event.actionlistener; import java.awt.event.actionevent; import java.awt.graphics; import java.io.file; import java.io.ioexception; import javax.imageio.imageio; import javax.swing.imageicon; import javax.swing.jfilechooser; import javax.swing.jframe; import javax.swing.jlabel; import javax.swing.swingutilities; import javax.swing.timer; import org.imgscalr.scalr; public class app extends jframe implements actionlistener{ bufferedimage originalimage = null; bufferedimage temp = null; jfilechooser chooser = null; public app(){ setvisible(true); } public static void main(string[] args) throws ioexception, interruptedexception { swingutilities.invokelater( () -> { app app = new app(); timer timer = new timer(1000, app); timer.start(); }); } @override public void actionperformed(actionevent ae){ if(null == chooser){ chooser = new jfilechooser(); chooser.showopendialog(this); loadimage(); } showimage(); repaint(); } @override public void paint(graphics g){ super.paint(g); if(null == temp){ return; } g.drawimage(temp, 0, 0, null); } public void loadimage(){ try{ originalimage = imageio.read(chooser.getselectedfile()); } catch(ioexception ioe){ ioe.printstacktrace(); } } public void showimage() { int width = (int) ((math.random()*1000)+1); int height = (int) ((math.random()*1000)+1); temp = scalr.resize(originalimage,scalr.mode.best_fit_both, width, height); setsize(width, height); } }
java image memory-leaks
Comments
Post a Comment