Oh man, this is a huge problem, and managing it is critical. I find the best way to deal with catching myself doing trivial work is to immediately stop coding at all, step away from the computer, and get a little exercise.
That way I can't use busyness as an excuse for just spinning my wheels. After a few minutes of not thinking too hard, I'll head back to the desk and verbally state my goals. After that, I tend to be orders of magnitude more productive.