  We introduce a mean counting function for Dirichlet series, which plays the
same role in the function theory of Hardy spaces of Dirichlet series as the
Nevanlinna counting function does in the classical theory. The existence of the
mean counting function is related to Jessen and Tornehave's resolution of the
Lagrange mean motion problem. We use the mean counting function to describe all
compact composition operators with Dirichlet series symbols on the
Hardy--Hilbert space of Dirichlet series, thus resolving a problem which has
been open since the bounded composition operators were described by Gordon and
Hedenmalm. The main result is that such a composition operator is compact if
and only if the mean counting function of its symbol satisfies a decay
condition at the boundary of a half-plane.
