views:

98

answers:

2

Hello!

I learned yesterday that in DelphiXE using the compiler directive:

{$SetPEFlags IMAGE_FILE_LARGE_ADDRESS_AWARE}

you can access/use 4GB address space on 64bit computers even though DelphiXE compiler produces a 32bit code.

I tried today DelphiXE program on 4GB machine with Windows7 and after starting the program I really get the following result:

  • Kbytes of physical memory: ~4.000.000KB
  • Kbytes of free physical memory: ~3.200.000KB
  • Percent of Memory in use: ~20%
  • Kbytes of virtual address space: ~4.000.000KB¨
  • Kbytes of free virtual address space: ~4.000.000KB

Each time after loading several objects (tables, strings, lists, a lot of pointers) the amoung of free memory goes down, what is fine, untill all memory is used. Everything fine. But here are some questions:

1. Sometimes I got the filling that program is using hard disc instead of RAM, because it slows down (but the memory is still available). Is that possible? If yes, how to prevent program using disc, when RAM is still available? Or maybe some temporary files are written to disc?

  1. What would happen with the same program on machine with 8G Ram? Would 32bit program be able to use all 8G? I guess not since pointers are only 32 bits and there is a limit what they can access.

  2. If I would compile the same program on 64bit machine with 64 bit compiler (what is not possible yet unfortunatelly), my guess is that on machine with 4GRam, 64bit program would have less free RAM space available than 32bit program with "IMAGE_FILE_LARGE_ADDRESS_AWARE" enabled, because pointers are 64bits and therefore they alone spent more space than 32 bit pointers. Am I thinking completelly wrong?

Thanks for any reply.

+2  A: 

Yes, the Operating System will swap parts of your virtual memory (what you call RAM) to the paging file if it decides this is necessary.

A 32 bit program will not be able to use more than 4 GB (even on 64 bit Windows) and finally a native 64 bit program will not use more memory than a 32 bit program because of the pointer size (internally 32 bit pointer are 64 bit pointers in x64 windows).

And a side note: if you set the Large Address Aware switch you should be really sure that your application (which includes vcl, delphi units and components) can handle addresses > 2 GB. For instance casting a pointer to an Integer is something which is not allowed then. There are more pitfalls of course.

Remko
Thanks for exact and very useful answers. I am not doing strange things, at least as far as I know. Definitelly not casting a pointer to integer, can you point to some more such forbidden actions, please. So that I can check if using Large Address Aware will not cause any problems
Petra
I guess you need to watch for any Pointer Operations and especially Pointer Math since a pointer that points to memory > 2GB is a negative number when treated as an integer pointer. Sometimes the Tag or Data field of an Object or Vcl component (which is an integer) is used to store a pointer. Sending pointers over in Windows Messages (Post/SendMessage) also uses an integer as lParam and wParam. There's probably a lot more things to pay attention to...
Remko
"internally 32 bit pointer are 64 bit pointers in x64 windows"? A 32 bit applications will still use 32 bit long pointers in its virtual address space - Windows will translate them to 64 bit physical addresses (as DOS uses 20 bit physical addresses, and PAE uses 36 bit physical addresses on 32 bit machines). 64 bit applications uses 64 bit long virtual addresses mapped to 64 bit physical addresses. A 64 bit application use more memory to store its pointers (and other data types).
ldsandon
@ldsandon: I meant that as far as windows is concerned there is such thing as a 32 bit pointer. Within a 32 bit application a pointer type is of course 4 bytes and not 8 but I think the extra memory for variables is not significant assuming one doesn't use millions of them.
Remko
That depends on the type of applications. Code intensive ones with relatively small data structure won't see much larger memory use, data intensive ones using large pointer-based structure will see it, especially if the pointer/data size ratio is somehow near 1. For example each TList will double its memory use. It is true for most application it would be barely noticeable, but it is true that 64 bit applications use some more memory than 32 bit ones.
ldsandon
+1  A: 

64 bit applications use some more memory than 32 bit ones, because some data types will be 64 bit long instead of 32 (not only pointers - and that depends on what datatypes your applications uses and compiler standard types). Also some instructions may requires more bytes to be coded. As UTF-16 strings require more space than ANSI ones. Of course if you have to manipulate large 64 bit structures is not a good idea to use a 64 bit machine with 4GB RAM only. One of the reason to use a 64 bit OS is to manage more than 4GB without using any pagination trick.

ldsandon
+1. (some instructions could also requires less bytes/opcodes because x64 has only one calling convention and more registers)
Remko
But it also have stricter alignment needs...
ldsandon