411 REFERENCE · Wikibooks
Minecraft/How the Game Works
Reference material by Wikibooks contributors. This retains its source identity and is not 411 news reporting.
Spawning
Rules mob spawning follows:
Mobs must spawn within 32 blocks of a player
Mobs cannot spawn within 24 blocks of any player
Enemies must spawn in the dark
Mobs will begin despawn (vanish into thin air) if it has not been named with a name tag and no players are within 32 blocks. Most mobs (exept those named with a name tag) instantly despawn if they are further than 128 blocks from the player. Mobs do not despawn if they are in a confined space smaller than 25 blocks in all directions. This is extremely helpful when farming animals for meat.
As a general rule, only mobs that can spawn naturally (and can respawn) can despawn (preventing you from going to a village or end city, leaving, coming back and seeing it empty). The mobs that can't despawn include:
Villagers
../Npc/s
Tamed animals
The Wither
The Ender dragon
Shulkers
Pillagers
Illagers
Evokers
Elder Guardians
Iron Golems
Snow Golems
For these mobs, getting farther away than 32 blocks causes them to freeze until you get near again. It also causes them to be invisible until you get closer.