views:

234

answers:

5

When moving an application from 32bit to 64bit, where will increased memory usage occur?

I understand that pointers will double in size, I suspect that the chars in a string are 'bunched' to use memory more efficiently (so will not use much more memory).

Where else would memory usage increase? Is there anywhere that it would decrease, or where non-arithmetic operations would see a speed benefit?

+6  A: 

You may see additional alignment to cost a few extra bytes here and there. Code will probably be larger due to 64 bit constants in operands.

As for speed, you may experience slowdowns due to the increased memory usage. The CPU cache will fill up more quickly.

I have seen significant speed benefits going from x86 going to x64, because the x86 has far fewer registers than the x64 architecture. Compilers make use of the extra registers to better optimize your code. I saw 15% speedup on identical hardware.

jdv
Interesting, I hadn't considered cache. So an x86 compiler will not use all available registers on a 64bit compatible CPU? Is there a way to force it?
Peter Gibson
@Peter 32-bit executables can't access 64-bit register, as far as I know. The best way to tell the compiler that it "can use" the 64-bit registers is to tell it to output 64-bit executables.
luiscubal
@Peter: No, there's no way to reference a x64 register with x86 instructions. Compile for 64-bit if you want to use the various 64-bit extensions.
jalf
+3  A: 

As you've noted pointers will larger. Depending on the processor architecture so might also be ints and/or longs. Strings should stay the same size but be aligned differently in memory for efficiency. Generally, memory alignment of data structures on 64-bit boundaries will cause increased fragmentation in most cases.

Also your process address space may appear (in many architectures) to be much larger with stack frame pointers appearing in high memory (and growing downwards) but as these are invariably virtual memory pointers the actual physical memory in use by your application is usually considerably smaller.

bjg
Unlikely that int's will be largers. The two most common 64 bit models are LP64 (Posix) and LLP64 (Windows) and in both of those, int's are still 32 bit.
R Samuel Klatchko
+2  A: 

There are a few cases where you might save memory. The actual code might be a bit shorter in places, because fewer load/stores are necessary due to the increased number of registers. The default calling convention passes parameters in registers, for example.

Overall, a 64-bit app will probably use a bit more memory than a 32-bit one. But it's not going to be a deal-breaker.

jalf
"Overall, a 64-bit app will probably use a bit more memory than a 32-bit one." But won't that one bit take 8 Byte due to alignment issues? ;)
tstenner
+2  A: 

Depending on the architecture the code can also grow. Global variables and constant are often referenced via absolute addresses (which are relocated by the program loader), these references are 64 bit in 64 bit mode. On x64 there is an explicit mov instruction for 64 bit constants so the program will only grow of the size of the constant. Jump and call instructions may also get bigger, but that depends on a lot of parameters of the compiler and the linker. On other architectures it can even be worse. On SPARC for instance, when going from 32 to 64 bit can make the code grow significantly. As the sparc doesn't have an instruction that can load more than 22 bits, when loading the 32 bit address of a global variable or constant, it needs 2 instructions, to load a 64 bit constant it even needs 5 instructions with 3 registers. By augmenting the register pressure the compiler often misses optimization opportunities, making the code much bigger than necessary.

tristopia
+1  A: 

About stack: The reasons why 64-bit programs require more stack memory

Andrey Karpov