Click here to Skip to main content
Click here to Skip to main content

If Only We’d Used ANTS Profiler Earlier...

By , 9 Nov 2007
 

Editorial Note

This article is in the Product Showcase section for our sponsors at CodeProject. These reviews are intended to provide you with information on products and services that we consider useful and of value to developers.

This is a showcase review for our sponsors at The Code Project. These reviews are intended to provide you with information on products and services that we consider useful and of value to developers.

Screenshot - CIMG1156_small.jpg

If Only We'd Used ANTS Profiler Earlier... We Would Have Had a Shot at the $2 Million Cash Prize!

My name is Bryan Cattle. I'm a graduate in Electrical Engineering from Princeton University and a member of the DARPA Grand Challenge team. The DARPA Grand Challenge program is a series of races sponsored by the US government to encourage research into autonomous vehicles. Driverless cars attempt to traverse a course without human intervention. The top prize is $2 million.

Driverless Truck Running on 10,000 lines of C# Code

Screenshot - prospect11.jpg

We were participants in the 2005 Grand Challenge, where our team advanced all the way to the national finals in Las Vegas. The final race was to navigate a 138-mile course through the Mojave desert.

Our cars used an asynchronous event-based code stack written from scratch by us. At the lowest level, it consisted of device drivers and low-level controllers for basic functions such as steering wheel position and GPS. Above that was obstacle detection. We were unique among the teams in the finals in that we used stereo vision, as opposed to scanning lasers, to detect and range obstacles. All in all, we wrote 10,000 lines of C# code to drive the cars.

Bug in the Obstacle-detection Code

In the finals, we ran for 9 miles before succumbing to a memory leak in the obstacle-detection code. Actually, most of our code is written in garbage-collected C#, so it wasn't a memory leak per se, but it wasn't until two weeks later that we discovered the true problem.

It was the closest thing to a memory leak that you can have in a "managed" language. C# manages your memory for you by watching the objects you create. When your code no longer maintains any reference to the object, it automatically gets flagged for deletion without the programmer needing to manually free the memory, as they would need to do in C or C++. Hence, in order to be allocating memory that is never freed in C#, you need to somehow be referencing an object in a way that you don't know about. Our actual bug occurred in the obstacle detection code. As we detect obstacles in each frame, we store the ones we detect. As the car moves, we call an update function on each of the obstacles that we know about, to update their position in relation to the car. Obviously, once we pass an obstacle, we don't need keep it in memory, so everything 10 feet behind the car got deleted.

Or so we thought. We kept noticing that the computer would begin to bog down after extended periods of driving. This problem was pernicious because it only showed up after 40 minutes to an hour of driving around and collecting obstacles. The computer performance would just gradually slow down until the car just simply stopped responding, usually with the gas pedal down, and would just drive off into the bush until we pulled the plug. We looked through the code on paper, literally line by line, and just couldn't for the life of us imagine what the problem was. It couldn't be the list of obstacles: right there was the line where the old obstacles got deleted. Sitting in a McDonald's the night before the competition, we still didn't know why the computer kept dying a slow death. Because we didn't know why this problem kept appearing at 40 minutes, we decided to set a timer. After 40 minutes, we would stop the car and reboot the computer to restore the performance.

On race day, we set the timer and off she went for a brilliant 9.8 mile drive. Unfortunately, our system was seeing and cataloging every bit of tumbleweed and scrub that it could find along the side of the road. Seeing far more obstacles than we'd ever seen in our controlled tests, the list blew up faster than expected and the computers died only 28 minutes in, ending our run.

ANTS Profiler Revealed the Problem

We had vacations coming up a few weeks after the race, so we left the cars in Vegas and returned, two weeks later, to investigate the problem. One of our team members downloaded the 14-day trial of ANTS Profiler and we ran it on our car's guidance code. We profiled the memory usage and saw the obstacle list blowing up. How could this be? We called "delete" on those old obstacles! To our amazement, it was only minutes before we realized that our list of detected obstacles was never getting garbage collected. Though we thought we had cleared all references to old entries in the list, because the objects were still registered as subscribers to an event, they were never getting deleted.

