
首先
创建一个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);
}
}
public class CpuInfoCollector
{
[DllImport("kernel32")]
//获取系统内存信息
private static extern void GlobalMemoryStatus(ref MemoryInfor meminfo);
private const string CategoryName = "Processor";
private const string CounterName = "% Processor Time";
private const string InstanceName = "_Total"; //最大内存使用
private float m_MaxMemory=0;
//最大内存使用时间
private DateTime m_MaxMemoryTime=DateTimeNow;
//最小内存使用
private float m_MinMemory;
//最小内存使用时间
private DateTime m_MinMemoryTime = DateTimeNow; //最大Cpu使用
private float m_MaxCpu=0;
//最大Cpu使用时间
private DateTime m_MaxCpuTime = DateTimeNow;
//最小Cpu使用
private float m_MinCpu;
//最小Cpu使用时间
private DateTime m_MinCpuTime = DateTimeNow; /// <summary>
/// 机器性能类(获取CPU使用率)
/// </summary>
private static PerformanceCounter pc;
public DTServerInfoCollector()
{
pc = new PerformanceCounter(CategoryName, CounterName, InstanceName);
}
/// <summary>
/// 获取进程名称
/// </summary>
private static string m_ProcessName = SystemReflectionAssemblyGetExecutingAssembly()GetName()Name;
/// 获取CPU占用率
public float CpuLoad
{
get
{
//实时cpu信息
float cpu = pcNextValue();
if (cpu>MaxCpu)
{
//最大cpu使用赋值
MaxCpu = cpu;
//最大cpu使用时间
MaxCpuTime = DateTimeNow;
}
if (cpu < MinCpu)
{
//最小cpu使用赋值
MinCpu = cpu;
//最小cpu使用时间
MinCpuTime = DateTimeNow;
}
ThreadSleep(500);
return pcNextValue();
}
} //最大内存使用
public float MaxMemory
{
get { return m_MaxMemory; }
set { m_MaxMemory = value; }
} //最大内存使用时间
public DateTime MaxMemoryTime
{
get { return m_MaxMemoryTime; }
set { m_MaxMemoryTime = value; }
}
//最小内存使用
public float MinMemory
{
get { return m_MinMemory; }
set { m_MinMemory = value; }
}
//最小内存使用时间
public DateTime MinMemoryTime
{
get { return m_MinMemoryTime; }
set { m_MinMemoryTime = value; }
}
//最大Cpu使用
public float MaxCpu
{
get { return m_MaxCpu; }
set { m_MaxCpu = value; }
} //最大Cpu使用时间
public DateTime MaxCpuTime
{
get { return m_MaxCpuTime; }
set { m_MaxCpuTime = value; }
}
//最小Cpu使用
public float MinCpu
{
get { return m_MinCpu; }
set { m_MinCpu = value; }
}
//最小Cpu使用时间
public DateTime MinCpuTime
{
get { return m_MinCpuTime; }
set { m_MinCpuTime = value; }
}
/// 获取内存使用
private MemoryInfo GetMemoryInfo()
{
MemoryInfo mInfo = new MemoryInfo();
try
{
MemoryInfor memInfor = GetMemoryInfor();
long lMemoryTotalMB = memInfordwTotalPhys/1024/1024;
long lMemoryAvailMB = memInfordwAvailPhys/1024/1024;
//已经使用内存=最大内存-剩余内存
long lMemoryUsedMB = lMemoryTotalMB - lMemoryAvailMB;
//最大内存
mInfoMemory = lMemoryTotalMB;
if (lMemoryUsedMB>MaxMemory)
{
MaxMemory = lMemoryUsedMB;
MaxMemoryTime = DateTimeNow;
}
if (lMemoryUsedMB < MinMemory)
{
MinMemory = lMemoryUsedMB;
MinMemoryTime = DateTimeNow;
}
//已经使用内存
mInfoMemoryUsed = lMemoryUsedMB;
//最大使用内存
mInfoMaxMemory = MaxMemory;
//最大内存的时间
mInfoMaxMemoryTime = MaxMemoryTime;
//最小使用内存
mInfoMinMemory = MinMemory;
//最小内存的时间
mInfoMinMemoryTime = MinMemoryTime; }
catch (Exception ex)
{
return null;
}
return mInfo; }
/// <summary>
/// 获取系统内存信息
/// </summary>
private MemoryInfor GetMemoryInfor()
{
MemoryInfor memoryInfo = new MemoryInfor();
GlobalMemoryStatus(ref memoryInfo);
return memoryInfo;
}
/// <summary>
/// 获取程序的线程数
/// </summary>
public static int Threads
{
get
{
int i = 0;
try
{
foreach (Process vProcess in ProcessGetProcesses())
{
if (vProcessProcessName == m_ProcessName)
{
i = vProcessThreadsCount;
break;
}
}
}
catch (Exception ex)
{
}
return i;
}
}
}
/// <summary>
/// 定义内存的信息结构
/// </summary>
[StructLayout(LayoutKindSequential)]
public struct MemoryInfor
{
public uint dwLength;
/// <summary>
/// 已经使用的内存
/// </summary>
public uint dwMemoryLoad;
/// <summary>
/// 总物理内存大小
/// </summary>
public uint dwTotalPhys;
/// <summary>
/// 可用物理内存大小
/// </summary>
public uint dwAvailPhys;
/// <summary>
/// 交换文件总大小
/// </summary>
public uint dwTotalPageFile;
/// <summary>
/// 可用交换文件大小
/// </summary>
public uint dwAvailPageFile;
/// <summary>
/// 总虚拟内存大小
/// </summary>
public uint dwTotalVirtual;
/// <summary>
/// 可用虚拟内存大小
/// </summary>
public uint dwAvailVirtual;
}
如果您使用的是华为手机,手机提醒内存不足,可以通过以下方法 *** 作处理:
一、存储剩余空间不足(低于10%)
请参考以下方法排查解决:
1 清理存储空间
(1)建议卸载不常用的应用,下载过多应用会占用较大内存;
(2)清理手机空间:进入手机管家, 选择清理加速并勾选需要清理的数据 ,点击 删除 (已选 ),您还可以使用一键优化,根据扫描结果及清理建议,释放存储空间。
2 检查是否使用了多个多用户/隐私空间
添加多用户和开启隐私空间会占用您的存储空间,建议您可以删除不使用的多用户或隐私空间:
温馨提醒:如果您没有使用多用户/隐私空间,或者您的手机不支持此功能,请您忽略此方案。
(1)进入手机设置,搜索多用户,查看是否有已创建不使用的多用户,如果有,请您点击多用户,选择删除用户。
(2)进入手机设置,搜索隐私空间,查看是否有已创建不使用的隐私空间(显示开启表示您未创建隐私空间,显示进入表示您已创建了隐私空间),如果有,请您点击右上角三个点按钮,选择删除隐私空间。
3 将大文件移动至存储卡中保存
如果您的的手机支持插入外置存储卡,建议您插入外置存储卡后将手机内部存储中的、音频、视频等文件移动至外置存储卡来释放存储空间。 *** 作如下:
进入文件管理 > 浏览 > 我的手机,找到需要移动的文件,长按勾选此文件,点击移动按钮移动至外置存储卡保存。
4 将默认存储位置修改为存储卡
如果您的手机支持插入外置存储卡,建议您插入外置存储卡后进入设置 > 存储 > 默认存储位置,将默认存储位置修改为外置存储卡,这样也可以使手机本身内存不被过多占用。
5 将手机恢复出厂设置
建议您备份好数据(微信/QQ等应用需单独备份),进入手机设置,搜索恢复出厂设置,根据手机界面提示完成恢复出厂设置 *** 作。
二、存储剩余空间充足
请参考以下方法排查:
1 如果您手机是EMUI5X的版本,请您提前备份好数据(微信/QQ等应用需单独备份)将系统升级至最新版本。早期版本存在“分身类应用在升级更新时提示内存不足”的问题。
2 可能您在浏览器中访问了一些应用推广类网站,页面伪装提示手机剩余存储空间或运行内存不足来向您推广应用“优化”系统。建议先清理浏览器缓存,然后更新手机管家病毒库来拦截伪装的网站:
EMUI 8X及以下: 手机管家 > 病毒查杀 > 设置,选择自动更新病毒库。
EMUI 9X/10X/11X & MagicUI 2X/ 3X/40:手机管家 > 设置,选择自动更新病毒库/联网病毒查杀。
3 系统更新升级失败,提示“内部存储不足,请至少删除XXX后重试”或者“空间不足无法升级”:系统升级需要预留一定的空间,请根据提示清理足够空间。
题主,我大概明白你的意思了,我的也是miui10。
你看你想要的是不是我说的这个情况。
在设置里面找桌面与最近任务,点开显示内存信息,如下图所示。
然后打开后台程序那个界面的时候就能显示出来了
VBS dim qiu,hz
Plugin hz=WindowForeground()
Plugin qiu=MemoryReadSingle(hz,&H08FE5E5C)
MessageBox ""&cint(qiu)&""
VBS qiu=""&cint(qiu)&""
Rem 开始
Plugin hz=WindowForeground()
Plugin qiu=MemoryReadSingle(hz,&H08FE5E5C)
If qiu>=100
Delay 500
KeyPress 51,3
Delay 5000
EndIf
Delay 500
Goto 开始
上面这段代码是用按键精灵读取一个游戏内存信息的代码,当读取到的信息大于或等于100的时候就按键盘上的3键,功能是实现了--
以上就是关于怎么获取 Java 程序使用的内存全部的内容,包括:怎么获取 Java 程序使用的内存、C# 怎么获取CPU使用率、可用内存等、手机显示剩余内存严重不足,怎么办等相关内容解答,如果想了解更多相关内容,可以关注我们,你们的支持是我们更新的动力!
欢迎分享,转载请注明来源:内存溢出
微信扫一扫
支付宝扫一扫
评论列表(0条)