Squiz Matrix  4.12.2
 All Data Structures Namespaces Functions Variables Pages
Memory.java
1 package ij.plugin;
2 import ij.*;
3 //import ij.process.*;
4 import ij.gui.*;
5 //import java.awt.*;
6 import java.io.*;
7 import ij.util.Tools;
8 import java.lang.reflect.*;
9 
10 
12 public class Memory implements PlugIn {
13  String s;
14  int index1, index2;
15  File f;
16 
17  public void run(String arg) {
18  changeMemoryAllocation();
19  //IJ.log("setting="+getMemorySetting()/(1024*1024)+"MB");
20  //IJ.log("maxMemory="+maxMemory()/(1024*1024)+"MB");
21  }
22 
23  void changeMemoryAllocation() {
24  IJ.maxMemory(); // forces IJ to cache old limit
25  int max = (int)(getMemorySetting()/1048576L);
26  if (max==0)
27  {showError(); return;}
28  GenericDialog gd = new GenericDialog("Memory");
29  gd.addNumericField("Maximum Memory: ", max, 0, 4, "MB");
30  gd.showDialog();
31  if (gd.wasCanceled()) return;
32  int max2 = (int)gd.getNextNumber();
33  if (gd.invalidNumber()) {
34  IJ.showMessage("Memory", "The number entered was invalid.");
35  return;
36  }
37  if (max2<32 && IJ.isMacOSX()) max2 = 32;
38  if (max2<8 && IJ.isWindows()) max2 = 8;
39  if (max2==max) return;
40  if (max2>=1700) {
41  if (!IJ.showMessageWithCancel("Memory",
42  "Note: setting the memory limit to a value greater\n"
43  +"than 1700MB may cause ImageJ to fail to start."))
44  return;
45  }
46  try {
47  String s2 = s.substring(0, index1) + max2 + s.substring(index2);
48  FileOutputStream fos = new FileOutputStream(f);
49  PrintWriter pw = new PrintWriter(fos);
50  pw.print(s2);
51  pw.close();
52  } catch (IOException e) {
53  String error = e.getMessage();
54  if (error==null || error.equals("")) error = ""+e;
55  String name = IJ.isMacOSX()?"Info.plist":"ImageJ.cfg";
56  String msg =
57  "Unable to update the file \"" + name + "\".\n"
58  + " \n"
59  + "\"" + error + "\"\n";
60  IJ.showMessage("Memory", msg);
61  return;
62  }
63  IJ.showMessage("Memory", "The new " + max2 +"MB limit will take effect after ImageJ is restarted.");
64  }
65 
66  public long getMemorySetting() {
67  long max = 0L;
68  if (IJ.isMacOSX()) {
69  max = getMemorySetting("ImageJ.app/Contents/Info.plist");
70  } else
71  max = getMemorySetting("ImageJ.cfg");
72  return max;
73  }
74 
75  void showError() {
76  int max = (int)(maxMemory()/1048576L);
77  String msg =
78  "ImageJ is unable to change the memory limit. For\n"
79  + "more information, refer to the installation notes.\n"
80  + " \n";
81  if (max>0)
82  msg += "Current limit: " + max + "MB";
83  IJ.showMessage("Memory", msg);
84  }
85 
86  long getMemorySetting(String file) {
87  String path = System.getProperty("user.dir")+File.separator+file;
88  f = new File(path);
89  if (!f.exists()) return 0L;
90  long max = 0L;
91  try {
92  int size = (int)f.length();
93  byte[] buffer = new byte[size];
94  FileInputStream in = new FileInputStream(f);
95  in.read(buffer, 0, size);
96  s = new String(buffer, 0, size, "ISO8859_1");
97  in.close();
98  index1 = s.indexOf("-mx");
99  if (index1==-1) index1 = s.indexOf("-Xmx");
100  if (index1==-1) return 0L;
101  if (s.charAt(index1+1)=='X') index1+=4; else index1+=3;
102  index2 = index1;
103  while (index2<s.length()-1 && Character.isDigit(s.charAt(++index2))) {}
104  String s2 = s.substring(index1, index2);
105  max = (long)Tools.parseDouble(s2, 0.0)*1024*1024;
106  }
107  catch (Exception e) {
108  IJ.log(""+e);
109  return 0L;
110  }
111  return max;
112  }
113 
116  public long maxMemory() {
117  // Call maxMemory using reflection so this class can be compiled with Java 1.3
118  long max = 0L;
119  try {
120  Runtime rt = Runtime.getRuntime();
121  Class c = rt.getClass();
122  Method maxMemory = c.getDeclaredMethod("maxMemory", new Class[0]);
123  Long l = (Long)maxMemory.invoke(rt, new Object[] {});
124  max = l.longValue();
125  } catch (Exception e) {}
126  return max;
127  }
128 
129 }