Oct 18 2011

Common Causes for Memory Leaks

You’ll always have to deal with memory issues, no matter the programming language. Even with the Java programming language, if the right precautions are not taken, you will have some sort of memory leaks, memory issue, out of memory exception, or heap size problem. I’ve seen two common types of memory issues in every application I’ve worked on.

A common source of memory leaks is global static singleton god object that collects or manages a lot of data, maybe a system cache, object lookup table, service locator, etc. This type of singleton pattern will require other objects to register with it, add themselves to the global pool of objects, but if they are not properly removed, unregistered, when they are no longer needed you will see your memory usage increase over time. I’ve seen this issue when using the callback or listener pattern and the listener object itself holds a lot of other data. This sort of problem is usually relatively easy to identify with a profiler, it will usually be one of the largest objects in your system.

The other, more difficult memory leak to identify, is when you have hundreds of thousands of objects each taking up a reasonable amount of memory. In this case, a single object instance will not take a lot of memory but collectedly the hundreds of thousands of objects can eat up a lot of memory. Here are a few things you can think about when dealing with a small class that spawns thousand of objects…

If you have int types, see if you can change them to short or byte types. Try subclassing if you have any number of properties that most often than not set or are null. Think about lazy loading arrays, lists, and other objects references. If there are many object instances of this class, and any portion of these instances are logically equivalent, think of using the flyweight pattern.

Mar 18 2011

Chipotle Online Order System

I love me a good burrito, and although Chipotle is not my favorite burrito joint, they do have an online order system that makes ordering lunch for a team of hungry, and picky, developers simple. But while scheduling a pick up order for the entire team I felt more like a tester than a customer.

Right on the first page after login in, I encountered infinite navigation loops. At page where I was to order, there is a big red button that read “ORDER NOW.” If you click this button, it reloads the current page you are already on. After trying that trick for a few times I realized I had to select one of the four order methods: online, app, fax, or burritos by the box.

Order Now

Order Now

I then had ‘issues’ adding invitees to my order. Adding invitees is a three step process. You have to add a list of email addresses in the text area, click ‘Add To Invitees’, this then does some AJAX magic and creates a set of check boxes for each invitee and you have to select all before they are actually invited. I can honestly say that I had to this this step a few times to get it right. If done right, each invitee receives an email with a link to order their own meal.

Add Invitees

Add Invitees

One large problem that a few folks had was that they didn’t order what they actually wanted. Unbeknownst to several team members, Chipotle wants users to be explicit about each ingredient in a burrito. A lot of team members just clicked on Chicken and didn’t realize that Chipotle would just give you a piece of tortilla with some chicken. The burrito looked like it had some stunted growth issue, but worst was the faces of the folks that ordered such a mini-burrito. One problem here is that the UI for ordering what goes inside the burrito is cluttered and goes against conventions of ordering a burrito in most taquerias. This mistake happened to three developers, so I can deduce that it is a common error when ordering online from the Chipotle.

Burrito Builder

Burrito Builder

One final issue I encountered was with the pick up of the online order. I didn’t realize or received a message from Chipotle that I had to log into the system after everyone had made their order and check out. When the time came, I just showed up at my local Chipotle to pick up the food but to my surprised they had not received it because I forgot to check out. Of course, I logged onto their website from my phone there was no way for me to see the recent history or pending order. Lucky for me, I was able to reach an hungry developer in the office and asked him to log into their website from my computer and he was able to finalize the order.

In the end, a few folks didn’t get what they expected, the food was late, and I learned a valuable lesson. Next time I’ll just fax the order in.

Aug 19 2008

Database Best Practices

