We prove an abstract result of almost global existence of small solutions to semi-linear Hamiltonian partial differential equations satisfying very weak non resonance conditions and basic multilinear estimates. Thanks to works by Delort--Szeftel, these assumptions turn out to typically hold for Hamiltonian PDEs on any smooth compact boundaryless Riemannian manifold. As a main application, we prove the almost global existence of small solutions to nonlinear Klein--Gordon equations on such manifolds: for almost all mass, any arbitrarily large r and sufficiently large s, solutions with initial data of sufficiently small size ε≪1 in the Sobolev space Hs×Hs−1 exist and remain in Hs×Hs−1 for polynomial times ∣t∣≤ε−r. This is the first result of almost global existence without specific assumptions on the compact manifold. We also apply this abstract result to nonlinear Schr{ö}dinger equations close to ground states and nonlinear Klein--Gordon equations on Rd with positive quadratic potentials.