2015年6月10日 星期三

Processing: Get Free Memory Info With AppleScript

since: 2015/06/10
update: 2015/06/10

reference:
1. Troubleshooting · processing/processing Wiki

2. Applescript show free memory | Apple Support Communities
3. AppleScript: Essential Sub-Routines
4. io - How do I write to a text file using AppleScript? - Stack Overflow

A. 撰寫 Applescript:
    1. 開啟 Automator:

   
     2. Automator > 檔案 > 新增 > 應用程式 > 選擇:
    
     3. 工具程式 > 執行 AppleScript > 拖拉到右方

    4. 預設程式碼:
on run {input, parameters}
   
    (* Your script goes here *)
   
    return input
end run




    5. 修改如下:
on run {input, parameters}
   
   
    # get the number of free pages using 'vm_stat' and multiply by the page size
    set vmStats to (text 12 thru -2 of (do shell script "vm_stat | grep 'Pages free'")) * 4096
    # display dialog vmStats with title "Memory usage" buttons {"OK"} default button 1
   
    # write to file
    set someText to number_to_string(vmStats)
   
    set textFile to "/Lanli/RD/Projects/Processing/memory_test/free_mem.txt"
    do shell script "echo  " & quoted form of someText & " >  " & quoted form of textFile

   
   
    return input
end run


# A sub-routine that converts numeric values to strings
on number_to_string(this_number)
    set this_number to this_number as string
    if this_number contains "E+" then
        set x to the offset of "." in this_number
        set y to the offset of "+" in this_number
        set z to the offset of "E" in this_number
        set the decimal_adjust to characters (y - (length of this_number)) thru ¬
            -1 of this_number as string as number
        if x is not 0 then
            set the first_part to characters 1 thru (x - 1) of this_number as string
        else
            set the first_part to ""
        end if
        set the second_part to characters (x + 1) thru (z - 1) of this_number as string
        set the converted_number to the first_part
        repeat with i from 1 to the decimal_adjust
            try
                set the converted_number to ¬
                    the converted_number & character i of the second_part
            on error
                set the converted_number to the converted_number & "0"
            end try
        end repeat
        return the converted_number
    else
        return this_number
    end if
end number_to_string


    6. 檔案 > 儲存: 
        /Lanli/RD/Projects/Processing/memory_test/free_mem.app

-----------------------------------------------------------------------------------------------

B. 開啟 Processing 撰寫程式如下:
import java.util.Date;

String words1;
String words2;
String words3;
String words4;

long allocated;
long free;
long maximum;
long checkTimer;

String[] config;
float free_mem;

void setup() {
  frameRate(15);
  size(400, 400);
  textSize(24);
 
  words1 = "";
  words2 = "";
  words3 = "";
  words4 = "";
 
  checkTimer = (new Date()).getTime();

  //  File[] roots = File.listRoots();
  //
  //  // For each filesystem root, print some info 
  //  for (File root : roots) {
  //    println("File system root: " + root.getAbsolutePath());
  //    println("Total space (bytes): " + root.getTotalSpace());
  //    println("Free space (bytes): " + root.getFreeSpace());
  //    println("Usable space (bytes): " + root.getUsableSpace());
  //  }

}

void draw() {
  background(0);

  if ((new Date()).getTime() - checkTimer >= 2000) {
    checkTimer = (new Date()).getTime();

    open("/Lanli/RD/Projects/Processing/memory_test/free_mem.app");
    //delay(250);
    config = loadStrings("free_mem.txt"); 
    free_mem = float(config[0]) / 1024 / 1024; 

    // The amount of memory allocated so far (usually the -Xms setting)
    allocated = Runtime.getRuntime().totalMemory() / 1024 /1024;

    // Free memory out of the amount allocated (value above minus used)
    free = Runtime.getRuntime().freeMemory() / 1024 /1024;

    // The maximum amount of memory that can eventually be consumed
    // by this application. This is the value set by the Preferences
    // dialog box to increase the memory settings for an application.

    maximum = Runtime.getRuntime().maxMemory() / 1024 / 1024; 

    words1 = "app allocated = "+ allocated + " MB";
    words2 = "app free = "+ free + " MB";
    words3 = "app maximum = "+ maximum + " MB";
    words4 = "sys free_mem = "+ free_mem + " MB";
  }

  text(words1, 10, 25, 540, 300);
  text(words2, 10, 125, 540, 300);
  text(words3, 10, 225, 540, 300);
  text(words4, 10, 325, 540, 300);

  System.gc();
}

-----------------------------------------------------------------------------------------------

C. 執行結果:
       備註: 實體記憶體 (8.00 GB) - 記憶體用量 (2.45 GB) - 快取(2.59 GB)
                 = 2.96 GB 接近 3015.1016 MB / 1024 = 2.944 GB

沒有留言:

張貼留言

注意:只有此網誌的成員可以留言。