简体   繁体   English

C ++内存泄漏会对CPU使用产生负面影响吗?

[英]Can C++ memory leaks negatively affect CPU usage?

I have a C++ program that has a pretty terrible memory leak, about 4MB / second. 我有一个C ++程序,它有一个非常可怕的内存泄漏,大约4MB /秒。 I know where it's coming from and can fix it, but that's not my main problem. 我知道它来自哪里可以解决它,但这不是我的主要问题。 My program is taking up a very large amount of CPU usage and it isn't running as fast as I want it to. 我的程序占用了大量的CPU,并且运行速度不如我想要的那么快。 I have two different threads in the program. 我在程序中有两个不同的线程。 One by itself takes ~50% CPU, which is fine, and the other by itself takes ~15% CPU, which is fine. 一个人自己需要大约50%的CPU,这很好,而另一个本身需要大约15%的CPU,这很好。 Together however CPU usage is 100% and the program cannot run as fast as it needs to. 但是,CPU使用率是100%,并且程序无法以所需的速度运行。

Can a memory leak by itself cause a problem like this? 内存泄漏本身会导致这样的问题吗? I know the program will eventually crash due to the leaked memory, but does a memory leak immediately lead to a slower program? 我知道程序最终会因泄漏的内存而崩溃,但是内存泄漏会导致程序变慢吗? By immediately I mean the program is too slow at the very start, not just when the memory footprint is huge. 我立刻意味着程序从一开始就太慢了,而不仅仅是当内存占用空间很大时。

Thanks! 谢谢!

Regardless of whether or not your memory leak is causing the problem it needs to be fixed. 无论您的内存泄漏是否导致问题,都需要修复。 Once you fix the memory leak see if you're still having the problem. 一旦你修复了内存泄漏,看看你是否还有问题。 You should be able to answer your own question at that point. 那时你应该能够回答你自己的问题。

Allocations in general can be slow and it sounds like you're doing a lot of them here. 一般来说,分配可能很慢,听起来你在这里做了很多。 After fixing your leak, you will want to consider a pooled memory implementation to avoid thrashing your heap so much, especially in a multi-threaded environment. 在修复泄漏之后,您将需要考虑池化内存实现,以避免对堆进行如此多的颠簸,尤其是在多线程环境中。

It should not be a problem while you still have available memory. 当你还有可用的内存时,这应该不是问题。 However, memory allocation is a relatively slow process, so depending on how often you do it, it might account for your problems. 但是,内存分配是一个相对较慢的过程,因此根据您执行此操作的频率,它可能会解决您的问题。

Don't forget that a profiler is your friend for all and every optimization needs. 不要忘记,探查器是您所有优化需求的朋友。 It knows much better than you what your main bottleneck is. 它比你更了解你的主要瓶颈。

Oh, and fix your memory leak now, while it's still easy. 哦,现在修复你的内存泄漏,虽然它仍然很容易。

Well, allocation is slow and does require at least some CPU effort to search for appropriate blocks to give. 好吧,分配很慢,并且需要至少一些CPU工作来搜索适当的块来提供。 Besides that I wouldn't think so. 除此之外,我不这么认为。 It sounds like your program is in quite a mess so I wouldn't doubt that there's some larger issue at heart. 这听起来像你的程序非常混乱,所以我不怀疑有一个更大的问题。

Have a look at your page faults. 看看你的页面错误。 In Windows, use Task Manager, and in Unix/Linux try ps. 在Windows中,使用任务管理器,在Unix / Linux中尝试ps。 It's likely that the extra processor time is being used to allocate additional memory to your process, and once the free physical memory has been exhausted, the OS has to swap out unused pages to the disk. 可能是额外的处理器时间用于为进程分配额外的内存,并且一旦空闲的物理内存耗尽,操作系统就必须将未使用的页面交换到磁盘。

Another approach would be to use a profiling tool to see where the bottlenecks are in your code. 另一种方法是使用分析工具来查看代码中的瓶颈所在。

There are several angles here: 1) You have memory leaks. 这里有几个角度:1)你有内存泄漏。 This is bound to cause page faults in your cache so data has to be sourced from RAM/disk. 这必然会导致缓存中的页面错误,因此数据必须来自RAM /磁盘。 More I/O, more problems. 更多I / O,更多问题。 Are you using a memory manager? 你在使用内存管理器吗? If not consider using one. 如果不考虑使用一个。 Look into dlmalloc for a start. 查看dlmalloc的开始。


2) CPU usage 100% may not be due to this problem. 2)CPU使用率100%可能不是由于此问题。 Obviously the way to go is to first fix this leak and then use a profiler [Quantify is best, but costs. 显然,要走的路是首先解决这个漏洞,然后使用剖析器[量化是最好的,但成本。 VTune is not bad, although I don't like the interface. VTune也不错,虽然我不喜欢这个界面。 Else gprof is not bad and its free.] and see which parts of your code is taking up CPU cycles. 其他gprof也不错,它是免费的。]并查看代码的哪些部分占用了CPU周期。


3) I see that you are using threads. 3)我看到你正在使用线程。 Syncing threads up is non-trivial. 同步线程并非易事。 See if you are unnecessarily waiting for mutexes or something similar. 看看你是否在不必要地等待互斥或类似的东西。

Disposing of previously allocated memory fragments should be relatively faster than their allocation and (as long as memory leak means "memory lost due to missing deallocation call") it shouldn't really affect your speed, only overall memory usage. 处置先前分配的内存片段应该比它们的分配相对更快(并且(只要内存泄漏意味着“由于缺少释放调用而导致内存丢失”),它不应该真正影响你的速度,只影响整体内存使用。

Although if you allocate huge amounts of memory every second and don't do proper deallocations, this could be the problem. 虽然如果你每秒分配大量内存并且没有进行适当的解除分配,这可能是问题所在。 I had the same issue when wrongly compiled gtkmm + librsvg leaked ~5 megabytes per second on screen redraw and that, of course, had some notable performance impact. 当错误编译gtkmm + librsvg在屏幕重绘时泄漏〜每秒5兆字节时,我遇到了同样的问题,当然,这会产生一些显着的性能影响。

Of course, this doesn't mean you shouldn't eliminate your memory leaks if you know that they exist. 当然,这并不意味着如果您知道它们存在,就不应该消除内存泄漏。 Memory leaks could cause something more serious than performance troubles. 内存泄漏可能会导致比性能问题更严重的问题。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM