JavaFx lineChart real-time Monitor   about memory

 public class EffectTest extends Application {

 StackPane root;
private static int MAX_DATA_POINTS = ;
private static int Y_DATA_RANGE = ;
private static int TICK_UNIT = ; private static int UPDATE_INTERVAL_MS = ;
private LineChart.Series<Number, Number> series1;
private LineChart<Number, Number> lineChart;
private NumberAxis xAxis = new NumberAxis();
private NumberAxis yAxis = new NumberAxis(); private SequentialTransition animation;
private Paint paintXticklabel;
private double nextX = ; Random rnd = new Random();
double currentMemBAK=;
public EffectTest() { Timeline timeline = new Timeline();
timeline.getKeyFrames().add(new KeyFrame(Duration.millis(UPDATE_INTERVAL_MS*), new EventHandler<ActionEvent>() {
@Override
public void handle(ActionEvent actionEvent) { // update chart data
// note that we add one data point and remove one data point in this simple example.
// in a production environment you'd have to add multiple and remove multiple data points double currentMem= Runtime.getRuntime().totalMemory()-Runtime.getRuntime().freeMemory();
double drawy =(currentMem-currentMemBAK)/;
currentMemBAK=currentMem;
System.out.println("currentMem:"+currentMem);
// add new points
//series1.getData().add(new XYChart.Data<Number, Number>(nextX, Math.cos(Math.toRadians(nextX)) * Y_DATA_RANGE)); //mem
series1.getData().add(new XYChart.Data<Number, Number>(nextX, drawy+)); //cpu
//series1.getData().add(new XYChart.Data<Number, Number>(nextX, getCpuRatioForWindows())); // remove points that shouldn't be visible anymore
if (series1.getData().size() > MAX_DATA_POINTS) {
series1.getData().remove(); }
System.out.println("node size:"+series1.getData().size()); nextX += ; // update using series 1 as reference
// series 2 contains same amount of data; if it doesn't for your case,
// you need to adapt this here and calculate the proper range
List<Data<Number, Number>> data = series1.getData();
xAxis.setLowerBound(data.get().getXValue().doubleValue());
xAxis.setUpperBound(data.get(data.size() - ).getXValue().doubleValue()); ////////
lineChart.getXAxis().setTickLabelFill(paintXticklabel); // xAxis.setTickUnit(1);
root.setTranslateX(-); }
}));
timeline.setCycleCount(Animation.INDEFINITE); animation = new SequentialTransition();
animation.getChildren().addAll(timeline);
} public Parent createContent() { xAxis = new NumberAxis();
xAxis.setForceZeroInRange(false);
xAxis.setAutoRanging(false);
xAxis.setTickLabelsVisible(false);
xAxis.setTickMarkVisible(false);
xAxis.setMinorTickVisible(false);
xAxis=new NumberAxis(, Y_DATA_RANGE+, TICK_UNIT/); // set Axis property
// final NumberAxis yAxis = new NumberAxis(1, 21, 0.1);
// xAxis.setTickUnit(1);
// xAxis.setPrefWidth(35);
// xAxis.setMinorTickCount(10);
// xAxis.setSide(Side.RIGHT);
// xAxis.setTickLabelFormatter(new NumberAxis.DefaultFormatter(xAxis) {
// @Override
// public String toString(Number object) {
// String label;
// label = String.format("%7.2f", object.floatValue());
// return label;
// }
// }); //yAxis = new NumberAxis(-Y_DATA_RANGE, Y_DATA_RANGE, TICK_UNIT);
yAxis = new NumberAxis(, Y_DATA_RANGE+, TICK_UNIT/);
yAxis.setAutoRanging(false); lineChart = new LineChart<>(xAxis, yAxis);
lineChart.setAnimated(false);
lineChart.setLegendVisible(false); //set if dont want symbols on the point
lineChart.setCreateSymbols(false);
series1 = new LineChart.Series<>();
// series1.getData().add(new LineChart.Data<Number, Number>(0d, 0d));
lineChart.getData().add(series1); //save ticklabe
paintXticklabel=xAxis.getTickLabelFill();
return lineChart;
} public void play() {
animation.play();
} @Override
public void stop() {
animation.pause();
} @Override
public void start(Stage primaryStage) throws Exception { primaryStage.setTitle("Drawing Operations Test"); root = new StackPane();
root.getChildren().add(createContent()); Scene s= new Scene(root);
primaryStage.setScene(s);
primaryStage.show(); play();
} public static void main(String[] args) {
launch(args);
// getCpuRatioForWindows() ;
System.out.println(getCpuRatioForWindows()); } private static final int CPUTIME = ;
private static final int PERCENT = ; public static double getCpuRatioForWindows()
{
try
{
String procCmd =
System.getenv("windir")
+ "\\system32\\wbem\\wmic.exe process get Caption,CommandLine,KernelModeTime,ReadOperationCount,ThreadCount,UserModeTime,WriteOperationCount"; long[] c0 = readCpu(Runtime.getRuntime().exec(procCmd));
Thread.sleep(CPUTIME);
long[] c1 = readCpu(Runtime.getRuntime().exec(procCmd));
if (c0 != null && c1 != null)
{
long idletime = c1[] - c0[];
long busytime = c1[] - c0[];
return Double.valueOf(PERCENT * (busytime) * 1.0 / (busytime + idletime)).intValue() ;
}
else
{
return ;
}
}
catch (Exception ex)
{
ex.printStackTrace();
return ;
}
}
private static final int FAULTLENGTH = ;
private static long[] readCpu(final Process proc)
{
long[] retn = new long[];
try
{
proc.getOutputStream().close();
InputStreamReader ir = new InputStreamReader(proc.getInputStream());
LineNumberReader input = new LineNumberReader(ir);
String line = input.readLine();
if (line == null || line.length() < FAULTLENGTH)
{
return null;
}
int capidx = line.indexOf("Caption");
int cmdidx = line.indexOf("CommandLine");
int rocidx = line.indexOf("ReadOperationCount");
int umtidx = line.indexOf("UserModeTime");
int kmtidx = line.indexOf("KernelModeTime");
int wocidx = line.indexOf("WriteOperationCount");
long idletime = ;
long kneltime = ;
long usertime = ;
while ((line = input.readLine()) != null)
{
if (line.length() < wocidx)
{
continue;
}
//Caption,CommandLine,KernelModeTime,ReadOperationCount,
// ThreadCount,UserModeTime,WriteOperation
String caption = substring(line, capidx, cmdidx - ).trim();
String cmd = substring(line, cmdidx, kmtidx - ).trim();
if (cmd.indexOf("wmic.exe") >= )
{
continue;
}
String s1 = substring(line, kmtidx, rocidx - ).trim();
String s2 = substring(line, umtidx, wocidx - ).trim();
if (caption.equals("System Idle Process") || caption.equals("System"))
{
if (s1.length() > )
idletime += Long.valueOf(s1).longValue();
if (s2.length() > )
idletime += Long.valueOf(s2).longValue();
continue;
}
if (s1.length() > )
kneltime += Long.valueOf(s1).longValue();
if (s2.length() > )
usertime += Long.valueOf(s2).longValue();
}
retn[] = idletime;
retn[] = kneltime + usertime;
return retn;
}
catch (Exception ex)
{
ex.printStackTrace();
}
finally
{
try
{
proc.getInputStream().close();
}
catch (Exception e)
{
e.printStackTrace();
}
}
return null;
}
private static String substring(String src, int start_idx, int end_idx)
{
byte[] b = src.getBytes();
String tgt = "";
for (int i = start_idx; i <= end_idx; i++)
{
tgt += (char)b[i];
}
return tgt;
}
}

