Why does 32-bit application use less RAM than 64-bit version, even though 32-bit executable is larger - .net-4.0

I have created an application in .NET. When I compile a 64bit version and a 32bit version of the same software, the 64bit executable is smaller.
However, when you run them both, the 64bit version uses more RAM.
I'm sure something is happening "under the hood", and was just interested why? (It's not a worry either way)
Thanks.
EDIT: C#.NET 4.0 if it matters.

In 32 bit applications, pointers are 32 bits i.e. 4 bytes, whereas they are 64 bits i.e. 8 bytes in 64 bit applications. So pointers (e.g. object reference) take up twice as much memory.
Also, in 32-bit applications objects have an overhead of 12 bytes per object, whereas in 64 applications they have an overhead of 24 bytes. Double again.
These affects will be noticed at runtime, not in the dll size.

Pointers are twice as big in 64bit mode. That could explain some (sometimes much) of the RAM usage difference.

Related

Why does the amount of memory available to x86 applications fluctuate in vb.net? [duplicate]

Which is the maximum amount of memory one can achieve in .NET managed code? Does it depend on the actual architecture (32/64 bits)?
There are no hard, exact figure for .NET code.
If you run on 32 bit Windows; your process can address up to 2 GB, 3 GB if the /3GB switch is used on Windows Server 2003.
If you run a 64 bit process on a 64 bit box your process can address up to 8 TB of address space, if that much RAM is present.
This is not the whole story however, since the CLR takes some overhead for each process. At the same time, .NET will try to allocate new memory in chunks; and if the address space is fragmented, that might mean that you cannot allocate more memory, even though some are available.
In C# 2.0 and 3.0 there is also a 2G limit on the size of a single object in managed code.
The amount of memory your .NET process can address depends both on whether it is running on a 32/64 bit machine and whether or not it it running as a CPU agnostic or CPU specific process.
By default a .NET process is CPU agnostic so it will run with the process type that is natural to the version of Windows. In 64 bit it will be a 64 bit process, and in 32 bit it will be a 32 bit process. You can force a .NET process though to target a particular CPU and say make it run as a 32 bit process on a 64 bit machine.
If you exclude the large address aware setting, the following are the various breakdowns
32 bit process can address 2GB
64 bit process can address 8TB
Here is a link to the full breakdown of addressable space based on the various options Windows provides.
http://msdn.microsoft.com/en-us/library/aa366778.aspx
For 64 bit Windows the virtual memory size is 16 TB divided equally between user and kernel mode, so user processes can address 8 TB (8192 GB). That is less than the entire 16 EB space addressable by 64 bits, but it is still a whole lot more than what we're used to with 32 bits.
I have recently been doing extensive profiling around memory limits in .NET on a 32bit process. We all get bombarded by the idea that we can allocate up to 2.4GB (2^31) in a .NET application but unfortuneately this is not true :(. The application process has that much space to use and the operating system does a great job managing it for us, however, .NET itself seems to have its own overhead which accounts for aproximately 600-800MB for typical real world applications that push the memory limit. This means that as soon as you allocate an array of integers that takes about 1.4GB, you should expect to see an OutOfMemoryException().
Obviously in 64bit, this limit occurs way later (let's chat in 5 years :)), but the general size of everything in memory also grows (I am finding it's ~1.7 to ~2 times) because of the increased word size.
What I know for sure is that the Virtual Memory idea from the operating system definitely does NOT give you virtually endless allocation space within one process. It is only there so that the full 2.4GB is addressable to all the (many) applications running at one time.
I hope this insight helps somewhat.
I originally answered something related here (I am still a newby so am not sure how I am supposed to do these links):
Is there a memory limit for a single .NET process
The .NET runtime can allocate all the free memory available for user-mode programs in its host. Mind that it doesn't mean that all of that memory will be dedicated to your program, as some (relatively small) portions will be dedicated to internal CLR data structures.
In 32 bit systems, assuming a 4GB or more setup (even if PAE is enabled), you should be able to get at the very most roughly 2GB allocated to your application. On 64 bit systems you should be able to get 1TB. For more information concerning windows memory limits, please review this page.
Every figure mentioned there has to be divided by 2, as windows reserves the higher half of the address space for usage by code running in kernel mode (ring 0).
Also, please mind that whenever for a 32 bit system the limit exceeds 4GB, use of PAE is implied, and thus you still can't really exceed the 2GB limit unless the OS supports 4gt, in which case you can reach up to 3GB.
Yes, in a 32 bits environment you are limited to a 4GB address-space but Windows claims about half. On a 64 bits architecture it is, well, a lot bigger. I believe it's 4G * 4G
And on the Compact Framework it usually is in the order of a few hundred MB
I think other answers being quite naive, in real world after 2GB of memory consumption your application will behave really badly. In my experience GUIs generally go massively clunky, unsusable after lots of memory consumptions.
This was my experience, obviously actual cause of this can be objects grows too big so all operations on those objects takes too much time.
The following blog post has detailed findings on x86 and x64 max memory. It also has a small tool (source available) which allows easy easting of the different memory options:
http://www.guylangston.net/blog/Article/MaxMemory.

MS Access 32-bit or 64-bit

I have created an Access 2016, 32-bit application that is about to be deployed. Every user will access the application through a RDP connection and then onto a MS Server 2012 R2 server (I doubt if it will ever be installed on any other platforms). As far as each user is concerned there will be nothing on the server, except the Access database (using an Access runtime license), and each machine will be pretty much locked down.
All of the libraries used in the Access application are available in either 32-bit or 64-bit formats, plus the back-end databases are usually small (typically between 10MB and 200MB). That said the compiled Access front-end is about 18MB and can run some rather complex functions/calculations (but nothing on a scientific level).
So finally, my question. Would there be any real benefit in moving to a 64-bit version (in either the short or long-term)?
Short answer: No. 64 bit version doesn't give any useful advantages, but you will have some additional problems with compatibility.
I would say this answer strongly depends on how many RAM your application will need. Because 32 bit is limited to 2 GB or something I think.
So this is not a real question of benefits or performance but of not running into any RAM issues if the amount fo data increases I think.
See also Choose between the 64-bit or 32-bit version of Office.
Besides that I see no advantages in choosing 64 bit.

Intellij idea 32 bit vs 64 bit speed and performance

Currently I am using intellij idea 14.0.3(earlier I was using 12.1.4) on 64 bit windows 8.1.
When we install it, the installer creates the shortcut in start menu and other places which defaults to the 32 bit .exe file even on a 64 bit system.
I know that I can use the 64 bit executable to run idea in 64 bit mode as given in this SO answer.
But is there any significant performance difference between the two versions of the IDE?
And which executable is recommended for 64 bit systems? Shall I keep using 32 bit? or shall I switch to 64bit version?
The difference between running the 32 and 64 bits launcher is which Java will be used to start the IDE and what are the vmoptions parameters passed to it.
When starting the 32 bit one, IDEA uses it's own bundled 32 bit JRE. If there is no such one, IDEA tries to find 32 bit JRE in several places on specific order (%IDEA_HOME%, %JDK_HOME%, %JAVA_HOME%). The values in idea.exe.vmoptions are passed to it.
When starting the 64 bit one, it tries to find 64 bit JRE in several places on specific order. The values in idea64.exe.vmoptions are passed to it.
So if you want to allocate 2 GB RAM or more (with -xmx), this is not going to happen with 32 bit Java (resp. IDEA). And for large projects using less than 2GB causes the IDE to hang a lot. For smaller projects I don't think you'll feel any difference.
For reference this is the bug about this, so far they are not acting on it:
https://youtrack.jetbrains.com/issue/IDEA-146040

Can a 32bit process access 64GB memory?

I've a strange situation: A server, containing 64GB of memory, runs a SQL server process (64 bit) which consumes 32 GB of memory. There is about 17 GB memory available.
MS Dynamics Nav is running on top of SQL
Besides the 64bit SQL process, there is another SQL process and a NAS, both running 32 bits.
Every now and then, an error message is logged in the eventviewer, saying
There is not enough memory to execute this function.
If you work in a single-user installation, you can try reducing the
value of the 'cache' program property. You can find information about
how to optimize the operating system in the documentation for yo
Now I'm wondering what the problem is, since there is still 17 GB memory available. Is it possible that a 32-bit process cannot allocate memory in the last segment (60 to 64 GB)?
32 bit processes are limited to about 4 GB of memory usage. The x64 architecture should allow a 32bit process to run in any of the available memory space, but your 32bit process will still be limited by it's maximum addressible space (~4GB).

what is meant by 32-bit application?

I am not sure what is meant by 16-bit or 32-bit applications. Is that a 16-bit application is an application which would not require more than 2^16 bytes of memory space? Does this 16-bit refers to the max size of the application?
It means the application has been compiled for a processor that has 16 bits of memory addressing or 32 bit of memory addressing. Same goes for 64 bit applications.
The number refers to the maximum amount of memory that the application can address.
See wikipedia - 16-bit, 32-bit, 64-bit (and more).
A 32-bit application is software that runs in a 32-bit flat address space.
Answers to common questions
Will a 64 bit CPU run a standard (32-bit) program on a 64-bit version of an OS?
Yes it will. 64 bit systems are backward compatible with the 32 bit counterparts.
Will a 64-bit OS run a standard application on a 64 bit processor?
Again, it will. This is because of backward compatibility.
Can I run W2K and WXP on an 64 bit CPU, and use old software?
Yes, a 32 bit OS (W2K and WXP) will run on a 64 bit processor. Also, you should be able to run "old software" on a 64 bit OS.
The number(32 or 16 of the assembler directive of the addressmode (example "[use16]" and "[use32]")) does not refers to the maximum amount of memory that the application can address!
Because with the 80386+ it is also possible to use operandsize- and adresssize prefixes in combination with the 16 bit PM for to address up to 4 GB of ram.
(The maximum amount of memory that our application can be use is refering to the segment entries of the segmentsize inside of a GDT/LDT selector, or by the default size for a segment of 64 kb.)
The only one differnce between the 32 bit - and the 16 bit addressmode is the meaning and the usage of those operandsize- and addresssize prefixes.
[use16]
So if we want to use in the 16 bit addressmode 32 bit operands/addresses, then we have to add those prefixes to our opcode. Without those prefixes we can only use 16 bit.
[use32]
In the 32 bit addressmode we found a diametrical opposite situation, so if we want to use 32 bit operands/addresses, then we have to leave out those prefixes from our opcode and only if we want to use 16 operand/addresses, then we have to add those prefixes to our opcode.
If we use these size-directives above(or similar notation) carefully, then our assembler will do this job.
Operand size prefix in 16-bit mode
Dirk