虚拟v。物理内存在评估C / C ++内存泄漏 [英] virtual v. physical memory in assessing C/C++ memory leak

查看:168
本文介绍了虚拟v。物理内存在评估C / C ++内存泄漏的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个C ++应用程序,我试图阻止内存泄漏,我意识到我不完全理解虚拟和物理内存之间的区别。



top 的结果(所以16.8g =虚拟,111m =物理):



4406 um 20 0 16.8g 111m 4928 S 64.7 22.8 36:53.65客户端



我的进程拥有500个连接,每个用户一个,这些数字意味着每个用户有大约30 MB的虚拟开销。没有进入我的应用程序的细节,唯一的方法这听起来远程现实,添加在一起的所有向量,结构,线程,堆栈上的函数等,是如果我不知道虚拟内存实际意味着什么。




  • 因此,我的问题是:什么操作在C + +会膨胀虚拟内存这么多?

  • 如果我的任务是使用虚拟内存的gigs,这是一个问题吗?

  • 堆栈和堆函数变量,向量等 - c>删除内存泄漏(通过 delete free() 或者这样)必然会减少物理和虚拟内存使用?


解决方案

p> 虚拟内存是您的程序处理的内容。它由 malloc new 等返回的所有地址组成。每个进程都有自己的虚拟地址空间。虚拟地址的使用在理论上受到程序地址大小的限制:32位程序有4GB的地址空间; 64位程序有更多。实际上,进程可以分配的虚拟内存量小于这些限制。



物理内存是焊接到您主板的芯片,或安装在内存插槽中。



虚拟内存子系统映射您的程序使用的虚拟地址到CPU发送到RAM芯片的物理地址。在任何特定时刻,您分配的大部分虚拟地址都未映射;因此物理内存的使用低于虚拟内存的使用。如果访问已分配但未映射的虚拟地址,操作系统将不可见地分配物理内存并将其映射。如果不访问虚拟地址,操作系统可能会取消映射物理内存。



依次回答您的问题:



  • 充满虚拟内存这么多?


new malloc ,静态分配大型数组。



  • 这是一个问题,如果我的任务是使用gigs的虚拟内存?


这取决于程序的使用模式。如果你分配了你从来没有触摸过的大量内存轨迹,如果你的程序是一个64位程序,你可能会使用虚拟内存的演示。



此外,如果你的内存使用增长没有约束,你最终会耗尽一些资源。



  • 堆栈和堆函数变量,向量等 - 这些必然会增加物理内存的使用吗?


不一定,但可能。触摸变量的动作确保至少暂时地,它(和所有的存储器靠近)在物理存储器中。 (Aside:像 std :: vector 的容器可以在堆栈或堆上分配,但包含的对象在堆上分配。)



  • 删除内存泄漏(通过delete或free()等)必然会减少物理和虚拟内存使用?


物理:可能。虚拟:是。


I have a C++ application that I am trying to iron the memory leaks out of and I realized I don't fully understand the difference between virtual and physical memory.

Results from top (so 16.8g = virtual, 111m = physical):

4406 um 20 0 16.8g 111m 4928 S 64.7 22.8 36:53.65 client

My process holds 500 connections, one for each user, and at these numbers it means there is about 30 MB of virtual overhead for each user. Without going into the details of my application, the only way this could sound remotely realistic, adding together all the vectors, structs, threads, functions on the stack, etc., is if I have no idea what virtual memory actually means. No -O optimization flags, btw.

So my questions are:

  • what operations in C++ would inflate virtual memory so much?
  • Is it a problem if my task is using gigs of virtual memory?
  • The stack and heap function variables, vectors, etc. - do those necessarily increase the use of physical memory?
  • Would removing a memory leak (via delete or free() or such) necessarily reduce both physical and virtual memory usage?

解决方案

Virtual memory is what your program deals with. It consists of all of the addresses returned by malloc, new, et al. Each process has its own virtual-address space. Virtual address usage is theoretically limited by the address size of your program: 32-bit programs have 4GB of address space; 64-bit programs have vastly more. Practically speaking, the amount of virtual memory that a process can allocate is less than those limits.

Physical memory are the chips soldered to your motherboard, or installed in your memory slots. The amount of physical memory in use at any given time is limited to the amount of physical memory in your computer.

The virtual-memory subsystem maps virtual addresses that your program uses to physical addresses that the CPU sends to the RAM chips. At any particular moment, most of your allocated virtual addresses are unmapped; thus physical memory use is lower than virtual memory use. If you access a virtual address that is allocated but not mapped, the operating system invisibly allocates physical memory and maps it in. When you don't access a virtual address, the operating system might unmap the physical memory.

To take your questions in turn:

  • what operations in C++ would inflate virtual memory so much?

new, malloc, static allocation of large arrays. Generally anything that requires memory in your program.

  • Is it a problem if my task is using gigs of virtual memory?

It depends upon the usage pattern of your program. If you allocate vast tracks of memory that you never, ever touch, and if your program is a 64-bit program, it may be okay that you are using gigs of virtual memory.

Also, if your memory use grows without bound, you will eventually run out of some resource.

  • The stack and heap function variables, vectors, etc. - do those necessarily increase the use of physical memory?

Not necessarily, but likely. The act of touching a variable ensures that, at least momentarily, it (and all of the memory "near" it) is in physical memory. (Aside: containers like std::vector may be allocated on either stack or heap, but the contained objects are allocated on the heap.)

  • Would removing a memory leak (via delete or free() or such) necessarily reduce both physical and virtual memory usage?

Physical: probably. Virtual: yes.

这篇关于虚拟v。物理内存在评估C / C ++内存泄漏的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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