One simple solution is that which you find in games: mana. Make the use of magic a limited resource, and there you have a limit on the number of undead. More experienced/knowledgeable users can use more mana and use more undead, but are still limited. The non-undead will rot, and you won't even have that many bodies to put your magic on.
it can be concluded that at some point the world will be overpopulated
Of course this is to answer your question. But one thing that strikes me that nobody else commented yet, is on your main concern. You asked how to avoid a problem and people are telling you how to do so, but nobody commented on the problem itself. Overpopulation? Let's think about it for a minute.
"wiped all but few thousand survivors"
Earth is right now at 7 billion people, and if we migrate to sustainable means of production, we can hold much more. There are extensive parts of land with no use at all currently. So, to overpopulate, specially with beings that take space but don't need food, clothes or consume anything else, we could maybe triple that or more. 227
Not even counting that you could keep your undead in the ocean.
Assuming a world similar to earth in size and land area, and a population of only a few thousand, it would take several thousands of years to repopulate all of it and reach the current state, in terms of population. And that is assuming an non-conservative growth. Then you add a few other factors:
* when someone dies, their respective undead go back to rotting until someone else takes over (Google says Maggots can consume up to 60 percent of a human body in under seven days, meaning if people don't act fast, that undead is lost forever)
* plus the natural wearing out of the undeads in use
* plus wars where massive numbers of undead gets destroyed
Keeping the supply replenished starts to look like a challenge. Even if you don't follow any other of the suggestions here to limit the usage of necromancy and such, we're still looking at several thousands of years of having enough space for everyone, living and undead.
How can I explain that it will never happen?
When you take into account all those factors, they don't explain that it will "never happen", but for sure it is not a problem to be worried about for uncountable generations (more than 100 for sure). And if the world is medieval, they will most probably evolve their technologies quite a lot, very fast. Specially with so much free labor. It's very feasible they will reach the space age in less than 2 millenia.
Maybe ask yourself how relevant to your story it is to worry about that at this point. ;-)
Note to self: a civilization with space exploration technology and necromancy. Sounds original, this idea can go somewhere. X-D 335