gpt4 book ai didi

c++ - 如何从 C++ 程序内部测量内存使用情况?

转载 作者:IT老高 更新时间:2023-10-28 12:41:38 26 4
gpt4 key购买 nike

对于 c++ 程序,是否可以跟踪程序一次使用了多少内存?

例如,带有原型(prototype)的函数:

int getEstimatedTotalMemoryUsage();

我想如果不可能,那么就必须退出程序,进行系统调用并从那里检查结果。如果是这样,有哪些工具可用于此类目的?假设这样的事情是可能的,那就是。

编辑:我正在使用 linux,有什么工具可以为你做到这一点?

最佳答案

是的 - 使用 POSIX getrusage .来自 Linux man page :

Synopsis

#include <sys/time.h>
#include <sys/resource.h>

int getrusage(int who, struct rusage *usage);

Description

getrusage() returns current resource usages, for a who of either RUSAGE_SELF or RUSAGE_CHILDREN. The former asks for resources used by the current process, the latter for resources used by those of its children that have terminated and have been waited for.

struct rusage {
struct timeval ru_utime; /* user time used */
struct timeval ru_stime; /* system time used */
long ru_maxrss; /* maximum resident set size */
long ru_ixrss; /* integral shared memory size */
long ru_idrss; /* integral unshared data size */
long ru_isrss; /* integral unshared stack size */
long ru_minflt; /* page reclaims */
long ru_majflt; /* page faults */
long ru_nswap; /* swaps */
long ru_inblock; /* block input operations */
long ru_oublock; /* block output operations */
long ru_msgsnd; /* messages sent */
long ru_msgrcv; /* messages received */
long ru_nsignals; /* signals received */
long ru_nvcsw; /* voluntary context switches */
long ru_nivcsw; /* involuntary context switches */
};

关于c++ - 如何从 C++ 程序内部测量内存使用情况?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5120861/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com