I have a Samsung Galaxy S with stock Android 2.3.3 (with carrier modifications, regretfully).

Usually, when I reboot the phone, it shows the boot animation for a few seconds, then goes to the lock screen while it finishes booting.  Every once in a while (though more often lately than it used to), the boot animation screen gets stuck for about 5 minutes before showing me the lock screen.

This happens both on clean boots (i.e. shutting it down from the power button menu and then restarting) as well as when the phone freezes and I restart it, or when it crashes.

What is the phone doing at this time?

Why does it do it sometimes and not at other times?

How can I avoid this?