Kai,我提供了一个程序,该程序可以使用线程来完成您想做的事情。它是根据以下条款许可的:您必须为运行它的每个CPU内核每小时向我支付$ 0.0001。费用在每个日历月末支付。请尽快与我联系以获取我的Paypal帐户详细信息。
using System;
using System.Collections.Generic;
using System.Linq;
namespace GuidCollisionDetector
{
class Program
{
static void Main(string[] args)
{
//var reserveSomeRam = new byte[1024 * 1024 * 100]; // This indeed has no effect.
Console.WriteLine("{0:u} - Building a bigHeapOGuids.", DateTime.Now);
// Fill up memory with guids.
var bigHeapOGuids = new HashSet<Guid>();
try
{
do
{
bigHeapOGuids.Add(Guid.NewGuid());
} while (true);
}
catch (OutOfMemoryException)
{
// Release the ram we allocated up front.
// Actually, these are pointless too.
//GC.KeepAlive(reserveSomeRam);
//GC.Collect();
}
Console.WriteLine("{0:u} - Built bigHeapOGuids, contains {1} of them.", DateTime.Now, bigHeapOGuids.LongCount());
// Spool up some threads to keep checking if there's a match.
// Keep running until the heat death of the universe.
for (long k = 0; k < Int64.MaxValue; k++)
{
for (long j = 0; j < Int64.MaxValue; j++)
{
Console.WriteLine("{0:u} - Looking for collisions with {1} thread(s)....", DateTime.Now, Environment.ProcessorCount);
System.Threading.Tasks.Parallel.For(0, Int32.MaxValue, (i) =>
{
if (bigHeapOGuids.Contains(Guid.NewGuid()))
throw new ApplicationException("Guids collided! Oh my gosh!");
}
);
Console.WriteLine("{0:u} - That was another {1} attempts without a collision.", DateTime.Now, ((long)Int32.MaxValue) * Environment.ProcessorCount);
}
}
Console.WriteLine("Umm... why hasn't the universe ended yet?");
}
}
}
PS:我想尝试一下Parallel扩展库。那很简单。
使用OutOfMemoryException作为控制流只是感觉不对。
编辑
好吧,看来这仍然吸引了选票。因此,我已经修复了GC.KeepAlive()问题。并将其更改为与C#4一起运行。
为了阐明我的支持条款:仅在2010年2月28日提供支持。请仅使用时间机器在当天发出支持请求。
编辑2
与往常一样,GC在管理内存方面比我做得更好。我自己以前做过的任何尝试都注定要失败。