I have some thoughts that are contradicting each other.
Gym <=> Good for health and longevity.
Eat less calories <=> Good for health and longevity.
Gym or eating less calories - what is better for health?
You mention "health and longevity"...
Unless you're suffering from obesity-related health issues, eating less calories will not help you with "health and longevity". Eating balanced and nutritious meals is more important than calories if your concern is "health and longevity" (and not weight gain/loss).
"Health and longevity" is very subjective, and largely depends on other life factors.. for example, if Bob goes to the gym every day, and eats very healthily, but also is a heroin addict, then eating healthily and exercising likely won't really matter. That's an extreme example, but I hope it does prove a point.
If you're talking strictly about weight gain/loss, it's all about calories in <--> calories out. Simply put, if you eat less calories then you burn, you will lose weight. Exercising allows you to eat more (because you're burning more).
To be picky, it does not need to be "gym" (gym or fitness classes, jogging...); it is regular physical activity of lower intensity (walking, cycling to work...) that can already be healthy.
...marked health benefits are observed with relatively minor volumes of physical activity.
You can eat less calories than you burn if you want to lose weight.
Both physical activity and weight loss (if overweight) can be healthy; there is no need to overthink which is better. Do one or another or both, according to what you think is realistic for you to maintain long-term.