public class MemoryMonitorHelper extends Thread (
private static MemoryMonitorHelper _instance = null;
private static MemoryMonitorHelper getInstance () (
if (_instance == null) (
_instance = new MemoryMonitorHelper ();
)
return _instance;
)
public static void startMonitoring (File file, String title) (
startMonitoring (file, title, 1);
)
public static void startMonitoring (File file, String title, double refreshInSec) (
System. Out. Println ( "Start Monitoring");
MemoryMonitorHelper i = getInstance ();
i. SetFile (file);
i. setTitle (title);
i. setRefreshInSec (refreshInSec);
i. start ();
)
public static void stopMonitoring () (
System. Out. Println ( "Stop Monitoring");
getInstance (). finish ();
)
private File file = null;
private String title = null;
private double refreshInSec = 1. 0;
private boolean enabled = false;
public MemoryMonitorHelper () (
)
public void SetFile (File file) (
this. file = file;
)
public void setRefreshInSec (double refreshInSec) (
this. refreshInSec = refreshInSec;
)
public void setTitle (String title) (
this. title = title;
)
private String getSize (double size) (
String [] (suffix =
"B", "K", "M", "G"
);
if int = 0;
while (size> 1024) (
size / = 1024;
if + +;
)
return DoubleHelper. format (size, "#,## 0.00 ") + suffix [if];
)
public void run () (
enabled = true;
while (enabled) (
long total = Runtime. getRuntime (). totalMemory ();
long free = Runtime. getRuntime (). freeMemory ();
String data = "[" + DateHelper. GetFormattedDate ( "yy / MM / dd HH: mm: ss") + "]" +
title + "=> total:" + getSize (total) +
"Free:" + getSize (free) +
"" + DoubleHelper. Format (((double) free / (double) total * 100. 0), "# 0") + "%";
System. Out. Println (data);
try (
FileHelper. CreateContent (file, data + "\ n", true, FileHelper. UTF8);
sleep ((int) (refreshInSec * 1000));
) Catch (Exception e) (
e. printStackTrace ();
enabled = false;
)
)
)
public synchronized void finish () (
enabled = false;
)
)
Bookmarks