Reputation: 361
I'm experiencing an unexpected behaviour while running some very simple code.
First of all I'm using Visual Studio 2015 on a i7-4770 CPU, 32Gb memory (22,6 free)
My sample code:
int length = 10;
for (int i = 0; i < length; i++)
{
int j = i;
//ThreadPool.QueueUserWorkItem(ThreadProc_CLR, j);
Task.Factory.StartNew(() => { ThreadProc_CLR(j); });
}
public void ThreadProc_CLR(object parameter)
{
int i = Convert.ToInt32(parameter);
byte[] data = new byte[1000000000];
new Random().NextBytes(data);
System.Security.Cryptography.SHA1.Create().ComputeHash(data);
}
What I do not understand is why if I run my code using
I get a System.OutOfMemoryException after allocating the byte[] buffer for the 3rd or 4th time
If I uncheck "Prefer 32-bit" everything works smoothly. I've googled around looking for any docs explaining possible limitations but I haven't found any.
Upvotes: 0
Views: 818
Reputation: 27861
It seems that you are working on a 64-bit OS.
When you compile with "Any CPU" and the "Prefer 32-bit" setting is turned on. Then the process will execute as 32-bit process even if the OS is 64-bit.
32-bit processes can have a virtual address space of size 2GB (can be 4GB using the LARGEADDRESSAWARE setting). This is a lot less than what you need for your application to run. Your application seems to allocate about 10GB, and the cryptography API is also going to consume some memory.
When you uncheck the "Prefer 32-bit" setting, the process will run as a 64-bit process. This gives you a virtual address space of 8 TB.
Upvotes: 2
Reputation: 120380
You're not disposing of your HashAlgorithm instances, so they're probably sitting around consuming memory.
//...
using(var ha = System.Security.Cryptography.SHA1.Create())
{
ha.ComputeHash(data);
}
Upvotes: 0