Posted By: PhrostByte | Jul 13th, 2004 @ 9:07 AM
page 1 of 1
Comments: 10 | Views: 6456
Hey guys,  there seems to be something horribly wrong with the Windows memory manager.

See this source: http://dev.int64.org/memory.c
It runs in 8sec on winxp, and 0.013sec in linux.  Even with my 1.5GiB of ram I thought maybe it wasn't able to expand the memory each time but I made it use a fixed 10MiB heap it and performs just as bad.  Any gurus care to comment?
RomSteady
RomSteady
Kimono's rock...
Okay, token dumb questions...

1) Are you running the code as debug in Windows?

2) Which compiler and version are you using?

msemack
msemack
Embedded Systems Guy
There could be any number of things going on.

First you need to see if the code is taking a long time to execute by itself, or if something on the system is starving it.

Fire up PerfMon.  Get some solid graphs of CPU and Memory usage.  Make them detailed.  Look at total RAM, FS cache, Commit Charge, and RAM Used by your Program.  Look at total CPU time, and CPU time in your specific process.

Don't just look at Task Manager, it can miss sudden spikes and nosedives. 

Find an equivalent program in Linux, and compare the same things.

Have you done anything stupid like changing your Windows memory management settings (changing the pagefile, registry tweaks, etc)?

Make sure your Windows and Linux configurations are as close as possible (same background RAM and CPU usage, no Anti-virus software, no server services or other background tasks, etc). 

Also, where are you getting your time numbers?  Are they from the output of the code?  If yes, don't put too much faith in them.  You'd be amazed how inaccurate system timekeeping can be (especially in Windows).  Use a stopwatch.

Another interesting thing to do is look at the disassembly of the two programs.  Besdies obvious differences in system calls and statically linked libs, look at how the core algorithim is compiled.

It might be fun to try with a non-Microsoft compiler (Intel, OpenWatcom, gcc, etc).
jonathanh
jonathanh
My mod color is red
"Horribly wrong" is overstating things, unless your program spends its entire time realloc'ing memory - which is almost NEVER a good idea.  The Windows heap manager has historically been optimized for the common case (i.e. malloc and free), rather than the edge cases (realloc).  Finding a benchmark that punishes the edge case of one implementation is no great news.

Having said all that, if you really want to get into this stuff you should also test the Low Fragmentation Heap on Windows XP and up.
PhrostByte wrote:
I'm sorry but regardless of how often or not a function is used, realloc() going over 600 times slower on Windows *is* horribly wrong.  There is no excuse for slow code.



I think you're missing the point. Windows memory management is optimised for the most common case, so by making realloc() 600 times slower they may well have made malloc() 5 times faster. Since malloc() is called considerably more often, the overall effect is a more efficient system.

Good optimisation is often a case of sacrificing the performance in extreme cases to benefit the more common scenarios.
I got similar results. I compiled it and ran it just for fun:

XP / VS2K3 debug: 12s,
XP / VS2K3 full optimizations: 10s.
RH9 / GCC on MS Virtual PC: 0.010
Win98 / VS2K3 full opt / MS Virtual PC: 25+ (I said it was for fun, remember?)

It's interesting. But it's a benchmark of a single function call. What matters is real life application performance. When Apache or MySQL run 600 times faster on Linux I'll pay more attention. Still it's an interesting quirk to explore. 

/Lars.
Shouldn't you be freeing up buf even if realloc fails? I mean in its current form you will get a memory leak.
The problem may be the compiler optimizations. You should compare the assembly generated code.
page 1 of 1
Comments: 10 | Views: 6456
Microsoft Communities