A key feature of the vertebrate adaptive immune system is acquired immune memory, whereby hosts launch a faster and heightened response when challenged by previously encountered pathogens, preventing full infection. Here, we use a mathematical model to explore the role of ecological and epidemiological processes in shaping selection for costly acquired immune memory. Applying the framework of adaptive dynamics to the classic SIR (Susceptible-Infected-Recovered) epidemiological model, we focus on the conditions that may lead hosts to evolve high levels of immunity. Linking our work to previous theory, we show how investment in immune memory may be greatest at long or intermediate host lifespans depending on whether immunity is long lasting. High initial costs to gain immunity are also found to be essential for a highly effective immune memory. We also find that high disease infectivity and sterility, but intermediate virulence and immune period, increase selection for immunity. Diversity in host populations through evolutionary branching is found to be possible but only for a limited range of parameter space. Our model suggests that specific ecological and epidemiological conditions have to be met for acquired immune memory to evolve.