Evolution of specialization under non-equilibrium population dynamics