Masturbation may be good for you – or bad, depending on your age. The solitary sexual activity that is widely practised but little discussed, is linked with an increased risk of prostate cancer when practised frequently by young men in their twenties and thirties, doctors say.

But by the time men reach their fifties, it may protect against the disease because it helps remove toxins that have built up over a lifetime.

Prostate cancer is known to be driven by the male hormone testosterone, and men with high levels of testosterone tend to have a higher sex drive and a higher risk of the cancer.

Download the new Independent Premium app Sharing the full story, not just the headlines

But most research has examined older men because prostate cancer is unusual under 50. Researchers at the University of Nottingham studied the link between sexual activity in younger men and the disease to see if it affected their long-term risk. More than 400 men with prostate cancer diagnosed before the age of 60 were questioned about their sexual habits over the preceding decades and the results compared with 400 controls.

The findings showed that those who had been most sexually active in their twenties – having sexual intercourse or masturbating more than 20 times a month – were more likely to have the cancer. Frequent masturbation, but not sexual intercourse, in the twenties and thirties was significantly linked with the later development of prostate cancer.

In their 50s men who were most sexually active (more than 10 times a month for sexual intercourse and masturbation combined) enjoyed a small protective effect. The effect was greater when masturbation was assessed on its own.

Polyxeni Dimitripolou, who led the study published in the British Journal of Urology International, said: "It seems as if keeping up a certain level of sexual activity through the decades is better than having a high level early [in the 20s and 30s] and then nothing."

"One theory is that during the early years the prostate gland is more susceptible to hormonal changes and is still developing. As men age and accumulate toxins from the diet or through their lungs , sexual activity may help release them. Studies have found toxins in the semen and the fluid produced in the prostate. As you age it is more important to flush them out."

However, she admitted that there was no good explanation of why masturbation should have a greater impact on prostate cancer, either by increasing or reducing the risk, than sexual intercourse.

"For our sample there was no association with intercourse – all the effect was coming from masturbation. But it may have to do with our group of men. With a different group there could be different findings."

She added: "What makes our study stand out from previous research is that we focused on a younger age group than normal and included both intercourse and masturbation at various stages in people's lives."

"A possible explanation for the protective effect that men in their fifties appear to receive from overall sexual activity, and particularly masturbation, is that the release of accumulated toxins during sexual activity reduces the risk of developing cancer in the prostate area. This theory has, however, not been firmly established and further research is necessary."