From 3e55cff153f0873f625e47e0875a52eaba68b81f Mon Sep 17 00:00:00 2001
From: zzz <zzz@mail.i2p>
Date: Sat, 25 Apr 2015 00:02:55 +0000
Subject: [PATCH] Util: Catch and convert OOM in I2PThread.start() to a
 RuntimeException, to give better message to users and prevent JVM shutdown on
 what is unlikely to be a heap issue.

---
 core/java/src/net/i2p/util/I2PThread.java | 23 +++++++++++++++++++++++
 1 file changed, 23 insertions(+)

diff --git a/core/java/src/net/i2p/util/I2PThread.java b/core/java/src/net/i2p/util/I2PThread.java
index 17cfa12127..044b82f54c 100644
--- a/core/java/src/net/i2p/util/I2PThread.java
+++ b/core/java/src/net/i2p/util/I2PThread.java
@@ -76,6 +76,29 @@ public class I2PThread extends Thread {
     }
 ****/
     
+    /**
+     *  Overridden to provide useful info to users on OOM, and to prevent
+     *  shutting down the whole JVM for what is most likely not a heap issue.
+     *  If the calling thread is an I2PThread an OOM would shut down the JVM.
+     *  Telling the user to increase the heap size may make the problem worse.
+     *  We may be able to continue without this thread, particularly in app context.
+     *
+     *  @since 0.9.20
+     */
+    @Override
+    public void start() {
+        try {
+            super.start();
+        } catch (OutOfMemoryError oom) {
+            System.out.println("ERROR: Thread could not be started: " + getName());
+            if (!(SystemVersion.isWindows() || SystemVersion.isAndroid())) {
+                System.out.println("Check ulimit -u, /etc/security/limits.conf, or /proc/sys/kernel/threads-max");
+            }
+            oom.printStackTrace();
+            throw new RuntimeException("Thread could not be started", oom);
+        }
+    }
+    
     @Override
     public void run() {
         //_name = Thread.currentThread().getName();
-- 
GitLab