If Only We Had Used It Earlier...

We added one line of code to remove the event subscription and, over the next three days, we successfully ran the car for 300 miles through the Mojave desert.

ANTS Profiler helped us fix a problem in minutes that would have taken us weeks to track down. If only we'd thought of it before the competition, we would most likely have finished the entire race and had a chance at the top prize money.

Bryan Cattle, DARPA Grand Challenge team member at Princeton University

For more on the Princeton team, visit here. For more information on ANTS Profiler, visit the Red Gate website.

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

About the Author

Red Gate Software
Red Gate Software Ltd.
United Kingdom United Kingdom
Member
Organisation
4 members

Red Gate Software produces ingeniously simple tools for Microsoft technology professionals worldwide. We currently specialize in MS SQL Server, Cloud, .NET and Oracle database tools. Established in 1999, we are based in the beautiful, if rather flat, university town of Cambridge, UK

Comments and Discussions

 
You must Sign In to use this message board.
Search this forum  
    Spacing  Noise  Layout  Per page   
QuestionWhy in gods name use managed code for this?memberixup24 Dec '08 - 2:47 
What's next Airbus A390 controlled by vb.net?
 
Visual Basic Lover

GeneralResponse from Red GatememberJames.moore19 Nov '07 - 2:50 
Hi,
 
This article has obviously had quite a lot of attention over the weekend. To be clear this is a sponsored article, which we paid code project to place here, as stated at the top of the page. The content of the article was written by Bryan Cattle and any editing done by Red Gate was for style rather than content.
 
There seems to be a lot of criticism of the Princeton team, both here and on Slashdot, for missing such an “obvious” mistake. If I had written code which managed to drive a car 9.8 miles through the desert I would be incredibly proud. I feel that this vitriolic criticism of a group of undergraduates who had the gumption and the vision to go out there and create a software and hardware solution to drive a car across a desert is very disingenuous. They had a hard deadline and did an incredibly good job given the constraints on their time and budget.
 
They had tried to fix the problem they were facing and had put in a workaround so that they could compete on the day. Unfortunately, they had not used a profiler which could have shown them the underlying problem.
 
For me the story is a great illustration on why having the correct tools when programming is vital – regardless of if you are developing in C, C++, C#, SQL or any other language. At Red Gate we are committed to providing these tools and ultimately, due to the attention this article has had, I believe that we were right to publicise it and that hopefully the message that it is easier to succeed if you have the right tools is getting through.
 
Most of all I want to congratulate Bryan and the rest of the team for trying and succeeding to do something incredibly hard.
 
James

 
--
James Moore
Head of .NET
Red Gate Software Ltd
GeneralRe: Response from Red Gatememberehei19 Nov '07 - 3:22 
Certainly a great accomplishment, no argument there.
 
However, two words and the probem would've been found much faster.
 
Unit Testing
 
Big Grin | :-D
 
Of course, they would have to know to write the test that said "When the object is 10 miles back, we should delete that object and unregister it." If they had written tests and just missed that one . . . well, hindsight is 20/20.

GeneralI want to know who is the winnermembersuperisaac18 Nov '07 - 2:49 
Or which language is the winner? erlang or ocaml ?
GeneralI want to know who was the project head... [modified]memberJames R. Twine17 Nov '07 - 19:02 
   ...That thought that using a managed language was a good idea for this kind of project.  The trials and tribulations of inexperienced developers aside*, I want to know why students that are learning about developing a real-time system (driving a vehicle at speed is pretty real-time), using a platform that, IMHO, is not necessarily geared for performance, but for (code-level) safety.
 
   I shudder to think about a production system in a vehicle on the road travelling at speed, negotiating a lane change to pass while coming out of a curve, when the runtime decides that NOW is a good time to stop everything and start garbage collection.
 
   Sorry, but the chance of that happening just sends chills down my spine.  Couple that with the fact that students were allowed to go down such a path is downright horrifying.
 
   Peace!
 
* This also serves as a good example that no matter what language/platform you use, you still need to be skilled to use it correctly.  .NET may take care of a lot of the details for you, but you still need to know what you are doing.
 
