Squiz Matrix  4.12.2
 All Data Structures Namespaces Functions Variables Pages
MemoryMonitor.java
1 package ij.plugin;
2 import ij.*;
3 import ij.gui.*;
4 import ij.process.*;
5 import java.awt.*;
6 import java.awt.image.*;
7 import java.awt.event.*;
8 
17 public class MemoryMonitor implements PlugIn {
18  int width = 200;
19  int height = 75;
20  long fps, startTime, elapsedTime;
21  ImageProcessor ip;
22  int frames;
23  ImageCanvas ic;
24  float[] mem;
25  int index;
26  long value;
27  int max = 12*1204*1024; // 12MB
28  long maxMemory = IJ.maxMemory();
29 
30  public void run(String arg) {
31  if (IJ.altKeyDown()) {
32  // simulate frame grabber
33  width = 640;
34  height = 480;
35  }
36  ip = new ByteProcessor(width, height, new byte[width*height], null);
37  ip.setColor(Color.white);
38  ip.fill();
39  ip.setColor(Color.black);
40  ip.setFont(new Font("SansSerif",Font.PLAIN,12));
41  ip.setAntialiasedText(true);
42  ip.snapshot();
43  ImagePlus imp = new ImagePlus("Memory", ip);
44  imp.show();
45  imp.lock();
46  ic = imp.getCanvas();
47  mem = new float[width+1];
48  Thread.currentThread().setPriority(Thread.MIN_PRIORITY);
49  startTime = System.currentTimeMillis();
50  imp.unlock();
51  }
52 
53  void showValue() {
54  double value2 = (double)value/1048576L;
55  String s = IJ.d2s(value2,value2>50?0:2)+"MB";
56  if (maxMemory>0L) {
57  double percent = value*100/maxMemory;
58  s += " ("+(percent<1.0?"<1":IJ.d2s(percent,0)) + "%)";
59  }
60  if (width==640) {
61  elapsedTime = System.currentTimeMillis()-startTime;
62  if (elapsedTime>0) {
63  double scale = ic.getMagnification();
64  fps = (frames*10000)/elapsedTime;
65  s += ", " + fps/10 + "." + fps%10 + " fps";
66  }
67  }
68  ip.moveTo(2, 15);
69  ip.drawString(s);
70  }
71 
72  long memoryInUse() {
73  long freeMem = Runtime.getRuntime().freeMemory();
74  long totMem = Runtime.getRuntime().totalMemory();
75  return totMem-freeMem;
76  }
77 
78  void updatePixels() {
79  long used = memoryInUse();
80  if (frames%10==0) value=used;
81  if (used>0.9*max) max*=2;
82  mem[index++] = (float)used;
83  if (index==mem.length) index = 0;
84  ip.reset();
85  int index2 = index+1;
86  if (index2==mem.length) index2 = 0;
87  double scale = (double)height/max;
88  ip.moveTo(0, height-(int)(mem[index2]*scale));
89  for (int x=1; x<width; x++) {
90  index2++;
91  if (index2==mem.length) index2 = 0;
92  ip.lineTo(x, height-(int)(mem[index2]*scale));
93  }
94  }
95 
96 }