如何在我的C ++程序中检测和估计堆碎片? [英] How to detect and estimate heap fragmentation in my C++ program?

查看:94
本文介绍了如何在我的C ++程序中检测和估计堆碎片?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在开发一个VC ++ NT服务,旨在连续运行多个月。它使用VC ++运行时堆。很明显,堆碎片可以在某些时候导致其故障(认为它是内存不足)。

I'm developing a VC++ NT service that is meant to operate continuously for many months. It uses VC++ runtime heap intensively. Clearly heap fragmentation can at some point cause it malfunctioning (thinking it's out of memory).

我可以在我的服务上运行什么测试来估计它容易堆的程度碎片?

What tests can I run on my service to estimate the degree it is prone to heap fragmentation?

推荐答案

您已经得到了几个关于如何防止堆碎片问题的答案,你的问题直接。几乎唯一的方法估计它是如何可能遭受碎片问题是模拟大量的使用,并衡量你得到的碎片。

You've gotten a couple of answers that talk about how to prevent problems for heap fragmentation, but neither really addressed your question directly. Nearly the only way to estimate how likely it is to suffer problems for fragmentation is to simulate lots of use, and measure the fragmentation you get.

由于它是一个NT服务,模拟几个月的使用主要包括匆忙做出很多请求。有可能您可以提出的请求速度通常比预期的要快,因此您可以在几小时内模拟几个月的请求,甚至可能更少(根据您通常期望接收请求的速度) )。

Since it's an NT service, simulating months of use mostly consists of making a lot of requests in a hurry. Chances are that you can make requests faster than it's normally expected to receive them, so you can probably simulate several months worth of requests in only a few hours, and quite possibly even less (depending on the rate at which you normally expect to receive requests).

一旦你模拟了几个月的工作(或者你这样做),然后你需要查看堆来看你有多少碎片得到。这不容易,但它通常是可能的。你将开始通过注入一个线程到服务进程(在线程注入的Googling或者该顺序上的东西应该得到相当多的信息)。然后,你需要走的堆,看(特别是)块是自由的,但太小,不太可能满足大多数请求。假设你使用MS VC ++,你使用_heapwalk来遍历堆,它将遍历堆,告诉你堆中每个块的地址,大小和状态(空闲或使用中)。

Once you simulate months worth of work (or even as you're doing so) you then need to look at the heap to see how much fragmentation you're getting. This isn't easy, but it's normally possible. You'll start by injecting a thread into the service process (Googling on "thread injection" or something on that order should get a fair amount of info). Then you'll need to walk the heap, looking (in particular) for blocks that are free, but too small to be likely to satisfy most requests. Assuming you're using MS VC++, you walk the heap with _heapwalk, and it'll walk through the heap telling you the address, size, and status (free or in-use) of each block in the heap.

最后一个细节:为了产生有意义的结果,可执行文件和包含注入线程的DLL都必须链接到DLL中的运行时库。这意味着整个进程将有一个堆,因此您注入的线程将遍历服务正在使用的堆。如果你静态链接标准库,DLL和服务将各自有自己的堆。 DLL将走自己的堆,这将告诉你什么关于服务进程正在使用的堆。

One last detail: for this to produce meaningful results, both the executable AND the DLL containing your injected thread have to be linked to the run-time library in a DLL. This means there will be one heap for the whole process, so your injected thread will walk the heap being used by your service. If you link the standard library statically, the DLL and the service will each have its own heap. The DLL will walk its own heap, which will tell you nothing about the heap being used by the service process.

这篇关于如何在我的C ++程序中检测和估计堆碎片?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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