Music has a tendency to get stuck in our heads. You know the experience - a tune intrudes on your thoughts and plays, and replays, in a never-ending loop. It happened recently to me. So, as a science reporter, I thought I'd try to find out why.

Several weeks ago, I was at home on a Sunday morning when, for no apparent reason, three words popped into my head - "Funky Cold Medina".

That's the name of a song performed by rapper Tone Loc. I'm told it was a hit in the 1990s, but I hadn't heard it until the night before when a friend sang it at a karaoke bar.

I kept hearing the lyrics - "Cold coolin' at a bar, and I'm looking for some action. But like Mick Jagger said, I can't get no satisfaction."

When the song reappeared in my head, I could hear my friend singing it again and again... and again.

I was stuck with it for nearly a day and a half, before it finally went away.

But it left behind a nagging question. Why do we get songs stuck in our heads in the first place?

"I personally couldn't believe how little there was in terms of research on this phenomenon," says Dr Vicky Williamson, a music psychologist who started studying it a few years ago.

"It seemed to happen to me very frequently."

Dr Williamson, a memory expert at Goldsmith's College in London, found that scientists use a range of terms to describe the subject - stuck-song syndrome, sticky music, and cognitive itch, or most commonly "earworm" -a word which some people misunderstand.

Williamson collaborated with a BBC radio programme,Shaun Keaveny's Breakfast Show on 6Music, which asked its listeners what earworms they were waking up with.

Earworms reported to BBC 6Music "My bloody earworm is that bloody George Harrison song you played yesterday. Woke up at 4.30 this morning with it going around my head. Please don't ever play it again!" "I've got 'ch-ch-ch-changes' by David Bowie as a repetitive ear worm, because it was the last song I heard before the battery on my iPod ran out on a very long cycle ride." "My earworm is still 'Alive' by Pearl Jam and has been for days!" Shaun Keaveny's Breakfast Show - BBC 6Music

She also collected more stories and experiences through an online survey at her website,earwormery.com.

The data has shown some surprising findings. "When I had 1,000 earworm songs in my database, there were only about half a dozen or so that had been named more than once - that's how heterogeneous the response was. It's a very individual phenomenon," she says.

She now has more than 2,500 earworm experiences on record and that individuality remains a feature, though it occasionally changes when a film or TV show becomes popular.

"You suddenly get five or six people reporting the song from a new film because they've just been to see it," she says. "When we first started, a tune from the hit American TV show Glee - a song called Don't Stop Believing - raced to the top."

She identified a set of triggers that had apparently caused these tunes to pop into people's heads and stay there.

"The first one is music exposure, which means the person has heard the music recently," she says. (No surprise there. That explains why I was stuck with Funky Cold Medina.)

Earworm offenders Vicky Williamson nominates: Lady Gaga, Queen, Abba, Kylie, Beyonce, Adele, Europe, Coldplay, Elbow, Johnny Cash

The World newsroom nominates: Marcy Playground's Sex and Candy, Nothing but the Music by the Backstreet Boys, The Sound of Music, Popcorn by Hot Butter, Rump Shaker by Wreckx-N-Effect Listen to staff of The World reveal their own personal earworm problems

Another unsurprising finding was that if you hear a song repeatedly, you're more likely to get stuck with it.

But sometimes songs pop into our heads even when we haven't heard them for a long time. In this case, something in our current environment may trigger the memory.

Williamson experienced this recently herself, when she was in her office and noticed an old shoebox.

"It's from a shop called Faith," she says. "And just by reading the word 'Faith', my memory went down a line of dominoes and eventually reached the song 'Faith' by George Michael. And then he was in my head for the rest of the afternoon." She has now removed that shoe box from her office.

Another trigger she identified was stress.

One woman in Williamson's online survey said a song - Nathan Jones, by Bananarama - first got stuck in her head when she was 16 and taking a big exam.

"She now gets that song at every single moment of stress in her life," says Williamson. "Wedding, childbirth, everything."

There are various theories that may explain why this happens.

Williamson says earworms may be part of a larger phenomenon called "involuntary memory", a category which also includes the desire to eat something after the idea of it has popped into your head. "A sudden desire to have sardines for dinner, for example," as she puts it. Or suddenly thinking of a friend you've not seen for ages.

There are a couple of reasons why this might happen with music, she says.

"Firstly, because music can be encoded in so many ways, it's what we call a 'multi-sensory stimulus'," she says.

"This is especially true if you are a musician because you encode how to play it, what it looks like on a score, as well as what it sounds like.

"Secondly, music is often encoded in a very personal and emotional way, and we know that when we encode anything with emotional or personal connotations, it's recalled better in memory."

Other experts suggest music may get lodged in our heads because of the way humans evolved.

"For a very long period of time, we needed to remember information," says Daniel Levitin of McGill University in Montreal, an expert in the neuroscience of music.

"Information like where the well is, or which foods are poisonous and which aren't, and how to care for wounds so they won't become infected."

Modern humans have been around for some 200,000 years, but written language may have been invented only around 5,000 years ago, Levitin says. So through much of human history people memorised important information through songs.

Earworm 'triggers' Recent music exposure

Repeated music exposure

Word triggers (see Faith shoebox example)

People triggers (where sight or memory of a person is associated with a song)

Situation trigger (eg weddings can cause you to remember your own first dance song)

Stress

Surprise

Dreams

Mind wandering Source: Vicky Williamson

That practice continues today in cultures with strong oral traditions.

Levitin says the combination of rhythm, rhyme, and melody provides reinforcing cues that make songs easier to remember than words alone.

He says the main question people ask him about earworms is: "How do we turn them off?"

Levitin offers a piece of advice. "Just think of another song and hope that'll push out the first one."

Dr Vicky Williamson is currently trying to find the best "cures" for earworms. She says the structure of one tune may have a bearing on whether it's useful in displacing another.

She's also looking at whether everyday strategies help, like going for a run or doing a crossword.

Both Levitin and Williamson agree that getting an unwanted tune out of your head is a relief. But of course the song that cures you might just end up being the next one that gets stuck.

Additional reporting by Rob Hugh-Jones.

Listen to more on this story atPRI's The World, a co-production of the BBC World Service, Public Radio International, and WGBH in Boston. You'll also be able to hear what earworms plague people working on the show.

Thanks for all your comments. A selection will be published.