Every production application I have been involved with has made use of a database. As a programmer, you don’t have to be a DBA, but just as with software development you need to adhere to a core set of database best practices. Below are a few of the best practices I try to follow.

  • Use a database modeling tool that is vendor neutral.
  • Decide upon a database naming convention, standardize it across your organization and be consistent in following through with it. Always be consistent with the use of case and plural vs singular.
  • Have a similar naming between your table columns and object properties.
  • Try not to use system tables directly as they are not SQL standard and may change between releases.
  • Do not use SELECT * in your queries, explicitly write the required columns names in the select statement.
  • Always use a column list in your insert statements. This helps in avoiding problems when the table structure changes, like adding or removing columns.
  • Try to avoid wildcard characters at the beginning of the word while searching using the LIKE keyword.
  • Use the more readable ANSI standard join clause instead of he told style join.
  • If you have a choice, do not store binary files in the database. Instead store the path to the binary file in the database and store the file in the disk, preferably another disk drive.
  • Offload tasks like string manipulations, concatenations, row numbering, case conversions, type conversions, etc to the front-end application.
  • Store computed string manipulations, concatenation, case conversion, type conversions, etc in the database so you don’t have to waste CPU cycles recalculating.
  • Avoid leaking your database design details, such as primary keys.
  • Just like your code, be sure to comment your database design.
  • Use the bulk/multi insert statement.
  • Study the SQL execution plan prior to a major release.

Again, as a software developer you don’t have to be a database tuning expert but you also can’t be excused for not following the above best practices.

Technorati Tags: , , , ,

Jul 9 2008

Microsoft DreamSpark

Even thought I am a Windows user, I am no where near a Microsoft lackey but recently I learned that Microsoft is giving away a ton of professional grade software to students. Through the Microsoft DreamSpark program, students can get access to Visual Studio 2008 Professional, Windows Server 2003 Standard Edition, Microsoft Expression Studio, XNA Studio, XNA Creators Club, SQL Server 2005, as well as other free software such as their express edition software. That is well over $1,500 of free software. I am typically not a Microsoft fanboy but I do have to give credit to Microsoft for making its development tools free of charge to students across the world. Now I wish Adobe would do the same.

Technorati Tags: , , , , , , ,

Dec 3 2007

Favorite Programming Quotes 2007

Here is a list of great programming quotes from my readings over the last year. It is interesting to note that most of my reading material this year has been from blogs. There are just a great amount of programmer bloggers writing a lot of informative tutorials on every programming language, paradigm shift, and framework under the sun.

in my experience, one of the most significant problems in software development is assuming. If you assume a method will passed the right parameter value, the method will fail.
— Paul M. Duvall
Continuous Integration

Programming languages are like girlfriends: The new one is better because *you* are better.
— Derek Sivers
7 reasons I switched back to PHP after 2 years on Rails

The sooner we start coding fewer frameworks and more programs the sooner we’ll become better programmers.
— Warped Java Guy
Elementary Java Solutions

Starting a startup is hard, but having a 9 to 5 job is hard too, and in some ways a worse kind of hard.
— Paul Graham
The Future of Web Startups

In essence, let the market design the product.
— Paul Graham
The Future of Web Startups

A startup now can be just a pair of 22 year old guys. A company like that can move much more easily than one with 10 people, half of whom have kids.
— Paul Graham
The Future of Web Startups

Startups almost never get it right the first time. Much more commonly you launch something, and no one cares. Don’t assume when this happens that you’ve failed. That’s normal for startups. But don’t sit around doing nothing. Iterate.
— Paul Graham
How Not to Die

The key to performance is elegance, not battalions of special cases.
— Jon Bentley and Doug McIlroy

You’ll spend far more time babysitting old technologies than implementing new ones.
— Jason Hiner
IT Dirty Secrets

To Iterate is Human, to Recurse, Divine.
— James O. Coplien

No one hates software more than software developers.
— Jeff Atwood
Hanselminutes Podcast 74

I was a C++ programmer before I started designing Ruby. I programmed in C++ exclusively for two or three years. And after two years of C++ programming, it still surprised me.
— Matz
The Philosophy of Ruby

Good architecture is necessary to give programs enough structure to be able to grow large without collapsing into a puddle of confusion.
— Douglas Crockford
The Elements of JavaScript Style

Programming is difficult. At its core, it is about managing complexity. Computer programs are the most complex things that humans make. Quality is illusive and elusive.
— Douglas Crockford
The Elements of JavaScript Style

Code reuse is the Holy Grail of Software Engineering.
— Douglas Crockford
The Elements of JavaScript Style

The structure of software systems tend to reflect the structure of the organization that produce them.
— Douglas Crockford
The Elements of JavaScript Style

The definition of Hell is working with dates in Java, JDBC, and Oracle. Every single one of them screw it up.
— Dick Wall
CommunityOne 2007: Lunch with the Java Posse

