java.lang.Object
net.i2p.router.tasks.OOMListener
All Implemented Interfaces:
I2PThread.OOMEventListener

public class OOMListenerextends Object implements I2PThread.OOMEventListener
Out-of-memory error handler for emergency router shutdown.

This listener is registered with the I2PThread system to handle OutOfMemoryError events. When the JVM runs out of memory, this class attempts to perform an orderly shutdown of the router to prevent data corruption and hung processes.

The handler is designed to be resilient against additional OOM errors during the shutdown process and will make best-effort attempts to:

  • Clear caches to free memory
  • Log diagnostic information
  • Generate thread dumps for debugging
  • Shutdown the router with appropriate exit code
  • Field Details

    • _context

      private final RouterContext _context
      The router context to log and act on.
    • _wasCalled

      private final AtomicBoolean _wasCalled
      True once the listener has fired.
  • Constructor Details

    • OOMListener

      public OOMListener(RouterContext ctx)
      Create a new out-of-memory event listener.
      Parameters:
      ctx - the router context for accessing router services
  • Method Details

    • outOfMemory

      public void outOfMemory(OutOfMemoryError oom)
      Handle out-of-memory error by shutting down router gracefully.

      This method is called when the JVM runs out of memory. It attempts to perform emergency cleanup and shutdown the router to prevent further corruption or hangs.

      The method is designed to be resilient against additional OOM errors during the shutdown process and will attempt to:

      • Prevent multiple parallel shutdowns
      • Increase thread priority to aid shutdown
      • Clear caches to free memory
      • Log memory status and configuration hints
      • Generate thread dump for debugging
      • Log the event to event log
      • Shutdown router with OOM exit code
      Specified by:
      outOfMemory in interface I2PThread.OOMEventListener
      Parameters:
      oom - the out-of-memory error that triggered this handler
    • isDebianPackage

      private static boolean isDebianPackage(RouterContext ctx)
      Best guess if running from a Debian package.
      Parameters:
      ctx - the router context, used for the base directory and the update property
      Returns:
      true if the install directory and settings match a Debian package install
    • getWrapperConfigPath

      public static String getWrapperConfigPath(RouterContext ctx)
      The best guess path for the wrapper.config file.

      This method attempts to determine the location of the wrapper configuration file based on the installation type and platform. Since there's no system property that provides the actual path, this method makes educated guesses based on common installation patterns.

      The path determination follows these rules:

      • Linux service: /etc/i2p/wrapper.config (or /usr/share/i2p for Gentoo)
      • Debian package: /etc/i2p/wrapper.config
      • Other installations: {baseDir}/wrapper.config

      Note: The returned path may not exist - this is just the best guess for where the file should be located.

      Parameters:
      ctx - the router context for determining installation type
      Returns:
      the probable path to wrapper.config file