卸了360以後,與之捆綁的加速球也沒了。加速球能夠查看剩餘內存量,清理殘留進程,有的時候不以爲這小玩意有多大做用,卸了以後才知道後悔。java
加速球的替代方案比比皆是,如Windows自帶的任務管理器,窗口底部能看到內存使用率,手動殺死進程能釋放資源。筆者是個追求視覺效果的人,就以爲任務管理器的顯示效果太直白,缺少美感,因而萌生了本身動手寫個「內存監視器」的想法。git
內存監視器,顧名思義,就是監視內存的使用狀況,涉及到讀取內存信息和顯示彙總結果兩方面內容。讀取內存信息能夠藉助SIGAR庫,顯示彙總結果有JFreeChart。github
SIGAR,即System Information Gatherer And Reporter,跨平臺,支持多種語言,提供輕便的接口來收集CPU,內存,交換區,帳戶,文件系統,網絡等信息。網絡
JFreeChart用來建立高質量圖表,支持2D或3D的扇形圖,柱形圖,折線圖等圖表類型,還能以PNG,JPEG,PDF,SVG的格式導出圖表。ide
由於能夠在官方站點找到幫助文檔,因此此處再也不冗述其使用方法。ui
內存監視器的設計思想是每秒收集一次內存使用率,彙總起來繪製折線圖,詳細的源碼以下:spa
1 package org.warnier.zhang.systemmonitor; 2 3 import org.jfree.chart.ChartFactory; 4 import org.jfree.chart.ChartPanel; 5 import org.jfree.chart.JFreeChart; 6 import org.jfree.chart.StandardChartTheme; 7 import org.jfree.data.category.DefaultCategoryDataset; 8 import org.jfree.ui.ApplicationFrame; 9 import org.warnier.zhang.systemmonitor.util.SystemMonitor; 10 11 import javax.swing.*; 12 import java.awt.*; 13 import java.text.SimpleDateFormat; 14 import java.util.*; 15 import java.util.List; 16 import java.util.Timer; 17 18 /** 19 * Startpoint for whole project. 20 */ 21 public class Console extends ApplicationFrame { 22 private static final long delayMillis = 1000; 23 private List<Map<String, Double>> dataSet = new ArrayList<>(8); 24 25 public Console(String title) { 26 super(title); 27 ChartFactory.setChartTheme(getTheme()); 28 29 //Gather system info. 30 gatherSystemInfo(); 31 } 32 33 //Enable zh_CN. 34 private StandardChartTheme getTheme() { 35 StandardChartTheme theme = new StandardChartTheme("CN"); 36 Font font = new Font("宋體", Font.PLAIN, 12); 37 theme.setExtraLargeFont(new Font("宋體", Font.BOLD, 18)); 38 theme.setRegularFont(font); 39 theme.setLargeFont(font); 40 return theme; 41 } 42 43 private void gatherSystemInfo() { 44 final SystemMonitor monitor = new SystemMonitor(); 45 new Timer().schedule(new TimerTask() { 46 @Override 47 public void run() { 48 Map<String, Double> dataItem = new HashMap<>(1); 49 dataItem.put(SimpleDateFormat.getTimeInstance().format(new Date()), monitor.getMemory()); 50 dataSet.add(dataItem); 51 //Retain 8 latest items. 52 if (dataSet.size() > 8) { 53 dataSet.remove(0); 54 } 55 invalidate(getChart()); 56 } 57 }, 0, delayMillis); 58 } 59 60 private void invalidate(JFreeChart chart) { 61 ChartPanel chartPanel = new ChartPanel(chart); 62 // ? Can't use zh_CN. 63 //chartPanel.setFont(new Font("MS Song", Font.PLAIN, 12)); 64 setContentPane(chartPanel); 65 ((JPanel)getContentPane()).updateUI(); 66 } 67 68 private JFreeChart getChart() { 69 JFreeChart chart = ChartFactory.createLineChart("內存監視器", 70 null, 71 null, 72 wrapDataSet(dataSet)); 73 return chart; 74 } 75 76 private DefaultCategoryDataset wrapDataSet(List<Map<String, Double>> rawData) { 77 DefaultCategoryDataset dataSet = new DefaultCategoryDataset(); 78 for (int i = 0; i < rawData.size(); i++) { 79 for (Map.Entry<String, Double> entry : rawData.get(i).entrySet()) { 80 dataSet.addValue(entry.getValue(), "內存佔用率", entry.getKey()); 81 } 82 } 83 return dataSet; 84 } 85 86 public static void main(String[] args) { 87 Console console = new Console("System Monitor"); 88 console.setSize(680, 420); 89 console.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); 90 console.setVisible(true); 91 } 92 }
效果截圖設計
github連接:https://github.com/Warnier-zhang/SystemMonitor.git。code