Squiz Matrix  4.12.2
 All Data Structures Namespaces Functions Variables Pages
MemoryTest.java
1 package ij.plugin;
2 
3 import java.awt.*;
4 import java.awt.image.*;
5 import java.util.Vector;
6 import ij.*;
7 import ij.gui.*;
8 
10 public class MemoryTest implements PlugIn {
11 
12 
13  public void run(String arg) {
14 
15  boolean okay = IJ.showMessageWithCancel("Memory Test",
16  "The MemoryTest plugin opens as many\n"
17  + "1024x1024x8-bit images as possible and then\n"
18  + "calculates how much memory was used for each\n"
19  + "image. This is a demanding test that may require\n"
20  + "ImageJ to be restarted, may crash your browser, \n"
21  + "or may even crash your machine."
22  );
23  if (!okay)
24  return;
25 
26  int width = 1024;
27  int arraySize = 1024*1024;
28  Vector objects;
29  int nArrays = 0;
30  int nImages = 0;
31  byte[] pixels;
32  ColorModel cm;
33  Image img, saveImg=null;
34 
35  IJ.write("");
36  IJ.write("Opening " + width + "x" + width + " 8-bit image windows...");
37  collectGarbage();
38  int[] times = new int[2000];
39  long time, time2;
40  time = System.currentTimeMillis();
41  try {
42  while(true) {
43  NewImage.open(""+(nImages+1), width, width, 1, NewImage.GRAY8, NewImage.FILL_WHITE);
44  time2 = System.currentTimeMillis();
45  times[nImages] = (int)(time2-time);
46  time = time2;
47  nImages++;
48  }
49  }
50  catch(OutOfMemoryError ex) {
51  collectGarbage();
52  }
53 
54  IJ.wait(100);
55  objects = new Vector();
56  IJ.write("Probing memory...");
57  try {
58  while(true) {
59  System.gc();
60  byte[] a = new byte[arraySize];
61  objects.addElement(a);
62  nArrays++;
63  IJ.showStatus((nArrays*arraySize)/(1024*1024) + "MB");
64  IJ.wait(50);
65  }
66  }
67  catch(OutOfMemoryError e) {
68  objects = null;
69  collectGarbage();
70  IJ.write(arraySize*nArrays/(1024*1024) + "MB is available");
71  IJ.write(nImages + " images were opened");
72  if (nImages>0)
73  IJ.write("~" + arraySize*nArrays/(nImages*1024) + "K was required for each image");
74  IJ.write("");
75  //for (int i=0; i<nImages; i++)
76  // IJ.write((i+1)+": "+times[i]);
77  IJ.showStatus("");
78  }
79 
80  }
81 
82  void collectGarbage() {
83  for (int i=0; i<10; i++)
84  System.gc();
85  }
86 
87 }