可以用 system(命令) 調(diào)用 DOS/Windows 命令 獲取 正在使用多少 內(nèi)存 (memory).
成都創(chuàng)新互聯(lián)公司專注為客戶提供全方位的互聯(lián)網(wǎng)綜合服務(wù),包含不限于成都網(wǎng)站制作、成都做網(wǎng)站、扶綏網(wǎng)絡(luò)推廣、成都小程序開發(fā)、扶綏網(wǎng)絡(luò)營(yíng)銷、扶綏企業(yè)策劃、扶綏品牌公關(guān)、搜索引擎seo、人物專訪、企業(yè)宣傳片、企業(yè)代運(yùn)營(yíng)等,從售前售中售后,我們都將竭誠(chéng)為您服務(wù),您的肯定,是我們最大的嘉獎(jiǎng);成都創(chuàng)新互聯(lián)公司為所有大學(xué)生創(chuàng)業(yè)者提供扶綏建站搭建服務(wù),24小時(shí)服務(wù)熱線:18982081108,官方網(wǎng)址:jinyejixie.com
命令例子:
wmic process where name="cmd.exe" get WorkingSetSize
這里 "cmd.exe" 你可替換成 你的程序 名字。
你也可以用你的程序 進(jìn)程 PID 號(hào)數(shù) 調(diào)用, 命令是:
wmic process where processid=6884 get WorkingSetSize
這里6884你可替換成 你的程序 進(jìn)程 PID。
輸出有2行,第二行是占用內(nèi)存字節(jié)數(shù):
WorkingSetSize
4616192
c/c++ 語(yǔ)言 :
system("wmic process where processid=6884 get WorkingSetSize");
system("wmic process where name=\"cmd.exe\" get WorkingSetSize");
用程序名調(diào)用時(shí),若有多個(gè)同名程序在運(yùn)行,輸出的 內(nèi)存數(shù) 將分行輸出出來(lái)。
方法如下:
首先
創(chuàng)建一個(gè)Bean用來(lái)存貯要得到的信
public class MonitorInfoBean {
/** 可使用內(nèi)存. */
private long totalMemory;
/** 剩余內(nèi)存. */
private long freeMemory;
/** 最大可使用內(nèi)存. */
private long maxMemory;
/** 操作系統(tǒng). */
private String osName;
/** 總的物理內(nèi)存. */
private long totalMemorySize;
/** 剩余的物理內(nèi)存. */
private long freePhysicalMemorySize;
/** 已使用的物理內(nèi)存. */
private long usedMemory;
/** 線程總數(shù). */
private int totalThread;
/** cpu使用率. */
private double cpuRatio;
public long getFreeMemory() {
return freeMemory;
}
public void setFreeMemory(long freeMemory) {
this.freeMemory = freeMemory;
}
public long getFreePhysicalMemorySize() {
return freePhysicalMemorySize;
}
public void setFreePhysicalMemorySize(long freePhysicalMemorySize) {
this.freePhysicalMemorySize = freePhysicalMemorySize;
}
public long getMaxMemory() {
return maxMemory;
}
public void setMaxMemory(long maxMemory) {
this.maxMemory = maxMemory;
}
public String getOsName() {
return osName;
}
public void setOsName(String osName) {
this.osName = osName;
}
public long getTotalMemory() {
return totalMemory;
}
public void setTotalMemory(long totalMemory) {
this.totalMemory = totalMemory;
}
public long getTotalMemorySize() {
return totalMemorySize;
}
public void setTotalMemorySize(long totalMemorySize) {
this.totalMemorySize = totalMemorySize;
}
public int getTotalThread() {
return totalThread;
}
public void setTotalThread(int totalThread) {
this.totalThread = totalThread;
}
public long getUsedMemory() {
return usedMemory;
}
public void setUsedMemory(long usedMemory) {
this.usedMemory = usedMemory;
}
public double getCpuRatio() {
return cpuRatio;
}
public void setCpuRatio(double cpuRatio) {
this.cpuRatio = cpuRatio;
}
}
之后,建立bean的接口
public interface IMonitorService {
public MonitorInfoBean getMonitorInfoBean() throws Exception;
}
然后,就是最關(guān)鍵的,得到cpu的利用率,已用內(nèi)存,可用內(nèi)存,最大內(nèi)存等信息。
import java.io.InputStreamReader;
import java.io.LineNumberReader;
import sun.management.ManagementFactory;
import com.sun.management.OperatingSystemMXBean;
import java.io.*;
import java.util.StringTokenizer;
/**
* 獲取系統(tǒng)信息的業(yè)務(wù)邏輯實(shí)現(xiàn)類.
* @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;
/**
* 獲得當(dāng)前的監(jiān)控對(duì)象.
* @return 返回構(gòu)造好的監(jiān)控對(duì)象
* @throws Exception
* @author GuoHuang
*/
public MonitorInfoBean getMonitorInfoBean() throws Exception {
int kb = 1024;
// 可使用內(nèi)存
long totalMemory = Runtime.getRuntime().totalMemory() / kb;
// 剩余內(nèi)存
long freeMemory = Runtime.getRuntime().freeMemory() / kb;
// 最大可使用內(nèi)存
long maxMemory = Runtime.getRuntime().maxMemory() / kb;
OperatingSystemMXBean osmxb = (OperatingSystemMXBean) ManagementFactory
.getOperatingSystemMXBean();
// 操作系統(tǒng)
String osName = System.getProperty("os.name");
// 總的物理內(nèi)存
long totalMemorySize = osmxb.getTotalPhysicalMemorySize() / kb;
// 剩余的物理內(nèi)存
long freePhysicalMemorySize = osmxb.getFreePhysicalMemorySize() / kb;
// 已使用的物理內(nèi)存
long usedMemory = (osmxb.getTotalPhysicalMemorySize() - osmxb
.getFreePhysicalMemorySize())
/ kb;
// 獲得線程總數(shù)
ThreadGroup parentThread;
for (parentThread = Thread.currentThread().getThreadGroup(); parentThread
.getParent() != null; parentThread = parentThread.getParent())
;
int totalThread = parentThread.activeCount();
double cpuRatio = 0;
if (osName.toLowerCase().startsWith("windows")) {
cpuRatio = this.getCpuRatioForWindows();
}
else {
cpuRatio = this.getCpuRateForLinux();
}
// 構(gòu)造返回對(duì)象
MonitorInfoBean infoBean = new MonitorInfoBean();
infoBean.setFreeMemory(freeMemory);
infoBean.setFreePhysicalMemorySize(freePhysicalMemorySize);
infoBean.setMaxMemory(maxMemory);
infoBean.setOsName(osName);
infoBean.setTotalMemory(totalMemory);
infoBean.setTotalMemorySize(totalMemorySize);
infoBean.setTotalThread(totalThread);
infoBean.setUsedMemory(usedMemory);
infoBean.setCpuRatio(cpuRatio);
return infoBean;
}
private static double getCpuRateForLinux(){
InputStream is = null;
InputStreamReader isr = null;
BufferedReader brStat = null;
StringTokenizer tokenStat = null;
try{
System.out.println("Get usage rate of CUP , linux version: "+linuxVersion);
Process process = Runtime.getRuntime().exec("top -b -n 1");
is = process.getInputStream();
isr = new InputStreamReader(is);
brStat = new BufferedReader(isr);
if(linuxVersion.equals("2.4")){
brStat.readLine();
brStat.readLine();
brStat.readLine();
brStat.readLine();
tokenStat = new StringTokenizer(brStat.readLine());
tokenStat.nextToken();
tokenStat.nextToken();
String user = tokenStat.nextToken();
tokenStat.nextToken();
String system = tokenStat.nextToken();
tokenStat.nextToken();
String nice = tokenStat.nextToken();
System.out.println(user+" , "+system+" , "+nice);
user = user.substring(0,user.indexOf("%"));
system = system.substring(0,system.indexOf("%"));
nice = nice.substring(0,nice.indexOf("%"));
float userUsage = new Float(user).floatValue();
float systemUsage = new Float(system).floatValue();
float niceUsage = new Float(nice).floatValue();
return (userUsage+systemUsage+niceUsage)/100;
}else{
brStat.readLine();
brStat.readLine();
tokenStat = new StringTokenizer(brStat.readLine());
tokenStat.nextToken();
tokenStat.nextToken();
tokenStat.nextToken();
tokenStat.nextToken();
tokenStat.nextToken();
tokenStat.nextToken();
tokenStat.nextToken();
String cpuUsage = tokenStat.nextToken();
System.out.println("CPU idle : "+cpuUsage);
Float usage = new Float(cpuUsage.substring(0,cpuUsage.indexOf("%")));
return (1-usage.floatValue()/100);
}
} catch(IOException ioe){
System.out.println(ioe.getMessage());
freeResource(is, isr, brStat);
return 1;
} finally{
freeResource(is, isr, brStat);
}
}
您好,提問(wèn)者:
java中在內(nèi)存中劃分:棧內(nèi)存和堆內(nèi)存。
1、棧內(nèi)存:棧中是存放一些定義的變量的引用,比如:int a = 1; a那么就存在棧內(nèi)存中,java中垃圾回收是JVM幫我們完成的,這里比C大大提高了程序員的繁碎。如果想要控制可以使用System.gc();來(lái)通知JVM虛擬機(jī)執(zhí)行,但是什么時(shí)候執(zhí)行還是由JVM虛擬機(jī)來(lái)完成的。
2、堆內(nèi)存:堆中是存放一些比如數(shù)組,map類型等。
JVM調(diào)用GC的頻度還是很高的,主要兩種情況下進(jìn)行垃圾回收:
當(dāng)應(yīng)用程序線程空閑;另一個(gè)是java內(nèi)存堆不足時(shí),會(huì)不斷調(diào)用GC,若連續(xù)回收都解決不了內(nèi)存堆不足的問(wèn)題時(shí),就會(huì)報(bào)out of memory錯(cuò)誤。因?yàn)檫@個(gè)異常根據(jù)系統(tǒng)運(yùn)行環(huán)境決定,所以無(wú)法預(yù)期它何時(shí)出現(xiàn)。
根據(jù)GC的機(jī)制,程序的運(yùn)行會(huì)引起系統(tǒng)運(yùn)行環(huán)境的變化,增加GC的觸發(fā)機(jī)會(huì)。
為了避免這些問(wèn)題,程序的設(shè)計(jì)和編寫就應(yīng)避免垃圾對(duì)象的內(nèi)存占用和GC的開銷。顯示調(diào)用System.GC()只能建議JVM需要在內(nèi)存中對(duì)垃圾對(duì)象進(jìn)行回收,但不是必須馬上回收,
一個(gè)是并不能解決內(nèi)存資源耗空的局面,另外也會(huì)增加GC的消耗。
你可以先用內(nèi)存監(jiān)控工具,進(jìn)行監(jiān)控,看看這個(gè)功能到底用多少內(nèi)存。如果不多,其實(shí)都不需要實(shí)現(xiàn)你說(shuō)的代碼監(jiān)控的。如果你要使用代碼監(jiān)控,你可是使用Runtime類的幾個(gè)屬性,MaxMemory、FreeMemory、TotalMemory。然后實(shí)現(xiàn)個(gè)線程,在下載pdf功能前開啟線程,然后完畢時(shí)關(guān)閉線程,如果內(nèi)存即將溢出(設(shè)定個(gè)閾值,比如說(shuō)15%),就報(bào)錯(cuò),跳轉(zhuǎn)到錯(cuò)誤頁(yè)面。
使用java自帶的性能分析工具jvisualvm , 可以方便的查看內(nèi)存, 對(duì)象, 線程等多種信息.
win+R????然后輸入???jvisualvm??回車即可
效果如下圖
當(dāng)前文章:java代碼內(nèi)存使用 java開發(fā)內(nèi)存
文章出自:http://jinyejixie.com/article30/hpdgso.html
成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供外貿(mào)建站、網(wǎng)頁(yè)設(shè)計(jì)公司、電子商務(wù)、企業(yè)網(wǎng)站制作、自適應(yīng)網(wǎng)站、網(wǎng)站設(shè)計(jì)公司
聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請(qǐng)盡快告知,我們將會(huì)在第一時(shí)間刪除。文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如需處理請(qǐng)聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時(shí)需注明來(lái)源: 創(chuàng)新互聯(lián)
移動(dòng)網(wǎng)站建設(shè)知識(shí)