Hamiltonian or Lagrangian mechanics as described by the principle of least action is some kind of complementary to the principle of increasing entropy (second law of thermodynamics) needed to describe described dissibative systems in thermodynamics.

Hamiltonian/Lagrangian mechanics only describes the dynamics of reversible processes in accordance with Liouville's/von Neumann's equation

\[\frac{\partial \rho}{\partial t} + iL\rho = 0\]

where $\rho$ is the distribution function (density operator in QM) and $L$ is the Liouville operator involving the Poisson bracket (commutator in QM). The fine grained entropy $S = -k_B \ln \rho$ which is proportional to volume in phase space does not increase.

For large systems with many degrees of freedom, it is for practical reasons (initial conditions not exactly known, instability of the system, etc) not feasable to follow the trajectory in the corresponding very high-dimensional phase space. For large systems that are not in thermodynamic equilibrium, irreversibility and the second law of thermodynamics, characterized by an increase of the coarse grained entropy $S = -k_B \int d\Gamma f_N \ln f_s$ (where $f_s$ is a reduced distribution function) emerge as a result of losing information (to not modelled degrees of freedom) by "coarse graining" the evolution of the system in phase space. The evolution of the system does no longer follow the homogenous Liouville/von Neumann equation given above; as dissipative terms that appear on the right hand side, have to be taken into account in this case.

The amount of information needed to describe the dynamic evolution of a system depends on how far away it is from thermodynamic equilibrium. To describe a system way out of thermodynamic equilibrium, in principle the full N-particle distribution function is needed to describe the state of the system by given by the mean values of appropriate relevant quantities. In the course of the evolution towards equilibrium, the number of (effective) degrees of freedom that have to be explicitely modelled reduces until the system has reached equilibrium. In thermodymanic equilibrium, the system is fully describe by its conserved quantities and all dissipative processes have stopped.