Optimal stability and instability results for a class of nearly integrable Hamiltonian systems