09-09-2008 05:12 AM - edited 09-09-2008 05:14 AM
09-09-2008 02:02 PM
Hello,
A degradation of the response time of PostDeferredCall is definitely unexpected, and I would like to investigate it. But so far, I haven't been able to reproduce the problem. I tried running a test program with 20,000 iterations, but it didn't show any slow down over time (although it does show an alternating phenomenon in the response time of consecutive calls which I can't quite explain either).
It might be that I couldn't reproduce the problem because your scenario differs from my test program in some significant ways. I'm attaching the program here.I'm hoping that you can tweak it in the direction of your real program, and so reproduce the problem. If you do, please reattach it here so that I can reproduce it myself.
Thanks
Luis
09-10-2008 03:31 AM
09-10-2008 11:31 AM
Calling malloc/free can definitely have a performance impact. I would think that that could also happen in Linux, but the pattern might be different -- less obvious in some cases, maybe more obvious in others. Definitely if you keep allocating memory and holding on to it, the performance degradation will become more noticeable over time, as the amount of allocated memory increases, and therefore the memory manager has more data blocks to manage whenever it is invoked (by calling malloc or free). But even if you release each memory block before allocating the next one, there's the possibility that increased fragmentation of the free memory list can cause future allocations to become slower if, for example, it takes longer to find a free block that is large enough for the current allocation.
In general, memory allocation is one of the most common targets for optimization whenever developers set out to improve performance. Probably the most common strategy is to pre-allocate memory as much as possible. For example, if you are growing the same buffer periodically, you should grow it by big chunks at a time, rather than making minor size increments. This will reduce the number of memory increases. If, instead, you have lots of individual buffers, you might want to consider merging them together under a single memory block. Also, if you find yourself repeatedly allocating and freeing similar memory blocks, consider allocating it just once, reusing the same memory block, and then increasing its size when needed. You'll have to decide how big to make it initially, of course. But if you make it large enough, you should be able to get away with only having to increase it very rarely.
Luis