Category Archives: Code fun

Memory Management in .Net


Warning: Use of undefined constant user_level - assumed 'user_level' (this will throw an Error in a future version of PHP) in /var/www/lybecker.com/public_html/blog/wp-content/plugins/ultimate-google-analytics/ultimate_ga.php on line 524

I’ve written about Garbage Collection in the .Net Framework in version 2.0 and 3.0 a couple of years ago, but now Red Gate has created a simple and easy to understand funny comic “Memory Management in .Net”

Memory Management in .Net comic

Download the full one-page comic.

The .Net Framework 4.0 provides the new default behavior of background garbage collection.

Java 4-ever


Warning: Use of undefined constant user_level - assumed 'user_level' (this will throw an Error in a future version of PHP) in /var/www/lybecker.com/public_html/blog/wp-content/plugins/ultimate-google-analytics/ultimate_ga.php on line 524

I find this video hilarious…

You should use the best tools at hand to solve the problem. That said; choosing between Java or .Net doesn’t really matter in most cases. There are however some areas where Java is a better choice and vice versa.

I can’t wait to see it in the cinema 🙂

PS. I do develop with Java even though I do not blog much about it.

Update: YouTube removed the video due to copyright claims. You can still see it JavaZone.

Monitors and thread context


Warning: Use of undefined constant user_level - assumed 'user_level' (this will throw an Error in a future version of PHP) in /var/www/lybecker.com/public_html/blog/wp-content/plugins/ultimate-google-analytics/ultimate_ga.php on line 524

Running the below code will fail – why?


var syncRoot = new object();

Monitor.Enter(syncRoot);

ThreadPool.QueueUserWorkItem(x => Monitor.Exit(syncRoot));

It will throw a SynchronizationLockException with the message “Object synchronization method was called from an unsynchronized block of code.”

It is because System.Threading.Monitor requires the Enter and Exit methods must be executed on the same thread for the same synchronization object.

I did not know that :-/

Loooong running sql statement


Warning: Use of undefined constant user_level - assumed 'user_level' (this will throw an Error in a future version of PHP) in /var/www/lybecker.com/public_html/blog/wp-content/plugins/ultimate-google-analytics/ultimate_ga.php on line 524

Sql Server Profiler renegate statement

I was using SQL Server Profiler on a SQL Server 2005 Enterprise Edition looking for performance culprits, when I stumbled upon this very long running process.

I think this must be a world record. 🙂

This statement has only used 16 milliseconds of CPU but it has been running for more than 500.000 years!

When is DivideByZeroException thrown?


Warning: Use of undefined constant user_level - assumed 'user_level' (this will throw an Error in a future version of PHP) in /var/www/lybecker.com/public_html/blog/wp-content/plugins/ultimate-google-analytics/ultimate_ga.php on line 524

What a strange question most developers will say. DivideByZeroException
is thrown every time you divide by zero, right? No – it depends.

Let’s try – regular division with integers.

int i1 = 4;
int i2 = 0;

try
{
Console.WriteLine(i1 / i2);

}
catch (System.DivideByZeroException)
{
Console.WriteLine("DivideByZeroException");
}

Hmm, I see a DivideByZeroException coming, and that’s
absolutely correct. So what is all the fuzz about?

Let’s try again, but this time with floating point numbers.

double d1 = 4.0;
double d2 = 0.0;

try
{
Console.WriteLine(d1 / d2);

}
catch (System.DivideByZeroException)
{
Console.WriteLine("DivideByZeroException");
}

If you try to run this piece of code, no exception is
thrown. Instead INF (short for infinite) is returned. To be more precise a PositiveInfinity,
due to the decimalNumerator is positive.

I read this somewhere and had to test it. I even asked
my colleagues when DivideByZeroException is thrown and showed them these code examples.
None of them knew the right answer. So it’s not only me, whom is ignorant. 🙂