Source:
<code>private static void initializeSyncedRealm(Realm realm) { boolean commitChanges = false; try { realm.beginTransaction(); long currentVersion = realm.getVersion(); final boolean unversioned = (currentVersion == UNVERSIONED); final RealmProxyMediator mediator = realm.configuration.getSchemaMediator(); final Set<Class<? extends RealmModel>> modelClasses = mediator.getModelClasses(); final OsRealmSchema.Creator schemaCreator = new OsRealmSchema.Creator(); for (Class<? extends RealmModel> modelClass : modelClasses) { mediator.createRealmObjectSchema(modelClass, schemaCreator); } final OsRealmSchema schema = new OsRealmSchema(schemaCreator); long newVersion = realm.configuration.getSchemaVersion(); long schemaNativePointer = schema.getNativePtr(); if (realm.sharedRealm.requiresMigration(schemaNativePointer)) { if (currentVersion >= newVersion) { throw new IllegalArgumentException(String.format("The schema was changed but the schema version " + "was not updated. The configured schema version (%d) must be higher than the one in the Realm " + "file (%d) in order to update the schema.", newVersion, currentVersion)); } realm.sharedRealm.updateSchema(schemaNativePointer, newVersion); realm.setVersion(newVersion); commitChanges = true; } <START> schema.close(); <END> final Map<Class<? extends RealmModel>, ColumnInfo> columnInfoMap = new HashMap<>(modelClasses.size()); for (Class<? extends RealmModel> modelClass : modelClasses) { columnInfoMap.put(modelClass, mediator.validateTable(modelClass, realm.sharedRealm, false)); } realm.getSchema().setColumnIndices((unversioned) ? newVersion : currentVersion, columnInfoMap); if (unversioned) { final Transaction transaction = realm.configuration.getInitialDataTransaction(); if (transaction != null) { transaction.execute(realm); } } } catch (Exception e) { commitChanges = false; throw e; } finally { if (commitChanges) { realm.commitTransaction(); } else { realm.cancelTransaction(); } } }</code><technical_language>schema lifecycle follow Realm instance lifecycle instead? E.g. release release instance</technical_language>

Target:
private static void initializeSyncedRealm(Realm realm) { boolean commitChanges = false; OsRealmSchema schema = null; try { realm.beginTransaction(); long currentVersion = realm.getVersion(); final boolean unversioned = currentVersion == UNVERSIONED; RealmConfiguration configuration = realm.getConfiguration(); final RealmProxyMediator mediator = configuration.getSchemaMediator(); final Set<Class<? extends RealmModel>> modelClasses = mediator.getModelClasses(); final OsRealmSchema.Creator schemaCreator = new OsRealmSchema.Creator(); for (Class<? extends RealmModel> modelClass : modelClasses) { mediator.createRealmObjectSchema(modelClass, schemaCreator); } schema = new OsRealmSchema(schemaCreator); long newVersion = configuration.getSchemaVersion(); long schemaNativePointer = schema.getNativePtr(); if (realm.sharedRealm.requiresMigration(schemaNativePointer)) { if (currentVersion >= newVersion) { throw new IllegalArgumentException(String.format( "The schema was changed but the schema version was not updated. " + "The configured schema version (%d) must be greater than the version " + " in the Realm file (%d) in order to update the schema.", newVersion, currentVersion)); } realm.sharedRealm.updateSchema(schemaNativePointer, newVersion); realm.setVersion(newVersion); commitChanges = true; } final Map<Class<? extends RealmModel>, ColumnInfo> columnInfoMap = new HashMap<>(modelClasses.size()); for (Class<? extends RealmModel> modelClass : modelClasses) { columnInfoMap.put(modelClass, mediator.validateTable(modelClass, realm.sharedRealm, false)); } realm.getSchema().setColumnIndices( (unversioned) ? newVersion : currentVersion, columnInfoMap); if (unversioned) { final Transaction transaction = configuration.getInitialDataTransaction(); if (transaction != null) { transaction.execute(realm); } } } catch (Exception e) { commitChanges = false; throw e; } finally { if (schema != null) { schema.close(); } if (commitChanges) { realm.commitTransaction(); } else { realm.cancelTransaction(); } } }