When do mobs despawn in Minecraft?

Solution 1:

My answer is no longer correct, even though, at the time of posting, it was. Look to some of the other answers (particularly the bounty winner) for more up to date information.


Most important rule: when a chunk is unloaded, so are the mobs (and all other items) on it, although passive mobs won't be removed.

There is also a limit on the number of mobs that can be loaded at one time: 200 hostile and 15 neutral mobs (I don't know if there's a limit on passive mobs). Also, hostile mobs are removed if they leave the 9x9 chunks that they're allowed to spawn in.

Running away will usually allow you to stay out of the reach of hostile mobs, since a chunk needs to be loaded a certain amount of time before mobs will start to spawn, but the chunks immediately behind you could have the max allowable mobs, making it dangerous at best to turn around.

Take a look at the wiki for more information.

Solution 2:

From the EntityAnimal class in EntityAnimal.java:

protected boolean canDespawn()
{
    return false;
}

This should apply to all passive mobs. (I checked, they all extend the EntityAnimal class.)

Wolves however are interesting:

protected boolean canDespawn()
{
    return !isWolfTamed();
}

So this means that tamed wolves do not despawn, while wild wolves do despawn.

It seems that slimes and squid will also despawn. Both classes contain this method:

protected void updateEntityActionState()
<snipped>
    despawnEntity();
<snipped>

I didn't see any other references to despawning.

Solution 3:

As of Minecraft 1.8, these are the two main despawning rules:

  • If a mob has not been within 32 blocks1 of a player for more than 30 seconds, it has a 1 in 800 chance every tick of despawning (about 1 in 40 every second)
  • If there are no players within a 128 block radius2 of a mob, it will instantly despawn

Exceptions:

  • Mobs with nametags will never despawn, except...
  • Mobs which have picked up items (i.e. were not spawned with that item) will never despawn, except...
  • When the gamemode is set to peaceful, all hostile mobs will despawn (including those with nametags, or those who have picked up items)
  • Passive mobs will never despawn (except chickens originally spawned as chicken jockeys, which will follow zombie despawning rules)

For those who like diagrams, this image will help: (From the Minecraft Wiki)

Diagram of which the information is outlined in dotpoints

Footnotes:

1Not sure whether this is a sphere or a cylinder It is confirmed to be an Euclidean sphere (like the the instant despawn zone). Testing apparatus:

The zombie despawned after about 2 mins when I stood more than 32 blocks below it

2 An Euclidean sphere with a radius of 128 blocks

More Notes:

  • If there are more mobs than allowed by their mob cap, no special despawning rules are followed. Mob spawning is simply suspended until they despawn naturally. Source (looking for a better citation)