(CNN) Being overweight is linked to many health problems, and shedding some pounds is often presented as the best way to avoid them, no matter your age.
But it's not quite that simple, according to a study published Wednesday in The BMJ that examines the link between changes in body weight and the risk of premature death.
Researchers found the association between weight gain and mortality weakens as you get older, and losing weight in middle age or late adulthood may heighten the risk of premature death, particularly when it comes to heart disease.
"Our takeaway is that it's best to prevent weight gain at younger ages to reduce the risk of premature death later in life," said study author An Pan, a professor in the Department of Epidemiology and Biostatistics at the Tongji Medical College in Wuhan, China.
The study found that people who remained obese, as measured by body mass index, throughout their adult life had the highest risk of premature death. Weight gain from mid-20s into middle age was also associated with increased risk of mortality when compared to people who remained at normal weight throughout their life.
However, weight loss in middle and older age "was significantly related to increased mortality risk," the study said.
Obesity is major public health problem in the United States and globally. In the US, 38% of women and 36% of men were clinically obese in 2016, according to data cited by the study -- up from 14% and 11%, respectively, in 1975.
In what he described as a limitation of the research, Pan said that the study did not include an analysis of the reason for later-in-life weight loss. One factor that could play a role was whether the weight loss was intentional or not.
"Unintentional weight loss could be a sign of underlying conditions like diabetes or cancer," Pan said. Another reason, he said, could be because the weight loss involved someone who was already obese and thus already at a higher risk.
"The first message is to try not to gain weight when you're young, and in old age focus on maintaining a healthy lifestyle," Pan said. "Weight is a secondary consideration."
The study found that people who remained overweight, but not obese, throughout their adult life had little or no association with an increased risk of premature death.
The study looked at 36,052 people age 40 and older based on data from the US National Health and Nutrition Examination Survey -- a nationally representative annual survey that includes interviews, physical examinations and blood samples, to gauge the health of US citizens.
Participants' weight was measured as part of the study, and they were asked to share their weight from 10 years earlier and at age 25.
Deaths from any cause, and specifically from heart diseases, were recorded for an average of 12 years, during which time there were 10,500 deaths.
The study did not find any significant link between various weight change patterns and deaths from cancer.
Previous research has linked a high BMI in adulthood with a higher risk of premature death, but much less is known about the role of changing body weight over time. Pan said more research is needed to unravel the reasons for the link between changes in body weight and mortality, and the long-term health consequences of weight loss.