Its just coincidence or is a perfect size for a better ui?
Well spotted! BTW, so does SE. I think it's because 25px is standard size for a textbox (search), and then on each side you add about 5px of background and a total of 5px more for borders and shadows, and you arrive at 40. This doesn't explain Grooveshark, which is the only one of the four without a textbox in its header, but there you have buttons on the right, and the same goes for them. So if you're going minimalist and you want controls in your header, that's apparently the smallest reasonable header you can get, unless you're willing to compromise visual appeal.
Gmail only has 30, but they have no buttons or textboxes, just the menu, and extremely minimal borders.
40px is a relatively small header height. Small headers allow more elements to fit above the fold. By putting more things above the fold, usability is increased because users don't have to scroll as much to get to the content they need. All these sites have chosen a small height of 40px, as opposed to say 200px, for this reason.
From personal preference, I like to design my elements in multiples of 5px or 10px. It creates some sense of Feng Shui harmony. I would feel my chi off balance if my header was 39px tall. Maybe other designers like these "round" numbers too. They chose 40px because it's a multiple of 5 or 10. I remember my Economics teacher tell the class how on the TV, she only raises or lowers the volume to a multiple of 5 or else she'll feel weird. Most of the class seemed to conform to this nature. It's an innate desire for balance and harmony.
i personally work in multiple of 5, it's not because of the cognitive nature that people like or tend to design in multiples of 5, it's way too easier to measure every single space in the back of your mind fast without thinking if you're using the 960 grid, it even helps in the development, if you design and other guys get your stuff live, it's good to get them used to this system, it reduces flaws and misinterpretations.