Added memory check when consuming a message.

The consumer will be stopped if the available memory is not enough.

These changes were initiated by @icolomina.