arraylist - Garbage collector not working in java? -
this question has answer here:
- why java program taking memory? 3 answers
i have simple program saves screenshots arraylist - example take 100 screenshots, add them list , clear list. this:
public static void main(string[] args) { jframe frame = new jframe(); // creating simple jframe frame.setdefaultcloseoperation(jframe.exit_on_close); frame.setvisible(true); list<bufferedimage> list = new arraylist<>(); (int = 1; <= 100; i++) { bufferedimage capture = makescreenshot(); list.add(capture); system.out.println("capture saved arraylist " + i); } list.clear(); }
and method makescreenshot:
private static bufferedimage makescreenshot() { robot robot = null; try { robot = new robot(); } catch (awtexception e) { e.printstacktrace(); } toolkit toolkit = toolkit.getdefaulttoolkit(); rectangle screenbounds = new rectangle(toolkit.getscreensize()); bufferedimage capture = robot.createscreencapture(screenbounds); return capture; }
if run simple program, task manager (windows) shows program uses 1gb of ram. why java garbage colllector doesn't remove bufferedimages ram memory? there no references these bufferedimages, right?
i agree durron597, question have answer already. and, said, can't give program less memory needs complete cycle of screen capturing, processing , list clearing.
as stated in accepted answer, the jvm use memory can, , that's ok.
your program stabilize memory usage @ point , run forever. did try that, (not forever though).
but liked lower heap size version of test - how set up:
this how memory used after that:
and it's running 6 hours (almost 700k screenshots taken!):
so, if really need to, can still limit memory usage - have to?
from javadocs: "calling gc method suggests java virtual machine expend effort toward recycling unused objects in order make memory occupy available quick reuse."
so, system.gc()
suggestion - , might really bad one.
Comments
Post a Comment