Suppose you went back to Ada Lovelace and asked her the difference between a script and a program. She’d probably look at you funny, then say something like: Well, a script is what you give the actors, but a program is what you give the audience.
— Larry Wall
Programming is Hard, Let’s Go Scripting…

I went to school to learn how to program software applications, which inevitably have bug defects. There was no course at my university on testing, debugging, profiling, or optimization. These things you have to learn on your own, usually in a tight deadline.
— Juixe TechKnow

To most Java developers, Ruby/Rails is like a mistress. Ruby/Rails is young, new, and exciting; but eventually we go back to old faithful, dependable, and employable Java with some new tricks and idioms and we are the better programmer for it.
— Juixe TechKnow

You might as well pay your customers 50K because they are just your QA.
— Juixe TechKnow

May 2 2007

Unobtrusive JavaScript with Prototype and Behavior

If you are familiar with web application development you may already be familiar with the Model-View-Controller (MVC) design pattern. The MVC pattern aims to separate your server side business logic from your presentation code from your data model. The by-product of web applications are the HTML, CSS, and JavaScript pages generated and delivered to the client browser. Historically in the early Web 1.0 days, a web page’s content, style, and behavior would all be intertwined and glued tightly together in the HTML, do you remember the font tag? Somewhere along the way to the Web 2.0 network, developers opted to remove the style attributes from the content by using Cascading Style Sheets (CSS) classes.

CSS allows you to create style classes whose concern is purely a way to describe the size, font, color, and look of whatever content that uses the class.

.makeMeBeautiful {
  color: #00248F;
  text-decoration: underline;
  background-color: #FFFFFF;
  font-size: 1.1em;

To apply a CSS class on an HTML element you use the class attribute.

<div class="makeMeBeautiful">Hello, World</div>

The idea behind Unobtrusive JavaScript (UJS) is to remove JavaScript events, such as onclick and onmouseover, from the content in the same fashion as the style attribute. Unobtrusive JavaScript uses the class and id attribute of HTML tag elements to find and assign JavaScript events in a inconspicuous fashion, just as with CSS.

If you think of the HTML content as the Model, the CSS stylings as the View, and JavaScript behaviors as the Controller of MVC, then it makes perfect sense to remove all traces of style attributes and JavaScript events from the HTML.

Unobtrusive JavaScript with Prototype

Traditionally if you wanted to add a JavaScript onclick event to a HTML element you would do so by adding the onclick attribute. Unobtrusive JavaScript recommends that you instead find the tag by the class or id attribute and assign the even programmatically.

// Logic to execute when the end user
// clicks the element
function myOnClickFunction(elem) {

// Register the onclick event for all elements
// the given class name
window.onload = function() {
  // Find all elements that use that given CSS class
  var elements = document.getElementsByClassName("makeMeBeautiful");
    function(e) {
      // Assign the onclick method to the element
      Event.observe(e, "click", myOnClickFunction);

Note that the above JavaScript code works only when the Prototype JavaScript library is made available.

If your need to add Unobstrusive JavaScript events for a few tag elements and are already using Prototype then the above code will suffice.

Unobtrusive JavaScript with Behavior

For more restrictive selection process of the HTML elements to apply JavaScript events you can use the Behavior JavaScript library. The Behavior JavaScript library is a lot easier and more powerful way to add behavior to HTML elements than Prototype. With the Behavior library you define an associated hash map literal object thingie where the keys are CSS selectors and the value is an initializing JavaScript function. In the initializing function you can add any onclick, onmouseover, onchange, onsubmit, onfocus, onblur, onwhatever event.

var rules = {
  '.makeMeBeautiful' : function(elem){
    elem.onclick = function(){


The power of the Behavior library is that you can create the behavioral rules using CSS selectors to choose the tag elements for which to apply a given set of JavaScript functionality. Both the Prototype and Behavior code shown up to this point will apply the JavaScript onclick behavior on all elements that use the given CSS class. With the Behavior library you can use CSS selectors to restrict what elements to apply the behavior to. For example, to apply the onclick behavior only for div elements you can use the following rule.

var rules = {
  'div.makeMeBeautiful' : function(elem){
    // Your code here...

The idea behind Unobtrusive JavaScript is to separate the HTML, CSS, and JavaScript according to responsibilities in a similar fashion as the MVC design pattern suggests. UJS also helps in the maintenance of large sites and it allows for graceful degradation.