你可以先用內(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è)面。
目前創(chuàng)新互聯(lián)建站已為上千余家的企業(yè)提供了網(wǎng)站建設(shè)、域名、虛擬主機(jī)、綿陽(yáng)服務(wù)器托管、企業(yè)網(wǎng)站設(shè)計(jì)、鳳縣網(wǎng)站維護(hù)等服務(wù),公司將堅(jiān)持客戶導(dǎo)向、應(yīng)用為本的策略,正道將秉承"和諧、參與、激情"的文化,與客戶和合作伙伴齊心協(xié)力一起成長(zhǎng),共同發(fā)展。
方法如下:
首先
創(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);
}
}
1、ide一般run時(shí)可設(shè)置內(nèi)存大小,如eclipse設(shè)置如下
eclipse安裝后,在安裝目錄有個(gè)config.ini文件,內(nèi)容如下:
-vmargs
-Xms40m
-Xmx256m
或是 其實(shí)也很簡(jiǎn)單。打開Eclipse包,在Contents/MacOS 目錄下有一個(gè) eclipse.ini 文件,
用編輯工具打開他,把Xms128m更改成Xms256m。
這個(gè)文件用來(lái)配置eclipse啟動(dòng)時(shí)候的內(nèi)存分配方案,Xms是初始化內(nèi)存大小,Xmx是最大可使用內(nèi)存大小,這個(gè)默認(rèn)的配置是eclipse資源消耗最小化的配置。如果你的項(xiàng)目比較大,這個(gè)配置必須改,一般適當(dāng)調(diào)整為128,384即可,若項(xiàng)目更大一些則調(diào)整的再大一些,根據(jù)實(shí)際情況決定。這個(gè)參數(shù)配置的大小很關(guān)鍵,太小,eclipse垃圾回收會(huì)過于頻繁導(dǎo)致很慢,或者內(nèi)存堆棧溢出而崩潰。太大,eclipse會(huì)吃掉大量?jī)?nèi)存,垃圾回收周期變長(zhǎng),但每次回收會(huì)很慢,影響使用。所以你在配置的時(shí)候需要權(quán)衡,嘗試!
2、web可以在web容器中設(shè)置相關(guān)大小
3、一般寫代碼時(shí),如果會(huì)用到大內(nèi)存時(shí),要注意。
本文名稱:如何java代碼使用內(nèi)存 java內(nèi)存馬
文章分享:http://muchs.cn/article10/doegdgo.html
成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供網(wǎng)站制作、微信公眾號(hào)、微信小程序、建站公司、定制網(wǎng)站、響應(yīng)式網(wǎng)站
聲明:本網(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)