Mitigate the file size growing problem

This change does the following:
 * disables the caching of Realm instances in threads without an event loop
 * makes the Realm class implement Closable
 * does reference counting for closing Realm instances
 * checks if the Realm instance has not been closed before any operation