i developing swing application in java shows memory usage of each process on pc
the result should appear in jtable
the jtable contains 4 columns :
- 1) process name
- 2) pid
- 3) session name
- 4) memory usage
the jtable appear like

the jtable refresh each period using processbuilder class
the problem felt method little bit slow , boring when user see it
and question:
is there other powerful method ? or can in code enhance application?
in code :
jtable dragged palette (of type javax.swing.jtable) jlabel dragged palette (of type javax.swing.jlabel) the method used display process :
@suppresswarnings("empty-statement") private void getprocesslist() throws interruptedexception { private defaulttablemodel table; string filename = system.getproperty("java.io.tmpdir") + "\\list.txt", h;//to %tmp% scanner sc1 = null; int i, j, k = 0; table = (javax.swing.table.defaulttablemodel) processtable.getmodel(); processtable.editingstopped(null); table.setrowcount(0); try { processbuilder builder = new processbuilder("cmd.exe", "/c", "tasklist > %tmp%\\list.txt"); builder.redirecterrorstream(true); process p = builder.start(); p.waitfor(); try { sc1 = new scanner(new file(filename)); while (sc1.hasnext()) { h = sc1.nextline(); k++; } k = k - 5; } catch (filenotfoundexception ex) { system.out.println("file " + filename + " not found"); }; status.settext(" processes: " + k); object o[] = new object[4]; = 0; try { j = 0; sc1 = new scanner(new file(filename)); while (sc1.hasnext()) { st = new stringtokenizer(sc1.nextline(), "\n"); i++; if (i > 5) { while (st.hasmoretokens()) { st1 = new stringtokenizer(st.nexttoken(), " "); if (st1.hasmoretokens()) { o[0] = st1.nexttoken(); } if (st1.hasmoretokens()) { h = st1.nexttoken(); if (h.contains(".exe")) { o[0] = o[0] + " " + h; o[1] = st1.nexttoken(); } else { o[1] = h; } } if (st1.hasmoretokens()) { o[2] = st1.nexttoken(); } if (st1.hasmoretokens()) { h = st1.nexttoken(); } if (st1.hasmoretokens()) { o[3] = st1.nexttoken(); } if (st1.hasmoretokens()) { h = st1.nexttoken(); } table.addrow(o); j++; } } else { if (st.hasmoretokens()) { h = st.nexttoken(); } } } } catch (filenotfoundexception ex) { system.out.println("file " + filename + " not found"); } catch (exception ex) { system.out.println("unknownerror: " + ex.getmessage()); } { if (sc1 != null) { sc1.close(); } } } catch (ioexception ex) { logger.getlogger(processman.class.getname()).log(level.severe, null, ex); } }
you can speed things without many changes by:
- using separate thread poll running processes (instead of using edt); , writing changes jtable's tablemodel.
- avoiding intermediate file when calling tasklist, , instead reading output string, parse.
otherwise, go native: see https://stackoverflow.com/a/13478716/15472 example.
Comments
Post a Comment