this question has answer here:
- how find java memory leak 9 answers
i'm trying write program stringified json object steam , uses object determine if can buy item on steam market or not.
it works, seem getting massive memory leaks , have no idea how solve problem i'm beginner programmer. here code:
import java.io.ioexception; import java.net.malformedurlexception; import java.net.url; import java.net.urlconnection; import java.util.scanner; public class steammarketalert { @suppresswarnings("unused") private jalertwindow alert; private url jsonurl; private float walletvalue; private boolean itembuyable; public steammarketalert(url itemurl, float walletvalue) { this.itembuyable = false; this.jsonurl = getjsonurl(itemurl); this.walletvalue = walletvalue; } private url getjsonurl(url itemurl) { string jsonstring = itemurl.tostring(); string firstpart = jsonstring.substring(0, jsonstring.indexof("market/") + "market/".length()); string appid = jsonstring.split("/")[5]; string markethashname = jsonstring.split("/")[6]; string secondpart = "priceoverview/?currency=2&appid=" + appid + "&market_hash_name=" + markethashname; try { return new url(firstpart + secondpart); } catch (malformedurlexception e) { system.err.println("failed create json url"); return null; } } public void checkmarket() { thread thread = new thread(){ @override public void run(){ try { while(!itembuyable) { sleep(5000); if(isbuyable(getpagehtml())) itembuyable = true; } alert = new jalertwindow(); } catch (interruptedexception e) { e.printstacktrace(); } } }; thread.start(); } private boolean isbuyable(string pagehtml) { int firstindex = pagehtml.indexof(";") +1; float marketvalue = float.parsefloat(pagehtml.substring(firstindex, firstindex + pagehtml.substring(firstindex, pagehtml.length()).indexof("\""))); return (marketvalue <= walletvalue)? true:false; } private string getpagehtml(){ try(scanner scanner = new scanner(jsonurl.openconnection().getinputstream())) { scanner.usedelimiter("\\z"); return scanner.next(); } catch (ioexception e) { e.printstacktrace(); return null; } } public static void main(string[] args) { try { float walletvalue = 82.64f; url itemurl = new url("http://steamcommunity.com/market/listings/730/stattrak%e2%84%a2%20p90%20%7c%20asiimov%20%28factory%20new%29"); steammarketalert sma = new steammarketalert(itemurl,walletvalue); sma.checkmarket(); } catch (malformedurlexception e) { e.printstacktrace(); } } } i've narrowed problem down checkmarket() method. however, can't seem figure out going on. can please point out how can fix (and possibly point out flaws in code), note jalertwindow object displays jframe "can buy" on - nothing special.
edit: updated code since posting , users informed me try-with-resource blocks existed. thank has helped me understand how java garbage collection works. :)!
it's provbably not memory leak in java until outofmemoryexception or see constant garbage collection.
using 18mb in 10 minutes doesn't seem memory leak, that's how java works. if want make sure, can turn on verbose gc , see how it's collecting, don't think have real issue yet.
Comments
Post a Comment