资讯

精准传达 • 有效沟通

从品牌网站建设到网络营销策划,从策略到执行的一站式服务

java代码内存使用 java开发内存

如何计算一个Java程序运行占用多少内存

可以用 system(命令) 调用 DOS/Windows 命令 获取 正在使用多少 内存 (memory).

成都创新互联公司专注为客户提供全方位的互联网综合服务,包含不限于成都网站制作、成都做网站、扶绥网络推广、成都小程序开发、扶绥网络营销、扶绥企业策划、扶绥品牌公关、搜索引擎seo、人物专访、企业宣传片、企业代运营等,从售前售中售后,我们都将竭诚为您服务,您的肯定,是我们最大的嘉奖;成都创新互联公司为所有大学生创业者提供扶绥建站搭建服务,24小时服务热线:18982081108,官方网址:www.cdcxhl.com

命令例子:

wmic process where name="cmd.exe" get WorkingSetSize

这里 "cmd.exe" 你可替换成 你的程序 名字。

你也可以用你的程序 进程 PID 号数 调用, 命令是:

wmic process where processid=6884 get WorkingSetSize

这里6884你可替换成 你的程序 进程 PID。

输出有2行,第二行是占用内存字节数:

WorkingSetSize

4616192

c/c++ 语言 :

system("wmic process where processid=6884 get WorkingSetSize");

system("wmic process where name=\"cmd.exe\" get WorkingSetSize");

用程序名调用时,若有多个同名程序在运行,输出的 内存数 将分行输出出来。

如何获取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代码运行的时候将内存分成哪些区?

您好,提问者:

java中在内存中划分:栈内存和堆内存。

1、栈内存:栈中是存放一些定义的变量的引用,比如:int a = 1; a那么就存在栈内存中,java中垃圾回收是JVM帮我们完成的,这里比C大大提高了程序员的繁碎。如果想要控制可以使用System.gc();来通知JVM虚拟机执行,但是什么时候执行还是由JVM虚拟机来完成的。

2、堆内存:堆中是存放一些比如数组,map类型等。

为什么这么一小段java代码会使用那么多内存

JVM调用GC的频度还是很高的,主要两种情况下进行垃圾回收:

当应用程序线程空闲;另一个是java内存堆不足时,会不断调用GC,若连续回收都解决不了内存堆不足的问题时,就会报out of memory错误。因为这个异常根据系统运行环境决定,所以无法预期它何时出现。

根据GC的机制,程序的运行会引起系统运行环境的变化,增加GC的触发机会。

为了避免这些问题,程序的设计和编写就应避免垃圾对象的内存占用和GC的开销。显示调用System.GC()只能建议JVM需要在内存中对垃圾对象进行回收,但不是必须马上回收,

一个是并不能解决内存资源耗空的局面,另外也会增加GC的消耗。

请问用Java代码,怎样测试一段程序占用了多少内存?

你可以先用内存监控工具,进行监控,看看这个功能到底用多少内存。如果不多,其实都不需要实现你说的代码监控的。如果你要使用代码监控,你可是使用Runtime类的几个属性,MaxMemory、FreeMemory、TotalMemory。然后实现个线程,在下载pdf功能前开启线程,然后完毕时关闭线程,如果内存即将溢出(设定个阈值,比如说15%),就报错,跳转到错误页面。

如何看一段JAVA代码耗了多少内存

使用java自带的性能分析工具jvisualvm , 可以方便的查看内存, 对象, 线程等多种信息.

win+R    然后输入   jvisualvm  回车即可

效果如下图


分享标题:java代码内存使用 java开发内存
新闻来源:http://cdkjz.cn/article/hpdgso.html
多年建站经验

多一份参考,总有益处

联系快上网,免费获得专属《策划方案》及报价

咨询相关问题或预约面谈,可以通过以下方式与我们联系

业务热线:400-028-6601 / 大客户专线   成都:13518219792   座机:028-86922220