# Wednesday, 11 April 2007
« Memory Mapped Files | Main | Running Application Test Suites »
Detecting .NET 2.0 x64

In the category .NET trivia. I found a weird difference between .NET 2.0 x86 and x64. This code detects that it runs on x64 (at least the .NET 2.0 build included with Vista x64):

WeakReference r = new WeakReference(null);
try { throw new Exception(); }
catch (Exception x) { r.Target = x; }
if (r.Target != null)
  Console.WriteLine("Running on x64");

It appears that the last thrown exception is stored in a global (or rather probably thread local) variable and hence not garbage collectable until the next exception is thrown...

Update: I thought this was obvious, but since two commenters have felt the need to point out that you shouldn't use this in production, I'll say it explicitly:I was just pointing out some obscure implementation difference (arguably a bug), do not use this code in production.

Wednesday, 11 April 2007 15:31:01 (W. Europe Daylight Time, UTC+02:00)  #    Comments [3]
Wednesday, 11 April 2007 17:08:56 (W. Europe Daylight Time, UTC+02:00)
I think that it should be a bug in x64 implementation. What happends with this code in Mono x86 and x64?
Thursday, 12 April 2007 07:59:15 (W. Europe Daylight Time, UTC+02:00)
Incase anyone ever googles this page looking for X64 detection, the 'sane' way to detect if you are running on X64 ofcourse would be

if (IntPtr.Size == 8)
Console.WriteLine("Running on x64");
Console.WriteLine("Running on x86");
Thursday, 12 April 2007 20:06:47 (W. Europe Daylight Time, UTC+02:00)
I think that checking size of pointer is better method.

- It is not depened on hack
- It's faster (exceptions are usually expensive).
Home page

I apologize for the lameness of this, but the comment spam was driving me nuts. In order to be able to post a comment, you need to answer a simple question. Hopefully this question is easy enough not to annoy serious commenters, but hard enough to keep the spammers away.

Anti-Spam Question: What method on java.lang.System returns an object's original hashcode (i.e. the one that would be returned by java.lang.Object.hashCode() if it wasn't overridden)? (case is significant)

Comment (HTML not allowed)  

Live Comment Preview