获取应用程序占用了多少内存 android.os.Debug.MemoryInfo

获取应用程序占用了多少内存 android.os.Debug.MemoryInfo,第1张

getTotalPrivateDirty()就是获得自己进程所独占的内存

getTotalPss()意思就是假如有A,B,C,D,E 5个进程共享一片10kb的内存,A,B,F 3个进程又另外共享了30kb的内存,那么进程A调用这个函数返回就是10/5+30/3=12

所以理论上只要监视getTotalPrivateDirty()就能监视自己进程的内存变化

最后附上代码

public void getThisProcessMemeryInfo() {

int pid = AndroidosProcessmyPid();

androidosDebugMemoryInfo[] memoryInfoArray = activityManagergetProcessMemoryInfo(new int[] {pid});

Logd(TAG, "本应用当前使用了" + (float)memoryInfoArray[0]getTotalPrivateDirty() / 1024 + "mb的内存");

}

linux下获取占用CPU资源最多的10个进程,可以使用如下命令组合

ps aux|head -1;ps aux|grep -v PID|sort -rn -k +3|head

linux下获取占用内存资源最多的10个进程,可以使用如下命令组合:

ps aux|head -1;ps aux|grep -v PID|sort -rn -k +4|head

命令组合解析(针对CPU的,MEN也同样道理):

ps aux|head -1;ps aux|grep -v PID|sort -rn -k +3|head

该命令组合实际上是下面两句命令:

ps aux|head -1

ps aux|grep -v PID|sort -rn -k +3|head

//cpu频率

using MicrosoftWin32;

private int GetCPUFrequency()

{

RegistryKey rk = RegistryLocalMachineOpenSubKey(@"HARDWARE\DESCRIPTION\System\CentralProcessor\0");

object obj = rkGetValue("~MHz");

int CPUFrequency = (int)obj;

return CPUFrequency;

}

//////////////////////////////////

//磁盘空间 Management

using SystemManagement;

private long GetFreeDiskSpace()

{

ManagementObject disk = new ManagementObject(

"win32_logicaldiskdeviceid=\"d:\"");

diskGet();

string totalByte = disk["FreeSpace"]ToString();

long freeDiskSpaceMb = ConvertToInt64(totalbyte)/1024/1024;

return freeDiskSpaceMb;

}

/////////////////////

//内存信息

using System;

using SystemText;

using SystemRuntimeInteropServices;

namespace ConsoleApplication1

{

///// <summary>

/// Summary description for Class1

/// </summary>

class Class1

{

[StructLayout(LayoutKindSequential)]

public struct MEMORY_INFO

{

public uint dwLength;

public uint dwMemoryLoad;

public uint dwTotalPhys;

public uint dwAvailPhys;

public uint dwTotalPageFile;

public uint dwAvailPageFile;

public uint dwTotalVirtual;

public uint dwAvailVirtual;

}

[DllImport("kernel32")]

public static extern void GlobalMemoryStatus(ref MEMORY_INFO meminfo);

public static int Main(string[] args)

{

Class1 class1 = new Class1();

class1GetMemoryStatus();

return 0;

}

private void GetMemoryStatus()

{

MEMORY_INFO MemInfo;

MemInfo = new MEMORY_INFO();

GlobalMemoryStatus(ref MemInfo);

long totalMb = ConvertToInt64( MemInfodwTotalPhysToString())/1024/1024;

long avaliableMb = ConvertToInt64( MemInfodwAvailPhysToString())/1024/1024;

ConsoleWriteLine( "物理内存共有" + totalMb + " MB");

ConsoleWriteLine( "可使用的物理内存有" + avaliableMb +" MB");

}

}

//////////////////////////////

//cpu名字

using MicrosoftWin32;

private string GetCPUName()

{

RegistryKey rk = RegistryLocalMachineOpenSubKey(@"HARDWARE\DESCRIPTION\System\CentralProcessor\0");

object obj = rkGetValue("ProcessorNameString");

string CPUName = (string)obj;

return CPUNameTrimStart();

}

///////////////////////

//OS版本

using System;

namespace determineOS_CS

{

class Class1

{

static void Main(string[] args)

{

// Get OperatingSystem information from the system namespace

SystemOperatingSystem osInfo =SystemEnvironmentOSVersion;

// Determine the platform

switch(osInfoPlatform)

{

// Platform is Windows 95, Windows 98,

// Windows 98 Second Edition, or Windows Me

case SystemPlatformIDWin32Windows:

switch (osInfoVersionMinor)

{

case 0:

ConsoleWriteLine ("Windows 95");

break;

case 10:

if(osInfoVersionRevisionToString()=="2222A")

ConsoleWriteLine("Windows 98 Second Edition");

else

ConsoleWriteLine("Windows 98");

break;

case 90:

ConsoleWriteLine("Windows Me");

break;

}

break;

// Platform is Windows NT 351, Windows NT 40, Windows 2000,

// or Windows XP

case SystemPlatformIDWin32NT:

switch(osInfoVersionMajor)

{

case 3:

ConsoleWriteLine("Windows NT 351");

break;

case 4:

ConsoleWriteLine("Windows NT 40");

break;

case 5:

if (osInfoVersionMinor==0)

ConsoleWriteLine("Windows 2000");

else

ConsoleWriteLine("Windows XP");

break;

}break;

}

ConsoleReadLine ();

}

}

}

嘎嘎

以上就是关于获取应用程序占用了多少内存 android.os.Debug.MemoryInfo全部的内容,包括:获取应用程序占用了多少内存 android.os.Debug.MemoryInfo、Linux下如何查看哪些进程占用的CPU内存资源最多、c# 如何获取某一个进程的cpu和内存使用情况等相关内容解答,如果想了解更多相关内容,可以关注我们,你们的支持是我们更新的动力!

欢迎分享,转载请注明来源:内存溢出

原文地址: http://outofmemory.cn/web/9628129.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2023-04-30
下一篇 2023-04-30

发表评论

登录后才能评论

评论列表(0条)

保存