本文目錄一覽:
- 1、如何導出Java應用程序的內存快照文件
- 2、怎樣用Java獲取內存中的數據?
- 3、解決java讀取大文件內存溢出問題,如何在不
- 4、安裝好的JAVA軟體在內存的哪個文件夾里
- 5、java怎麼將生成的文件放入內存?
如何導出Java應用程序的內存快照文件
問了一下我之前的在遠標教育的大學同學,他說在windows下找到要導出的Java應用的進程號。比如我要導出本機的Eclipse的內存快照,因為Eclipse也是Java應用。首先要找到Eclispe的進程號。在命令行輸入:tasklist | findstr eclipse。
可以看到,Eclispe的進程號是8052。然後在命令行輸入:jmap -dump:format=b,file=elipse.hprof 8052。file=elipse.hprof意思是設置生成的文件名,8052就是Java程序的進程號。
如果是在Linux系統下面,在命令行輸入:ps -ef | grep eclipse。
可以看到eclipse的進程號是2622。同樣,在命令行輸入:jmap -dump:format=b,file=elipse.hprof 2622。
把生成的elipse.hprof在SecureCRT命令行輸入:sz elipse.hprof,把文件下載到本地,用MAT之類的工具進行分析就可以了。
怎樣用Java獲取內存中的數據?
方法如下:
首先
創建一個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) {
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;
}
然後,就是最關鍵的,得到cpu的利用率,已用內存,可用內存,最大內存等信息。
import java.io.InputStreamReader;
import java.io.LineNumberReader;
import sun.management.ManagementFactory;
import com.sun.management.OperatingSystemMXBean;
import java.io.*;
import java.util.StringTokenizer;
/**
* 獲取系統信息的業務邏輯實現類.
* @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 = Runtime.getRuntime().totalMemory() / kb;
// 剩餘內存
long freeMemory = Runtime.getRuntime().freeMemory() / kb;
// 最大可使用內存
long maxMemory = Runtime.getRuntime().maxMemory() / kb;
OperatingSystemMXBean osmxb = (OperatingSystemMXBean) ManagementFactory
.getOperatingSystemMXBean();
// 操作系統
String osName = System.getProperty(“os.name”);
// 總的物理內存
long totalMemorySize = osmxb.getTotalPhysicalMemorySize() / kb;
// 剩餘的物理內存
long freePhysicalMemorySize = osmxb.getFreePhysicalMemorySize() / kb;
// 已使用的物理內存
long usedMemory = (osmxb.getTotalPhysicalMemorySize() – osmxb
.getFreePhysicalMemorySize())
/ kb;
// 獲得線程總數
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();
}
// 構造返回對象
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);
}
}
解決java讀取大文件內存溢出問題,如何在不
1、傳統的在內存中讀取
讀取文件行的標準方式是在內存中讀取,Guava 和Apache Commons IO都提供了如下所示快速讀取文件行的方法:
這種方法帶來的問題是文件的所有行都被存放在內存中,當文件足夠大時很快就會導致程序拋出OutOfMemoryError 異常。
例如:讀取一個大約1G的文件:
這種方式開始時只佔用很少的內存:(大約消耗了0Mb內存)
然而,當文件全部讀到內存中後,我們最後可以看到(大約消耗了2GB內存):
這意味這一過程大約耗費了2.1GB的內存——原因很簡單:現在文件的所有行都被存儲在內存中。
把文件所有的內容都放在內存中很快會耗盡可用內存——不論實際可用內存有多大,這點是顯而易見的。
此外,我們通常不需要把文件的所有行一次性地放入內存中——相反,我們只需要遍歷文件的每一行,然後做相應的處理,處理完之後把它扔掉。所以,這正是我們將要做的——通過行迭代,而不是把所有行都放在內存中。
2、文件流
現在讓我們看下這種解決方案——我們將使用Java.util.Scanner類掃描文件的內容,一行一行連續地讀取:
這種方案將會遍歷文件中的所有行——允許對每一行進行處理,而不保持對它的引用。總之沒有把它們存放在內存中:(大約消耗了150MB內存)
3、Apache Commons IO流
同樣也可以使用Commons IO庫實現,利用該庫提供的自定義LineIterator:
由於整個文件不是全部存放在內存中,這也就導致相當保守的內存消耗:(大約消耗了150MB內存)
4、結論
這篇短文介紹了如何在不重複讀取與不耗盡內存的情況下處理大文件——這為大文件的處理提供了一個有用的解決辦法。
安裝好的JAVA軟體在內存的哪個文件夾里
JAVA軟體運行的文件夾,默認是在c盤下的programs files。
很多軟體都提供了自定義軟體存放的位置,可以選擇其他盤符。
java怎麼將生成的文件放入內存?
這個要使用到內存流。BufferedOutputStream或者BufferedWriter。
文件的讀取和寫入都應該會了吧?普通的流讀寫都是直接從文件中讀取或者寫入到文件中的,而內存流則是把文件中的內容寫入到電腦內存或者是從內存中讀取出來。具體的話就是把輸出流替換成BufferedOutputStream或者BufferedWriter即可
原創文章,作者:小藍,如若轉載,請註明出處:https://www.506064.com/zh-tw/n/290953.html