comments edit

Answer: When you don’t have enough change for the phone booth.

I’ll be here all week, thank you very much. Bad pun notwithstanding, the answer to this question is pretty much never (see Rico’s almost rule #1). The Garbage Collector in .NET is like a highly motivated and skilled employee. If you quit being a micro-manager (“You forgot to put the cover sheet on the TPS report”) and stop looking over its shoulder, it’s able to just do its job and perform quite well.

However, note that Rico says “Almost Rule #1”. That must mean there are appropriate exceptions to the rule, no matter how few they may be. What are those situations? The reason I ask is I ran into the following code on the net (dramatization):

/// 
/// Stops the socket server and closes 
/// every client connection.
/// 
public void Stop()
{
    if(_isDisposed)
        throw new ObjectDisposedException(
            "SocketServer", 
            "Object is already disposed.");

    CloseConnectedClients();
    CloseListener();

    GC.Collect();
    GC.WaitForPendingFinalizers();
}

This is the Stop() method of your typical Socket Server. It closes any connected socket clients and then closes the listening thread. After that, it calls GC.Collect() and GC.WaitForPendingFinalizers(), violating Rico’s almost rule. Is this perhaps one of those appropriate times to call GC.Collect()?

Typically, your socket server will have been running for a long time, so it is very likely it will have been promoted to Generation 2 and contain references to a several other Generation 2 objects. Rico points out that

If your algorithm is regularly producing objects that live to gen2 and then die shortly thereafter, you’re going to find that the percent time spent in GC goes way up. Forcing more of these collects is really the last thing you wanted to do (assuming you could, note again GC.Collect() doesn’t promise to do a gen2 collect).

However, this is situation is different in that the server has been around a while and calling Stop() on a server typically means you’re not planning to use the Server any time soon afterwards. In fact, you’re most likely about to dispose of it.

Given that, It seems to me that this might be one of those cases where calling GC.Collect() is appropriate. The goal here is a one time Generation 2 collection. Of course, there’s no guarantee that a Generation 2 collect will occur. Maybe this is a situation where it makes no difference either way. Any thoughts?

For more reading:\ Garbage Collector Basics and Performance Hints\ Programming For Garbage Collection

comments edit

I’ve heard a lot of complaints about what a b*tch moderating comments turns out to be. So why not create an API for moderating comments?

Suppose your blog engine put all incoming comments in a private authenticated RSS feed. You can then subscribe to this feed and for each item, hit “YES” or “NO” via your RSS Aggregator. I’d be willing to write an IBlogThis plug-in to support such an API if someone adds it to the various blogging back-ends. If it caught on, I’d be happy to add it to RSS Bandit if Dare and Torsten like the idea and approved it.

The API would probably be similar to the CommentAPI or my RatingAPI, but with a few modifications specific to comments. I’ll propose one later.

comments edit

BasketballBasketball was ugly this morning. Lots of bickering and fouling. Dan, you didn’t miss much man. Sometimes you just want to slap everyone upside the head and tell them to grow up and graduate from Pre-School. But that’s insulting to pre-schoolers who are much more mature than the fellas this morning. Then again, sometimes you wish you’d just make the damn game winning shot instead of clanking it in and out.

comments edit

One pet peeve I have is how the auto completion works regarding methods. I know this is nitpicky, but I’m wondering how other IDE’s that try to help you handle this. Suppose I type out the following and am about to hit the open parenthesis character.

public void SomeMethod

Resharper automatically adds a closing parenthesis and puts your cursor in between them.

public void SomeMethod(Cursor Is Here)

Now the reason I don’t like this is after I’m done typing the arguments, I still have to type a character (either a closing parenthesis which it absorbs or a right arrow key) and then type my open brace. I’d prefer it if it didn’t add the closing parenthesis and instead, when I did close the parenthesis, it would automatically add the open and close brace. Thus the sequence would look like:

public void SomeMethod(object someParam 
[about to type closing parens]

to

public void SomeMethod(object someParam)
{
    Braces added automatically and Cursor is Here
}

This makes more sense to me as this is where I’m going to be spending more of my time. In any case, if I’m just being a whiner, I can live with that. I haven’t played enough with Whidbey or other IDE enhancements to know how they handle this situation. It’s the little things that count.

comments edit

Patterns and Practices I’m trying to convince my coworkers that we should use the Isolated development mode for ASP.NET projects as described in the Patterns and Practices document Team Development with Visual Studio .NET and Visual SourceSafe. They’ve agreed to read the document and we’ll meet to discuss. I hope the document convinces them, because I’ve had too many headaches in the past dealing with FrontPage extensions.

My question to you all out there is do you set up your own development environment according to the guidelines set out in this document? How is it working for you or how have you changed it?

code comments edit

Ian Griffiths finds a niggle about my post on sockets.

This may surprise a few friends of mine who regard me as a “human dictionary”, but I had to look up the word “niggle”. Apparently only the “human” part of the appellation applies. I’ve apparently fooled them by reading a lot of sci-fi fantasy and choosing to learn and use “impressive” words such as Bacchanalian in everyday conversation (“I wrote this code in a drunken stupor from a bacchanalian display of excessive beer drinking.”). It’s really all smoke and mirrors. But I digress…

His comment is quite insightful and well worth repeating here in full.

One minor niggle with this code… \ \ Although the example is correct as it stands, it doesn’t mention an important issue: the Socket class is not thread-safe. This means that if you do use the async operations (and by the way, I’m completely with you here - I’m a big fan of the async operations) you need to take steps to synchronize access to the socket. \ \ As it stands there’s nothing wrong with this example as far as I can see. But what if you also have an asynchronous read operation outstanding? Can you guarantee that a read and a send won’t complete simultaneously, and that you’ll be trying to access the socket from both completion handlers simultaneously. \ \ So in practice, you tend to want to use some kind of locking to guarantee that your socket is only being used from one thread at a time, once you start using async socket IO. \ \ (Also, you left out one of the clever parts of IO completion ports - the scheduler tracks which threads are associated with work from an IO port, and tries to make sure that you have exactly as many running as you have CPUs. If one of the threads handling work from an IO completion port blocks, the OS will release another work item from the completion port. Conversely, if loads of IO operations complete simultaneously, it only lets them out of the completion port as fast as your system can handle them, and no faster - this avoids swamping the scheduler under high load.)

I have to say, Ian’s depth of knowledge on such topics (or nearly any geek topic) never ceases to impress me. Fortunately for my app, the client socket only receives data every three seconds and never sends data back to the remotely connected socket (how boring, I know). In any case, I will double check that I am synchronizing access to the socket just in case. Perhaps I’ll use the TimedLock to do that. ;)

While we’re in the business of finding niggles (Ian, you’ve hooked me on this word. For some strange reason, I can’t stop saying it) I should also point out that IO Completion ports awaken threads from the ThreadPool in order to perform an asynchronouse action. The entire asynchronous invocation model of .NET is built on the ThreadPool. Remember that the next time you call a method that starts with “Begin” such as “BeginInvoke”. Chances are, it’s using a thread from the ThreadPool (especially if its a framework method. I’ll make no guarantees for methods written by your coworkers.)”

By default, the max threadcount for the ThreadPool is 25 per processor. In my application, the remote socket sends short packets of data on a regular interval, so the threads that handle the received data are very short lived. Sounds like an ideal use of the ThreadPool doesn’t it? However, if I were expecting a huge number of simultaneous connections, I might look into changing the machine.config file to support more than 25 ThreadPool threads per processor. Before making any such change, measure measure measure.

If you have a situation where the operations on the data are long lived, you might consider spawning a full-fledged thread to handle the remote client communications and operations. Long running operations aren’t necessarily the best place to use a thread from the .NET built in ThreadPool.

comments edit

Resharper After hearing so much about how IntelliJ IDEA is the ideal IDE and blows every other IDE to smithereens from various Java pundits (and even a friend who’s a C# and Java developer), I was excited to try out Resharper 1.0.2 from Jetbrains, the maker of IDEA.

Installation went smoothly and I really am impressed with all the productivity enhancements contained within this tool. Rather than wait for Whidbey, I can have code expansions and refactoring NOW! The code formatting feature will surely help your team avoid coming to blows over such important and divisive issues such as “curly brace on its own line or after a method declaration?”. Everyone can have the code formatted to his or her own tastes. There’s a whole slew of other noteworthy features I won’t go into.

The one problem I have with ReSharper, and this is a doozy, is that it replaces the VS.NET Intellisense with its own, and its implementation way too slow for real development. Note that I’m running a fairly decent machine: Pentium 4 1.7 GHZ 512 MB of RAM. One thing I appreciate about VS.NET’s Intellisense is that it is snappy and can keep up with me. So it’s a huge detriment when I’m typing along and hit “.” and have to wait a second for the auto-completion box to display. I hope there’s a way to disable the auto-completion, but maintain all the other great features. Otherwise I’m afraid I will have to uninstall this.

Anyone else have good or bad experiences with this product? Any recommendations?

UPDATE: I couldn’t take it any longer. I had to uninstall this. It’s a shame

personal comments edit

No, I’m not talking about Pizza from Chicago. I’m talking about this dynamic duo:

Deep Dish

As you can see at the bottom, I’m listening to their remix of Dido’s Thank You which won a Grammy. These guys are one of my favorite electronica bands out there. They’re much more in the deep funky progressive house area. Somewhat dark, without being too malicious. I highly recommend you check them out. Especially this Dido remix.

[Listening to: Dido . Thank you - Deep Dish - Global Underground 021 - Moscow CD1 (7:25)]

comments edit

The path of least resistance when writing threading code as well as socket communications is to use techniques that cause indefinite blocking of some sort. Personally, I prefer never to block indefinitely. For example, it’s quite common to see code such as:

lock(someObject)
{
    //Do Something here...
}

Nothing wrong with this inherently, but this piece will try to acquire a lock on someObject indefinitely. Imagine if you mistakenly had code like (yes, it’s a bit contrived)

using System.Threading;

//... other stuff ...

object someObject = new Object();
object someOtherObject = new object();

public void LockItUp()
{
    lock(someObject)
    {
        Console.WriteLine("Lock 1 acquired.");

        ManualResetEvent mre = 
                new ManualResetEvent(false);
        WaitCallback callback = 
                new WaitCallback(ThrowAwayTheKey);        

        ThreadPool.QueueUserWorkItem(callback, mre);
        
        // wait till other thread has lock on 
        // someOtherObject
        mre.WaitOne(); 

        lock(someOtherObject)
        {
            Console.WriteLine("I never get called.");
        }
    }
}

void ThrowAwayTheKey(object resetEvent)
{
    lock(someOtherObject)
    {
        Console.WriteLine("Lock 2 acquired.");
        ManualResetEvent mre = 
                resetEvent as ManualResetEvent;
        if(mre != null)
            mre.Set(); //original thread can continue.

        lock(someObject)
        {
            Console.WriteLine("Neither do I");
        }
    }
}

Calling the method LockItUp will cause a deadlock and the application will hang until you kill it. Although this example is a bit contrived, you’d be surprised how easy it is in a sufficiently large and complicated system with multiple developers for you to run into this situation in a more roundabout manner. I see this often enough because using the lock statement is the path of least resistance. Instead, try using the TimedLock struct.

Another situation this type of thing comes up is with socket programming. Often I see code like this:

using System.Net.Sockets;

//... other stuff ...

byte[] _buffer = new byte[4096];
public void Listen(Socket socket)
{
    int bytesRead 
        = socket.Receive(_buffer, 0, 4096
                , SocketFlags.None);

    //You're sitting here all day.
}

If the remote socket isn’t forthcoming with that data, you’re going to be sitting there all day holding that thread open. In order to stop that socket, you’ll need another thread to call Shutdown or close on the socket. Contrast that with this approach:

byte[] _buffer = new byte[4096];
public void BeginListen(Socket socket)
{
    socket.BeginReceive(_buffer, 0, 4096
            , SocketFlags.None
            , new AsyncCallback(OnDataReceived)
            , socket);
    //returns immediately.
}

void OnDataReceived(IAsyncResult ar)
{
    Socket socket = ar.AsyncState as Socket;
    int bytesRead = socket.EndReceive(ar);
    
    //go on with your bad self...
}

BeginListen returns immediately and OnDataReceived isn’t called until there’s actual data to receive. An added benefit is that you’re not taking up a thread from the ThreadPool, but rather you’re using IO completion ports. IO Completion ports is a method Windows uses for asynchronous IO operations. When an asynchronous IO is complete, Windows will awaken and notify your thread. The IO operations run on a pool of kernel threads whose only task in life is to process I/O requests.

Since BeginListen returns immediately, you’re free to close the socket if no data is received after a certain time or in response to some other event. This may be a matter of preference, but this is a more elegant and scalable approach to sockets.

For more on asynchronous sockets, take the time to read Using an Asynchronous Server Socket and related articles.

comments edit

This seems to be my favorite geek subject, but I have to tell you a success story using Ian Griffith’s Timed Lock struct with my enhancement.

To recap, when you fail to acquire a lock on an object because another thread already has one, my enhancement allows you to see the stack trace of the blocking thread. Well the other day, I was running a suite of unit tests against a socket server I was building when one of the tests failed with a ThreadTimeoutException. Looking at the stack trace, I found the line of code where another thread was unnecessarily holding a lock on the object. I used to spend a lot of time poring through logs trying to decipher threading issues such as this.

comments edit

There are by some estimates more than a million weblogs. But most of them get no visibility in search engines. Only a few “A-List” blogs get into the top search engine results for a given topic, while the majority of blogs just don’t get noticed. The reason is that the smaller blogs don’t have enough links pointing to them. But this posting could solve that. Let’s help the smaller blogs get more visibility!

This posting is GoMeme 4.0. It is part of an experiment to see if we can create a blog posting that helps 1000’s of blogs get higher rankings in Google. So far we have tried 3 earlier variations. Our first test, GoMeme 1.0, spread to nearly 740 blogs in 2.5 days. This new version 4.0 is shorter, simpler, and fits more easily into your blog.

Why are we doing this? We want to help thousands of blogs get more visibility in Google and other search engines. How does it work? Just follow the instructions below to re-post this meme in your blog and add your URL to the end of the Path List below. As the meme spreads onwards from your blog, so will your URL. Later, when your blog is indexed by search engines, they will see the links pointing to your blog from all the downstream blogs that got this via you, which will cause them to rank your blog higher in search results. Everyone in the Path List below benefits in a similar way as this meme spreads. Try it!

Instructions: Just copy this entire post and paste it into your blog. Then add your URL to the end of the path list below, and pass it on! (Make sure you add your URLs as live links or HTML code to the Path List below.)

Path List\

  1. Minding the Planet\
  2. Luke Hutteman’s public virtual MemoryStream\
  3. JayBaz_MS blog\
  4. You’ve Been Haacked! and you like it.\
  5. (your URL goes here! But first, please copy this line and move it down to the next line for the next person).

\ (NOTE: Be sure you paste live links for the Path List or use HTML code.)\

[Via Luke Hutteman’s public virtual MemoryStream]

comments edit

Dave Winer writes:

There’s something missing from Windows. An application that hooks into the outbound Internet message flow, and shows me where messages are going. This would allow me to figure out what spyware is running on my system even if the various utilities can’t get rid of them. Then the next step would be to allow me to block traffic to certain servers. That would disable the spyware. It seems that I should have control of my machine at that level.

And Jeff Sandquist responds that Windows XP SP2 can do this via the new firewall, but look again. The firewall built into XP blocks incoming traffic, not outgoing. My guess is that feature will come with SP3. For now, I recommend the free version of ZoneAlarm which is perennially considered by many to be the best desktop firewall software out there. It will notify you when an application tries to send an outgoing message to the internet and its quite easy to configure.

comments edit

Having been on one of the most beautiful hikes during our trip to Seattle, it would be a grave disservice not to post a few pictures of the hike here. For all you Microsofties out there (and other geeks in Washington state), I highly recommend getting out of the office for this hike. Bring some water and sunscreen.

The four to five mile trail begins on the Pacific Crest Trail No. 2000 (the entire Pacific Crest Trail stretches from Canada to Mexico) from Chinook pass. At the parking area, you’ll cross Route 410 by walking on a wooden overpass seen above. The two in the picture below are the friends we visited, Dan and Grace Kalish.

Beginning of the hike \ Car careens off the road while hikers wave at camera.

After a bit of hiking, through lush green forest, we came upon the first of several lakelets. The hike has an elevation gain of just 700 feet, so it’s fairly easy, though we managed to make it a challenge by taking a wrong turn and visiting a full fledged lake. The detour set us back around another half hour.

A lake \ Dan wonders how those people in the lake got so small

Akumi and I had a plane to catch that evening and we were running out of time and getting a bit worried. It took us a bit longer than we expected to get out here, and the unexpected detour cost us even more time. At that point, we decided to play it safe and return back the way we came and not complete the loop. What a terrible decision that would have been. When we returned to the main loop, another hiker told us we were exactly halfway around the loop. That led to the obvious conclusion that we should continue. What a fortuitous decision, as this trail saves all the best views for those willing to stay for act 2.

Mt. Rainier \ You’ve seen one picture perfect lake surrounded by violet wildflowers in the woods with an enormous mountain backdrop, you’ve seen them all.

Phil has a snack \ Man loves nature. Man eats nature.

Not one to let a photographic opportunity go by, we made sure to pose for a group shot with our new friend, Mt. Rainier. As big as it looks in the photos, it looked absolutely ginormous in person. It positively encroaches on us as we take this shot.

Group Shot \ That cardboard backdrop is so damn convincing!

For most of the rest of the hike, we were treated to wonderful views of the mountain. It started to get rather blase. “Yet another view of the one of the most spectacular woodland scenes I’ve ever seen. Ho hum.”

Hiking along \ Tired of my incessant demands to stop and pose, my friends pose a mutiny.

Wildflowers \ Rainier tries to hide behind some trees.

At the end, the hike ended up being reasonably vigorous due to our detour, but quite manageable over all. The next time we hike here, we plan on making it a picnic next to the lake with the view of the mountain. I highly recommend this hike.

Directions From Seattle: via the Seattle Post Intelligencer\ Go south on Interstate 5 and take the state Route 18/Auburn exit (before Tacoma). Follow the signs to Auburn and state Route 164 (take the second Auburn exit off Highway 18, not the C Street exit). Follow 164 through Enumclaw to state Route 410 and turn left. Continue east to Chinook Pass, about 44 miles from Enumclaw. At Chinook Pass, continue a short distance past the wooden overpass to a parking area on the left side of Route 410 (the Mather Memorial Parkway). This is the parking area for the PCT with stock access and portable toilets, elevation 5,040 feet. If you are beginning from Tipsoo Lake, park at the Tipsoo Lake picnic area lot and look for the signed Naches Peak Loop trailhead opposite the lake.

comments edit

Patrick Cauldwell points out that the challenge of coding a business app isn’t in writing the code, that’s easy. The challenge is in understanding business requirements, and not because developers and architects can’t understand plain English.

In my experience, I find that often, “business people” (for the lack of a better word) do not apply the same rigor to developing a business model for an application as developers do towards requirements gathering, specifications, and coding for the application.

I understand that agile methodologies are designed to help manage change, but even they can’t keep up when the business owner doesn’t even know what the requirements should be, but is spouting them out anyways. What do you do when the business owner has been involved in every step of the project, played with every prototype and interim release, but at the end says, well that’s not what I want.

For ~95% of business software, the actual code needed to implement them ends up being, if not trivial, then at least not technically challenging.  Business software projects go over budget/schedule etc. because the business rules aren’t known (to anyone) or because no one in the organization really knows what they want.  …snip…

So when people complain that projects are going way over budget/schedule/what have you, it’s almost always (in my experience) due to the fact that engineers spend 6 hours a day poring over vague and conflicting sets of documents, and 2 hours writing code.

[Via Patrick Cauldwell’s Blog]

As a developer friend of mine used to say,

This job would be great if we didn’t have clients.

Of course I prefer the paycheck.

comments edit

I’m happily married, but if you’re not, and you live in L.A., I recommend you take your dog for a walk at the Barrington Dog Park, on the east side of Barrington just south of Sunset. We were playing a game of soccer, but we couldn’t help noticing that all the talent was in the dog park area adjacent to us. Looked like a bonafide LA pickup joint.

comments edit

If you’re a lazy mofo like me, there’s one thing you’re not lazy about, getting out of having to do a lot of work. An important skill that should be on every lazy developer’s utility belt is the ability to understand requirements.

However, there’s a subtle technique to this. When I say, “understand requirements”, I don’t necessarily mean understanding what the client tells you the requirement is, but taking a moment to dig deeper and get to the real purpose of the requirement. Let me tell you a story based on a true story.

WeLikeCustomers.com has a two email systems for communicating to its clients. One system sends news letters and “Please come back” emails to clients who have opted in. The other system sends emails welcoming newly registered users as well as password reminders etc… The second system is designed to send only the most important emails so as not to get blacklisted.

Millard in Marketing decides that the second system should route emails through an email delivery service that guarantees delivery. He tells Damien the developer that only emails in a certain category should be sent through the new delivery service, and all others should be sent in the current manner.

Well Damien thinks about it and tells Millard it will take a day or so to add this routing within the second system. Currently the system only supports sending emails to one configured SMTP server. Now he has to add the ability to send some emails to another SMTP server based on certain criteria. He dives right into designing a general purpose email routing system. After a bit of time, he has the sudden thought,

“Now why the hell are they routing only a portion of emails to this new service anyways?”

So he asks Millard, who replies,

“To make it easier for you of course.”

After some discussion, Damien finds out that Millard would prefer to send all emails to the delivery service. So he changes one setting in his config file pointing to the service’s SMTP address, closes his office door, and takes a nap. Meanwhile, Millard is sending out emails complimenting Damien for going above and beyond the call of duty.

Be like Damien.

personal comments edit

UPDATE: I cannot help you move to Alaska. This post was written in 2004. If you want to move to Alaska, please don’t contact me. Try looking for information at the official Alaska homepage. Thanks!

Alaska’s Flag As a relocated Alaskan, I read Rory’s post about moving Canada to the right a bit so that Alaska could join the “lower 48” with amusement. I don’t think that idea would go over too well with Alaskans, as we…er…they take great pride in their independence, not only in spirit, but in location. Not only that, it could affect the fishing season, and you don’t mess with the fishing season in Alaska, the biggest state in the Union. That’s right Texas, the stars at night are even bigger and brighter in Alaska. Get over it, we rule you.

Alaska Map However, Alaska is not paradise on Earth (except in the summer when the sun stays out quite late drinking and carousing). Did you know they pay residents to live there? It’s a little thing called the “Permanent Fund Dividend”. The Permanent Fund is a huge investment of oil revenues in a variety of stocks and bonds. Around October of every year, the interest earned from the fund pays each resident anywhere between 1 to 2 grand a year, depending on how well the market does. Just in time for some good Christmas shopping.

Not only that, there’s no sales tax and no income tax in Alaska. Yeah. Starting to sound like a good place to live, no? Well, before you rush off to go panning for gold, keep in mind that winter lasts around 8 to 9 months of the year, so I hope you like winter sports.

comments edit

There ought to be a law that requires employers to give their charges the day off due to great weather. Back in Alaska, we’d have the day off when it dropped to -60 F with snow up to your nostrils. They call it a “snow day”. Well there ought to be a “good weather day”.

Not that I needed it, I’m on vacation fool! No, I ask for this for all you suckers trudging to work while I was kayaking away on Lake Union in Seattle. We paddled our way through a channel to Lake Washington and had fun limboing underneath the freeway. Yes, underneath. There’s a portion of the freeway directly over the lake, and portions of it are only three feet above the water. My wife and I nearly scraped our noses trying to glide underneath laying on our backs against the kayak.

Earlier today we cashed in my REI dividend buying my wife some nice sandals. Seattle has the nicest REI store I’ve ever seen, complete with a huge climbing wall. It made me nostalgic for the wilderness of Alaska. I had the sudden urge to climb mountains, wrestle bears and sleep in caves. Then again, L.A. has its own version of wilderness that’s pretty wild too.