close method

  1. @override
Future<void> close()
override

Closes the class loader and releases all resources.

This operation:

  • Flushes all caches (calls flush)
  • Releases system resources
  • Marks the loader as closed
  • Prevents further operations

Post-Close Behavior

After closing, the loader becomes unusable:

Example

try {
  // Use the loader
  final classes = await loader.loadMultipleClasses(classNames);
} finally {
  // Always close in finally block
  await loader.close();
}

// Or use with try-with-resources pattern
await using(SystemClassLoader(), (loader) async {
  return await loader.loadClass<User>('com.example.User');
});

Resource Management

  • Releases memory held by caches
  • Closes any open file handles or connections
  • Notifies dependent systems of shutdown
  • Ensures clean application termination

Throws IOException if resource cleanup encounters errors.

Implementation

@override
Future<void> close() async {
  if (_closed) return;

  _closed = true;

  try {
    // Flush all caches
    await flush();

    // Complete the close operation
    _closeCompleter.complete();
  } catch (e) {
    _closeCompleter.completeError(e);
    rethrow;
  }
}