java - Will this cause memory leak? -
will code causes memory leak?
when garbage collector activated? when timer finishes? or gc called though timer still running?
public static swingworker sw; t2 = new timer (300,this); t2.start(); @override public void actionperformed(actionevent arg0) { try { sw = new textareamainpanelworker(); sw.execute(); } catch (throwable e) { e.printstacktrace(); } }
textareamainpanelworker class:
public class textareamainpanelworker extends swingworker<integer, integer> { protected integer doinbackground() throws exception { connectmysql.fetchmessage(mainpanel.jtep,mainpanel.sd,mainpanel.count); return 1; } protected void done() { try { connectmysql.rodolfol(mainpanel.jtep, mainpanel.sd); } catch (exception e) { e.printstacktrace(); } }
method query database:
public static void fetchmessage(jtextpane jtep,styleddocument sd,int count ) { try{ string query = "select members.username, message,color chat join members on chat.user_id = members.id message_id > "+count+" , user_id != 1"; ps = con.preparestatement(query); rs = ps.executequery(); }catch(exception e){} } public static void rodolfol(jtextpane jtep,styleddocument sd){ try { while(rs.next()) { try { final jlabel jp = new jlabel(rs.getstring("username")+ "\n"); jp.setalignmenty(0.75f); final string usernamechat = rs.getstring("username"); jp.addmouselistener(new mouselistener(){ @override public void mouseclicked(mouseevent e) {} @override public void mouseentered(mouseevent e) { cursor c = cursor.getpredefinedcursor(cursor.hand_cursor); jp.setcursor(c); } @override public void mouseexited(mouseevent e) { } @override public void mousepressed(mouseevent e) { if(swingutilities.isrightmousebutton(e)){system.out.print("lawl");} if(e.getclickcount() == 2)new onetoonechat(usernamechat); jp.setforeground(color.blue); } @override public void mousereleased(mouseevent e) { jp.setforeground(color.black); } }); jp.setfont(new font("arial",font.bold,16)); jtep.insertcomponent(jp); styleconstants.setforeground(mainpanel.sas2, color.black); mainpanel.sd.insertstring(mainpanel.sd.getlength(), ": ", mainpanel.sas2); styleconstants.setforeground(mainpanel.sas,new color(integer.parseint(rs.getstring("color")))); sd.insertstring(sd.getlength(),rs.getstring("message")+ "\n", mainpanel.sas); } catch (badlocationexception e1) { }finally{ } mainpanel.count++;} } catch (sqlexception e) { }finally{ if (rs != null) { try { rs.close(); } catch (sqlexception sqlex) { } rs = null; } if (ps != null) { try { ps.close(); } catch (sqlexception sqlex) { } ps = null; } } }
when garbage collector activated? when timer finishes? or gc called though timer still running?
garbage collections happen @ time, feels like. generally, when application running low on memory. gc may run when timer still running putting application on pause (with parallel/throughput collector).
so have no control on when , gc kick in. may happen @ time, regardless of doing in code.
Comments
Post a Comment