Is it too much to ask to be able to start an app before I go to bed at night, turn off the screen, and then turn the screen back on in the middle of the night and have it still there, undisturbed? Instead, after 4-5 hours of inactivity there is a good chance that the app will be killed! Why does Android even do this? It can't be for power saving, because the device is plugged in all night (battery charged), and it can't be for speeding up the device because nothing else is running (aside from a few CPU seconds an hour for push notification checks), or for dealing with running out of memory because the device in question has 3GB and almost 600MB is being reported as free. The app is not even in the background, so if the app explicitly foregrounds itself it doesn't really fix the issue.
Even with apps that do a good job of saving their current state and restoring it after being killed (and most do not) it's still a pain because the app is no longer on the screen so it has to be located and started, and then it takes a bit of time for it to for initialization code. During the middle of the night is a very inopportune time to have to do this!
In older versions of Android there were certain hacks you could do to the underlying Linux proc or sys filesystems to try to combat this problem. There is an existing question but it is nearly seven years old. But now I am running the latest version of Android (8.1) and I'm wondering about the current state of the art in solving this problem.