-- modified at 15:06 Sunday 18th November, 2007
 
   Given the low-weight votes, I guess that it needs to be explained to the less experienced that "geared for performance" in this context implies real-time performance, as the system being developed is a real-time system.  The last time I can remember even hearing "real-time" and ".NET" in the same sentence was an ACM paper regarding a set of requirements for a real-time .NET system.
 
-=- James
Please rate this message - let me know if I helped or not!
If you think it costs a lot to do it right, just wait until you find out how much it costs to do it wrong!
Avoid driving a vehicle taller than you and remember that Professional Driver on Closed Course does not mean your Dumb Ass on a Public Road!
See DeleteFXPFiles

GeneralTwo things...memberreinux17 Nov '07 - 14:18 
1. This line says a lot:
Because we didn't know why this problem kept appearing at 40 minutes, we decided to set a timer. After 40 minutes, we would stop the car and reboot the computer to restore the performance.
 
I'll tell you guys why you lost: it's because someone on your team had a really bad case of Windoze/Winblows scapegoating. Before even being level headed enough to look at the Working Set of the process in Task Manager, decided to just blame Windows and restart the entire computer rather than just the process.
 
Happens all the time. A couple years ago my internet connection was screwing up, and my ISP insisted it was Microsoft's fault for letting Blaster loose. Months later they came to replace my modem and everything was fixed.
 
2. If I were on this team, I would have written this article regardless of whether or not Red Gate endorsed it. It's called "reflection" and it's a good way to learn from mistakes. And it's a geek's equivalent of a bar story. So stop accusing Red Gate of "advertising" as if it's some sort of crime.
GeneralI count 14 peoplememberGates VP17 Nov '07 - 13:03 
Wow, I count 14 people on the team and they couldn't figure this out?
 
Seems like a classic PEBKAC problem to me. There are two reasons things don't get garbage collected: not properly dereferenced or they're still listening to an event (I know, effectively the same reason). The article actually says "deleting" objects, which makes it sounds like their custom objects were missing or not correctly implementing an IDisposable interface.
 
All in all, it's cute that the tool helped them, but it's really just a tool fixing up their rookie mistakes. I'm sure that ANTSProfiler has some uses, and the results were pretty significant in this story, but there were a ton of other ways that this thing could have been solved.
GeneralCommon ProblemmemberNick Berardi17 Nov '07 - 11:48 
Chances are you were using the observable pattern to monitor if your movement of the truck would bang in to anything that you have already observed. Then when it was 10 feet behind you, you would send the whole obstruction array through a calculator to remove the objects that were too far away. But the obstructions were still subscribed to the truck movement event. Chances are you want to use the WeakEvent Pattern.
 
http://msdn2.microsoft.com/en-us/library/aa970850.aspx[^]
 
Also described here in more detail:http://diditwith.net/PermaLink,guid,aacdb8ae-7baa-4423-a953-c18c1c7940ab.aspx[^]
 
Every developer runs in to this problem with their own language. I just taught one of my Jr. Developers about this concept a couple weeks ago in Action Script.
Generalor you could use java!membermystro_AKA_kokie17 Nov '07 - 9:01 
After all C# is essentially Java for .NET. Netbeans and VisualGC can accomplish the same thing for free, put the $300 (that you may spend on ANTS) in a savings accountSmile | :)
 
****WAO, I haven't posted to CP in ages!!!***
 

Looking for me in cyberspace? I am the electron with the red hat, occupying 3rd sit on the left of the data bus.
by the way, perl stinks.

"I believe god invented man, because he was disappointed in the monkey"
Mark Twain

GeneralRe: or you could use java!memberNick Berardi17 Nov '07 - 11:08 
Java will have the same problem. As does ActionScript. And as does any managed language. It is something programmers should be aware of.

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Rant Rant    Admin Admin   

Permalink | Advertise | Privacy | Mobile
Web02 | 2.6.130523.1 | Last Updated 9 Nov 2007
Article Copyright 2007 by Red Gate Software
Everything else Copyright © CodeProject, 1999-2013
Terms of Use
Layout: fixed | fluid