c++ 获得linux进程内存大小,C/C++获取进程常驻内存大小(get the process resident set size )...

1、进程内存接口函数

a40b8c9a7160d463a27407de30698c1f.png

2、接口封装实现#if defined(_WIN32)

#include

#include

#elif defined(__unix__) || defined(__unix) || defined(unix) || (defined(__APPLE__) && defined(__MACH__))

#include

#include

#if defined(__APPLE__) && defined(__MACH__)

#include

#elif (defined(_AIX) || defined(__TOS__AIX__)) || (defined(__sun__) || defined(__sun) || defined(sun) && (defined(__SVR4) || defined(__svr4__)))

#include

#include

#elif defined(__linux__) || defined(__linux) || defined(linux) || defined(__gnu_linux__)

#include

#endif

#else

#error "Cannot define getPeakRSS( ) or getCurrentRSS( ) for an unknown OS."

#endif

/**

* Returns the peak (maximum so far) resident set size (physical

* memory use) measured in bytes, or zero if the value cannot be

* determined on this OS.

*/

size_t getPeakRSS( )

{

#if defined(_WIN32)

/* Windows -------------------------------------------------- */

PROCESS_MEMORY_COUNTERS info;

GetProcessMemoryInfo( GetCurrentProcess( ), &info, sizeof(info) );

return (size_t)info.PeakWorkingSetSize;

#elif (defined(_AIX) || defined(__TOS__AIX__)) || (defined(__sun__) || defined(__sun) || defined(sun) && (defined(__SVR4) || defined(__svr4__)))

/* AIX and Solaris ------------------------------------------ */

struct psinfo psinfo;

int fd = -1;

if ( (fd = open( "/proc/self/psinfo", O_RDONLY )) == -1 )

return (size_t)0L; /* Can't open? */

if ( read( fd, &psinfo, sizeof(psinfo) ) != sizeof(psinfo) )

{

close( fd );

return (size_t)0L; /* Can't read? */

}

close( fd );

return (size_t)(psinfo.pr_rssize * 1024L);

#elif defined(__unix__) || defined(__unix) || defined(unix) || (defined(__APPLE__) && defined(__MACH__))

/* BSD, Linux, and OSX -------------------------------------- */

struct rusage rusage;

getrusage( RUSAGE_SELF, &rusage );

#if defined(__APPLE__) && defined(__MACH__)

return (size_t)rusage.ru_maxrss;

#else

return (size_t)(rusage.ru_maxrss * 1024L);

#endif

#else

/* Unknown OS ----------------------------------------------- */

return (size_t)0L; /* Unsupported. */

#endif

}

/**

* Returns the current resident set size (physical memory use) measured

* in bytes, or zero if the value cannot be determined on this OS.

*/

size_t getCurrentRSS( )

{

#if defined(_WIN32)

/* Windows -------------------------------------------------- */

PROCESS_MEMORY_COUNTERS info;

GetProcessMemoryInfo( GetCurrentProcess( ), &info, sizeof(info) );

return (size_t)info.WorkingSetSize;

#elif defined(__APPLE__) && defined(__MACH__)

/* OSX ------------------------------------------------------ */

struct mach_task_basic_info info;

mach_msg_type_number_t infoCount = MACH_TASK_BASIC_INFO_COUNT;

if ( task_info( mach_task_self( ), MACH_TASK_BASIC_INFO,

(task_info_t)&info, &infoCount ) != KERN_SUCCESS )

return (size_t)0L; /* Can't access? */

return (size_t)info.resident_size;

#elif defined(__linux__) || defined(__linux) || defined(linux) || defined(__gnu_linux__)

/* Linux ---------------------------------------------------- */

long rss = 0L;

FILE* fp = NULL;

if ( (fp = fopen( "/proc/self/statm", "r" )) == NULL )

return (size_t)0L; /* Can't open? */

if ( fscanf( fp, "%*s%ld", &rss ) != 1 )

{

fclose( fp );

return (size_t)0L; /* Can't read? */

}

fclose( fp );

return (size_t)rss * (size_t)sysconf( _SC_PAGESIZE);

#else

/* AIX, BSD, Solaris, and Unknown OS ------------------------ */

return (size_t)0L; /* Unsupported. */

#endif

}

接口引用:

size_t currentSize = getCurrentRSS( );

size_t peakSize = getPeakRSS( );

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值