8 import java.lang.reflect.*;
17 public void run(String arg) {
18 changeMemoryAllocation();
23 void changeMemoryAllocation() {
25 int max = (int)(getMemorySetting()/1048576L);
27 {showError();
return;}
28 GenericDialog gd =
new GenericDialog(
"Memory");
29 gd.addNumericField(
"Maximum Memory: ", max, 0, 4,
"MB");
31 if (gd.wasCanceled())
return;
32 int max2 = (int)gd.getNextNumber();
33 if (gd.invalidNumber()) {
37 if (max2<32 && IJ.isMacOSX()) max2 = 32;
38 if (max2<8 && IJ.isWindows()) max2 = 8;
39 if (max2==max)
return;
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."))
47 String s2 = s.substring(0, index1) + max2 + s.substring(index2);
48 FileOutputStream fos =
new FileOutputStream(f);
49 PrintWriter pw =
new PrintWriter(fos);
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";
57 "Unable to update the file \"" + name +
"\".\n"
59 +
"\"" + error +
"\"\n";
60 IJ.showMessage(
"Memory", msg);
63 IJ.showMessage(
"Memory",
"The new " + max2 +
"MB limit will take effect after ImageJ is restarted.");
66 public long getMemorySetting() {
69 max = getMemorySetting(
"ImageJ.app/Contents/Info.plist");
71 max = getMemorySetting(
"ImageJ.cfg");
78 "ImageJ is unable to change the memory limit. For\n"
79 +
"more information, refer to the installation notes.\n"
82 msg +=
"Current limit: " + max +
"MB";
83 IJ.showMessage(
"Memory", msg);
86 long getMemorySetting(String file) {
87 String path = System.getProperty(
"user.dir")+File.separator+file;
89 if (!f.exists())
return 0L;
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");
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;
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;
107 catch (Exception e) {
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[] {});
125 }
catch (Exception e) {}