Take the look at this working class.
看看这个工人阶级吧。
I created this class for my project in Qt, because Qt doesn't have its own methods to get information about memory like: Current process memory, Free Memory or Total Memory.
我在Qt中为我的项目创建了这个类,因为Qt没有自己的方法来获取有关内存的信息,如:当前进程内存,可用内存或总内存。
The class is adapted to Qt, but can be easily adapted to pure C or other api Its need to link "psapi.lib" or -lpsapi
该类适用于Qt,但可以很容易地适应纯C或其他api它需要链接“psapi.lib”或-lpsapi
The problem is I need to complete this class with Linux and Mac code, I think there is many experienced programmers that can fullfill it with correctly and working code...
问题是我需要用Linux和Mac代码完成这个类,我认为有很多有经验的程序员可以用正确的代码填充它...
the header *.h file
标题* .h文件
#ifndef __CLASS__MEMORYINFO__
#define __CLASS__MEMORYINFO__
#include <QtCore>
#ifdef WIN32
#include <Windows.h>
#endif
class class_MemoryInfo
{
public:
class_MemoryInfo( void );
qlonglong GetFreeMemory( void );
qlonglong GetTotalMemory( void );
qlonglong GetCurrentProcesMemory( void );
};
#endif
and the *.cpp file
和* .cpp文件
#include "class_MemoryInfo.h"
#ifdef WIN32
#include <Psapi.h>
#endif
class_MemoryInfo::class_MemoryInfo()
{
}
qlonglong class_MemoryInfo::GetFreeMemory( void )
{
qlonglong _value = 0;
#ifdef WIN32
MEMORYSTATUSEX MemoryStatus;
ZeroMemory( &MemoryStatus, sizeof( MEMORYSTATUSEX ) );
MemoryStatus.dwLength = sizeof( MEMORYSTATUSEX );
if ( GlobalMemoryStatusEx( &MemoryStatus) )
{
_value = MemoryStatus.ullAvailPhys;
}
else
_value = 0;
#endif
return _value;
}
qlonglong class_MemoryInfo::GetTotalMemory( void )
{
qlonglong _value = 0;
#ifdef WIN32
MEMORYSTATUSEX MemoryStatus;
ZeroMemory( &MemoryStatus, sizeof(MEMORYSTATUSEX ));
MemoryStatus.dwLength = sizeof( MEMORYSTATUSEX );
if ( GlobalMemoryStatusEx( &MemoryStatus) )
{
_value = MemoryStatus.ullTotalPhys;
}
else
_value = 0;
#endif
return _value;
}
qlonglong class_MemoryInfo::GetCurrentProcesMemory( void )
{
qlonglong _value = 0;
#ifdef WIN32
PROCESS_MEMORY_COUNTERS pmc;
if ( GetProcessMemoryInfo( GetCurrentProcess(), &pmc, sizeof(pmc)) )
_value = pmc.WorkingSetSize;
else
_value = 0;
#endif
return _value;
}
1 个解决方案
#1
0
Well, I won't completely write the drop-in equivalent for you, but here is a basic outline of at least the total memory. This should also work for free memory, but I think you may have to look elsewhere for process memory.
好吧,我不会完全为你写下插件,但这里至少是总内存的基本轮廓。这也适用于空闲内存,但我认为您可能需要寻找其他地方的进程内存。
Mac
Use sysctl
; man sysctl
to see all the options.
使用sysctl; man sysctl可以看到所有选项。
QProcess p;
p.start("sysctl", QStringList() << "hw.physmem");
p.waitForFinished();
QString system_info = p.readAllStandardOutput();
p.close();
Linux
Read /proc/meminfo
. Open that file to see all the options.
读/ proc / meminfo。打开该文件以查看所有选项。
// The actual command is "awk '/MemTotal/ { print $2 }' /proc/meminfo", but
// QProcess has trouble with the single quotes. Putting it as a separate
// argument (without the quotes) seems to work.
p.start("awk", QStringList() << "/MemTotal/ { print $2 }" << "/proc/meminfo");
p.waitForFinished();
QString memory = p.readAllStandardOutput();
p.close();
#1
0
Well, I won't completely write the drop-in equivalent for you, but here is a basic outline of at least the total memory. This should also work for free memory, but I think you may have to look elsewhere for process memory.
好吧,我不会完全为你写下插件,但这里至少是总内存的基本轮廓。这也适用于空闲内存,但我认为您可能需要寻找其他地方的进程内存。
Mac
Use sysctl
; man sysctl
to see all the options.
使用sysctl; man sysctl可以看到所有选项。
QProcess p;
p.start("sysctl", QStringList() << "hw.physmem");
p.waitForFinished();
QString system_info = p.readAllStandardOutput();
p.close();
Linux
Read /proc/meminfo
. Open that file to see all the options.
读/ proc / meminfo。打开该文件以查看所有选项。
// The actual command is "awk '/MemTotal/ { print $2 }' /proc/meminfo", but
// QProcess has trouble with the single quotes. Putting it as a separate
// argument (without the quotes) seems to work.
p.start("awk", QStringList() << "/MemTotal/ { print $2 }" << "/proc/meminfo");
p.waitForFinished();
QString memory = p.readAllStandardOutput();
p.close();