用C语言获取系统内存 [英] Get System Memory in C

查看:143
本文介绍了用C语言获取系统内存的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

大家好,


我正在寻找一种便携式的方式来获取可用的总物理内存。


类似于 ; getTotalMemory"它返回我电脑上安装的内存

字节数,

" getAvailableMemory"并且它以字节为单位返回可用内存。


你知道是否有C函数,c ++对象或其他任何在Linux中编译的
和Windows获取这些数据?


再见并谢谢!

Alessandro

Hi all,

I''m searching a PORTABLE way to get the available and total physical memory.

Something like "getTotalMemory" and it returns the memory installed on my PC
in bytes, and
"getAvailableMemory" and it returns the available memory in bytes.

Do you know is there''s a C function, a c++ Object or anything else that
compiles in Linux and Windows to get these data?

Bye and thanks!
Alessandro

推荐答案

Alessandro Monopoli< te ******* @ ammaza.com>潦草地写道:
Alessandro Monopoli <te*******@ammaza.com> scribbled the following:
大家好,
我正在寻找一种便携式的方式来获得可用的总物理内存。
Hi all, I''m searching a PORTABLE way to get the available and total physical memory.




没有人。


-

/ - Joona Palaste(pa*****@cc.helsinki.fi) -------------芬兰-------- \

\ ---------------- ----------------------------------------规则! -------- /

嘘!大师正在分解!

- Gary Larson



There isn''t one.

--
/-- Joona Palaste (pa*****@cc.helsinki.fi) ------------- Finland --------\
\-------------------------------------------------------- rules! --------/
"Shh! The maestro is decomposing!"
- Gary Larson


Alessandro Monopoli写道:
Alessandro Monopoli wrote:
你好所有,

我正在寻找一种便携式的方式来获得可用的总物理内存。

像getTotalMemory这样的东西。它以字节为单位返回我电脑上安装的内存,
" getAvailableMemory"并且它以字节为单位返回可用内存。

你知道是否有C函数,c ++对象或其他在Linux和Windows中编译以获取这些数据的东西?
Hi all,

I''m searching a PORTABLE way to get the available and total physical memory.

Something like "getTotalMemory" and it returns the memory installed on my PC
in bytes, and
"getAvailableMemory" and it returns the available memory in bytes.

Do you know is there''s a C function, a c++ Object or anything else that
compiles in Linux and Windows to get these data?




POSIX符号_SC_PHYS_PAGES和_SC_PAGE_SIZE可能就是你想要的b $ b,至少对于类UNIX操作系统来说是这样的:


long phypz = sysconf(_SC_PHYS_PAGES);

long psize = sysconf(_SC_PAGE_SIZE);


当然,你需要然后用手计算_total_物理内存。

不幸的是我不知道一个可移植的解决方案,以同样简单的方式获得_available_

物理内存。


Windows不支持所有这些符号,因此你需要调用

这样的东西,如GlobalMemoryStatus()填充MEMORYSTATUS

结构,本身包含一个成员dwTotalPhys包含该系统的

_total_物理内存和dwAvailPhys in指定

_available_物理内存。


欢迎提出建议和更好的解决方案。请告诉我们

其他人会推荐的内容。


\Steve


-


Steve Graegert {C / C ++&& Java&& .NET}

CSI科技集团(StReG)

< graegerts(AT)cs(DOT)技术(DOT)de>



The POSIX symbols _SC_PHYS_PAGES and _SC_PAGE_SIZE could be what you are
looking for, at least for UNIX-like operating systems:

long phypz = sysconf(_SC_PHYS_PAGES);
long psize = sysconf(_SC_PAGE_SIZE);

Of course, you need to calculate _total_ physical memory by hand then.
Unfortunately I don''t know a portable solution to obtain the _available_
physical memory the same easy way.

Windows does not support all of these symbols, so you will need to call
something like GlobalMemoryStatus() which fills a MEMORYSTATUS
structure, that itself contains a member dwTotalPhys containing the
_total_ physical memory for this system and dwAvailPhys indicating the
_available_ physical memory.

Suggestions and better solutions are welcome. Please let us know what
others would recommend.

\Steve

--

Steve Graegert {C/C++ && Java && .NET}
CSI Technology Group (StReG)
<graegerts(AT)cs(DOT)technologies(DOT)de>


>我正在寻找一种便携式的方式来获取可用的总物理内存。


我不相信甚至有半便携式定义

可用物理内存和总物理内存。


那么使用哪种便携方式来获取这些信息?


嗯,你可以使用总物理记忆对于资产库存,

虽然目前还不清楚它是否包括

IDE磁盘驱动器或视频内存上的缓冲存储器。


可用物理内存:你会尝试分配那么多b $ b,不是吗?没有人说这一切都是连续的,或者说你将能够把它全部用掉。没有人说答案

在你使用它的时候不会改变。


Gordon L. Burditt
>I''m searching a PORTABLE way to get the available and total physical memory.

I don''t believe that there is even a semi-portable DEFINITION of
"available physical memory" and "total physical memory".

And what portable way is there to USE that information?

Well, you might use "total physical memory" for an asset inventory,
although it''s unclear whether that includes the buffer memory on
IDE disk drives or video memory.

"available physical memory": you''re going to try to allocate that
much, aren''t you? Nobody said it was all contiguous, or that
you were going to be able to hog it all. And nobody says the answer
won''t change by the time you can use it.

Gordon L. Burditt


这篇关于用C语言获取系统内存的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