When you are trying to sleep is generally when compulsive thoughts start getting really bad. Now for me those thoughts are usually of anal sex with family members so it's not so bad, but not everyone else is as lucky. This is why I reccomend forming some kind of mental contruct, a series of specific thoughts you focus on before bed that shuts out other thoughts that my be upsetting or just not restful. That way you aren't just lying in bed contemplating mortality or remembering that really embarassing thing you said when you were 14. It takes practice but it genuinely helps.