‘获取当前的程序内存占用(生成exe后运行):
Private Sub Timer1_Timer()
Dim myUsage As Double
myUsage = RamUsage
Label1Caption = FormatUsage(myUsage) & "K (" & FormatUsage(myUsage / 1024) & " Mb)"
End Sub
Private Function RamUsage(Optional strProcess As String = "") As Double
If strProcess = "" Then strProcess = UCase(AppEXEName) & "EXE" 'Will count the current application as the process if no arguments given
Set objWMIService = GetObject("winmgmts:" & "{impersonationLevel=impersonate}!\\\root\cimv2")
Set colProcessList = objWMIServiceExecQuery("SELECT FROM Win32_Process WHERE Name='" & strProcess & "'")
For Each objProcess In colProcessList
RamUsage = objProcessworkingSetSize / 1024
Next
End Function
Private Function FormatUsage(tUsage As Double)
If Int(tUsage) = tUsage Then
If tUsage = 0 Then
FormatUsage = 0
Else
FormatUsage = Format(tUsage, "###,###")
End If
Else
FormatUsage = Format(tUsage, "###,####")
End If
End Function
首先
创建一个Bean用来存贮要得到的信
public class MonitorInfoBean {
/ 可使用内存 /
private long totalMemory;
/ 剩余内存 /
private long freeMemory;
/ 最大可使用内存 /
private long maxMemory;
/ *** 作系统 /
private String osName;
/ 总的物理内存 /
private long totalMemorySize;
/ 剩余的物理内存 /
private long freePhysicalMemorySize;
/ 已使用的物理内存 /
private long usedMemory;
/ 线程总数 /
private int totalThread;
/ cpu使用率 /
private double cpuRatio;
public long getFreeMemory() {
return freeMemory;
}
public void setFreeMemory(long freeMemory) {
thisfreeMemory = freeMemory;
}
public long getFreePhysicalMemorySize() {
return freePhysicalMemorySize;
}
public void setFreePhysicalMemorySize(long freePhysicalMemorySize) {
thisfreePhysicalMemorySize = freePhysicalMemorySize;
}
public long getMaxMemory() {
return maxMemory;
}
public void setMaxMemory(long maxMemory) {
thismaxMemory = maxMemory;
}
public String getOsName() {
return osName;
}
public void setOsName(String osName) {
thisosName = osName;
}
public long getTotalMemory() {
return totalMemory;
}
public void setTotalMemory(long totalMemory) {
thistotalMemory = totalMemory;
}
public long getTotalMemorySize() {
return totalMemorySize;
}
public void setTotalMemorySize(long totalMemorySize) {
thistotalMemorySize = totalMemorySize;
}
public int getTotalThread() {
return totalThread;
}
public void setTotalThread(int totalThread) {
thistotalThread = totalThread;
}
public long getUsedMemory() {
return usedMemory;
}
public void setUsedMemory(long usedMemory) {
thisusedMemory = usedMemory;
}
public double getCpuRatio() {
return cpuRatio;
}
public void setCpuRatio(double cpuRatio) {
thiscpuRatio = cpuRatio;
}
}
之后,建立bean的接口
public interface IMonitorService {
public MonitorInfoBean getMonitorInfoBean() throws Exception;
}
然后,就是最关键的,得到cpu的利用率,已用内存,可用内存,最大内存等信息。
import javaioInputStreamReader;
import javaioLineNumberReader;
import sunmanagementManagementFactory;
import comsunmanagementOperatingSystemMXBean;
import javaio;
import javautilStringTokenizer;
/
获取系统信息的业务逻辑实现类
@author GuoHuang
/
public class MonitorServiceImpl implements IMonitorService {
private static final int CPUTIME = 30;
private static final int PERCENT = 100;
private static final int FAULTLENGTH = 10;
private static final File versionFile = new File("/proc/version");
private static String linuxVersion = null;
/
获得当前的监控对象
@return 返回构造好的监控对象
@throws Exception
@author GuoHuang
/
public MonitorInfoBean getMonitorInfoBean() throws Exception {
int kb = 1024;
// 可使用内存
long totalMemory = RuntimegetRuntime()totalMemory() / kb;
// 剩余内存
long freeMemory = RuntimegetRuntime()freeMemory() / kb;
// 最大可使用内存
long maxMemory = RuntimegetRuntime()maxMemory() / kb;
OperatingSystemMXBean osmxb = (OperatingSystemMXBean) ManagementFactory
getOperatingSystemMXBean();
// *** 作系统
String osName = SystemgetProperty("osname");
// 总的物理内存
long totalMemorySize = osmxbgetTotalPhysicalMemorySize() / kb;
// 剩余的物理内存
long freePhysicalMemorySize = osmxbgetFreePhysicalMemorySize() / kb;
// 已使用的物理内存
long usedMemory = (osmxbgetTotalPhysicalMemorySize() - osmxb
getFreePhysicalMemorySize())
/ kb;
// 获得线程总数
ThreadGroup parentThread;
for (parentThread = ThreadcurrentThread()getThreadGroup(); parentThread
getParent() != null; parentThread = parentThreadgetParent())
;
int totalThread = parentThreadactiveCount();
double cpuRatio = 0;
if (osNametoLowerCase()startsWith("windows")) {
cpuRatio = thisgetCpuRatioForWindows();
}
else {
cpuRatio = thisgetCpuRateForLinux();
}
// 构造返回对象
MonitorInfoBean infoBean = new MonitorInfoBean();
infoBeansetFreeMemory(freeMemory);
infoBeansetFreePhysicalMemorySize(freePhysicalMemorySize);
infoBeansetMaxMemory(maxMemory);
infoBeansetOsName(osName);
infoBeansetTotalMemory(totalMemory);
infoBeansetTotalMemorySize(totalMemorySize);
infoBeansetTotalThread(totalThread);
infoBeansetUsedMemory(usedMemory);
infoBeansetCpuRatio(cpuRatio);
return infoBean;
}
private static double getCpuRateForLinux(){
InputStream is = null;
InputStreamReader isr = null;
BufferedReader brStat = null;
StringTokenizer tokenStat = null;
try{
Systemoutprintln("Get usage rate of CUP , linux version: "+linuxVersion);
Process process = RuntimegetRuntime()exec("top -b -n 1");
is = processgetInputStream();
isr = new InputStreamReader(is);
brStat = new BufferedReader(isr);
if(linuxVersionequals("24")){
brStatreadLine();
brStatreadLine();
brStatreadLine();
brStatreadLine();
tokenStat = new StringTokenizer(brStatreadLine());
tokenStatnextToken();
tokenStatnextToken();
String user = tokenStatnextToken();
tokenStatnextToken();
String system = tokenStatnextToken();
tokenStatnextToken();
String nice = tokenStatnextToken();
Systemoutprintln(user+" , "+system+" , "+nice);
user = usersubstring(0,userindexOf("%"));
system = systemsubstring(0,systemindexOf("%"));
nice = nicesubstring(0,niceindexOf("%"));
float userUsage = new Float(user)floatValue();
float systemUsage = new Float(system)floatValue();
float niceUsage = new Float(nice)floatValue();
return (userUsage+systemUsage+niceUsage)/100;
}else{
brStatreadLine();
brStatreadLine();
tokenStat = new StringTokenizer(brStatreadLine());
tokenStatnextToken();
tokenStatnextToken();
tokenStatnextToken();
tokenStatnextToken();
tokenStatnextToken();
tokenStatnextToken();
tokenStatnextToken();
String cpuUsage = tokenStatnextToken();
Systemoutprintln("CPU idle : "+cpuUsage);
Float usage = new Float(cpuUsagesubstring(0,cpuUsageindexOf("%")));
return (1-usagefloatValue()/100);
}
} catch(IOException ioe){
Systemoutprintln(ioegetMessage());
freeResource(is, isr, brStat);
return 1;
} finally{
freeResource(is, isr, brStat);
}
}
getTotalPrivateDirty()就是获得自己进程所独占的内存
getTotalPss()意思就是假如有A,B,C,D,E 5个进程共享一片10kb的内存,A,B,F 3个进程又另外共享了30kb的内存,那么进程A调用这个函数返回就是10/5+30/3=12
所以理论上只要监视getTotalPrivateDirty()就能监视自己进程的内存变化
最后附上代码
public void getThisProcessMemeryInfo() {
int pid = AndroidosProcessmyPid();
androidosDebugMemoryInfo[] memoryInfoArray = activityManagergetProcessMemoryInfo(new int[] {pid});
Logd(TAG, "本应用当前使用了" + (float)memoryInfoArray[0]getTotalPrivateDirty() / 1024 + "mb的内存");
}
File root = EnvironmentgetRootDirectory();
StatFs sf = new StatFs(rootgetPath());
long blockSize = sfgetBlockSize();
long blockCount = sfgetBlockCount();
long availCount = sfgetAvailableBlocks();
Logd("TAG", "block大小:" + blockSize + ",block数目:" + blockCount + ",总大小:"+ blockSize blockCount / (1024 1024) + "MB");
Logd("TAG","可用的block数目::" + availCount + ",可用大小:" + availCount blockSize / (1024 1024)+ "MB");
以上就是关于VB获得其他程序的内存使用情况!全部的内容,包括:VB获得其他程序的内存使用情况!、怎么获取 Java 程序使用的内存、获取应用程序占用了多少内存 android.os.Debug.MemoryInfo等相关内容解答,如果想了解更多相关内容,可以关注我们,你们的支持是我们更新的动力!
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)