最新文章

  1. win7+IIS7下木有4.0框架问题的解决方案
  2. Chart系列(一):Chart的基本元素
  3. 初识Redis
  4. 现代程序设计——homework-06
  5. springMVC项目在jboss7中配置应用自己的log4j--转载
  6. js 实现 di
  7. poj3694(动态询问割桥的数目)
  8. xdu_1048:二分匹配模板测试
  9. input type date 解决移动端显示placeholder
  10. Problem: Time(一道水却有意思的题
  11. SQL语法基础之INSEART语句
  12. UEditor在asp.netMVC4中的使用,包括上传功能,粘贴表格不显示边框问题
  13. 关于pythoh面向过程开发人员三步转面向对象的补充,再加一步,四步走战略。转面向对象也可以有固定公式。
  14. android 推流解决方案
  15. 本地计算机上的SQLServer(MSSQLSERVER)服务启动后停止,某些服务在未由其他服务或程序使用时将自动停止
  16. 利用VS2017跨平台远程调试aspnetcore应用
  17. iOS - AsyncSocket 的使用
  18. jQuery-编辑选择结果(添加、筛选、过滤或检测)
  19. redis link 链表结构
  20. BZOJ2226 &amp; SPOJ5971:LCMSum——题解

热门文章

  1. static_cast 与 dynamic_cast
  2. URAL 1823. Ideal Gas(数学啊 )
  3. CSS 文本字体颜色设置方法(CSS color)
  4. 6.控制器(ng-Controller)
  5. 关于Javascript的forEach 和 map
  6. Ionic2集成ArcGIS JavaScript API.md
  7. Dom4j 查找节点或属性
  8. C# double保留四位小数
  9. installp 软件的4种状态
  10. mysql安装遇到的坑