java.lang.OutOfMemoryError 使用 Discord JDA 350+ 线程

java.lang.OutOfMemoryError with Discord JDA 350+ threads

所以我会尽力解释我的问题。目前我正在研究一个相当不错的规模计划。但是,我从来没有使用过这么多线程,如 350+ 所说。它目前给我一个内存不足的错误,但是我已经尝试了各种选项:

下面列出了我因内存泄漏问题而收到的错误。

java.lang.OutOfMemoryError: unable to create native thread: possibly out of memory or process/resource limits reached
        at java.base/java.lang.Thread.start0(Native Method)
        at java.base/java.lang.Thread.start(Thread.java:804)
        at okhttp3.internal.http2.Http2Connection.start(Http2Connection.java:515)
        at okhttp3.internal.http2.Http2Connection.start(Http2Connection.java:499)
        at okhttp3.internal.connection.RealConnection.startHttp2(RealConnection.java:299)
        at okhttp3.internal.connection.RealConnection.establishProtocol(RealConnection.java:288)
        at okhttp3.internal.connection.RealConnection.connect(RealConnection.java:169)
        at okhttp3.internal.connection.StreamAllocation.findConnection(StreamAllocation.java:257)
        at okhttp3.internal.connection.StreamAllocation.findHealthyConnection(StreamAllocation.java:135)
        at okhttp3.internal.connection.StreamAllocation.newStream(StreamAllocation.java:114)
        at okhttp3.internal.connection.ConnectInterceptor.intercept(ConnectInterceptor.java:42)
        at okhttp3.internal.http.RealInterceptorChain.proceed(RealInterceptorChain.java:147)
        at okhttp3.internal.http.RealInterceptorChain.proceed(RealInterceptorChain.java:121)
        at okhttp3.internal.cache.CacheInterceptor.intercept(CacheInterceptor.java:94)
        at okhttp3.internal.http.RealInterceptorChain.proceed(RealInterceptorChain.java:147)
        at okhttp3.internal.http.RealInterceptorChain.proceed(RealInterceptorChain.java:121)
        at okhttp3.internal.http.BridgeInterceptor.intercept(BridgeInterceptor.java:93)
        at okhttp3.internal.http.RealInterceptorChain.proceed(RealInterceptorChain.java:147)
        at okhttp3.internal.http.RetryAndFollowUpInterceptor.intercept(RetryAndFollowUpInterceptor.java:125)
        at okhttp3.internal.http.RealInterceptorChain.proceed(RealInterceptorChain.java:147)
        at okhttp3.internal.http.RealInterceptorChain.proceed(RealInterceptorChain.java:121)
        at okhttp3.RealCall.getResponseWithInterceptorChain(RealCall.java:264)
        at okhttp3.RealCall.execute(RealCall.java:93)
        at net.dv8tion.jda.internal.requests.Requester.execute(Requester.java:200)
        at net.dv8tion.jda.internal.requests.Requester.execute(Requester.java:140)
        at net.dv8tion.jda.internal.requests.Requester.execute(Requester.java:123)
        at net.dv8tion.jda.internal.requests.ratelimit.BotRateLimiter$Bucket.run(BotRateLimiter.java:468)
        at java.base/java.util.concurrent.Executors$RunnableAdapter.call(Executors.java:515)
        at java.base/java.util.concurrent.FutureTask.run(FutureTask.java:264)
        at java.base/java.util.concurrent.ScheduledThreadPoolExecutor$ScheduledFutureTask.run(ScheduledThreadPoolExecutor.java:304)
        at java.base/java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1128)
        at java.base/java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:628)
        at java.base/java.lang.Thread.run(Thread.java:835)
[111,061s][warning][os,thread] Failed to start thread - pthread_create failed (EAGAIN) for attributes: stacksize: 1024k, guardsize: 0k, detached.
Exception in thread "Thread-365" java.lang.OutOfMemoryError: unable to create native thread: possibly out of memory or process/resource limits reached
        at java.base/java.lang.Thread.start0(Native Method)
        at java.base/java.lang.Thread.start(Thread.java:804)
        at java.base/java.util.concurrent.ThreadPoolExecutor.addWorker(ThreadPoolExecutor.java:937)
        at java.base/java.util.concurrent.ThreadPoolExecutor.ensurePrestart(ThreadPoolExecutor.java:1583)
        at java.base/java.util.concurrent.ScheduledThreadPoolExecutor.delayedExecute(ScheduledThreadPoolExecutor.java:346)
        at java.base/java.util.concurrent.ScheduledThreadPoolExecutor.schedule(ScheduledThreadPoolExecutor.java:562)
        at java.base/java.util.concurrent.ScheduledThreadPoolExecutor.execute(ScheduledThreadPoolExecutor.java:705)
        at okhttp3.internal.http2.Http2Connection$ReaderRunnable.applyAndAckSettings(Http2Connection.java:738)
        at okhttp3.internal.http2.Http2Connection$ReaderRunnable.settings(Http2Connection.java:710)
        at okhttp3.internal.http2.Http2Reader.readSettings(Http2Reader.java:289)
        at okhttp3.internal.http2.Http2Reader.nextFrame(Http2Reader.java:141)
        at okhttp3.internal.http2.Http2Reader.readConnectionPreface(Http2Reader.java:80)
        at okhttp3.internal.http2.Http2Connection$ReaderRunnable.execute(Http2Connection.java:607)
        at okhttp3.internal.NamedRunnable.run(NamedRunnable.java:32)
        at java.base/java.lang.Thread.run(Thread.java:835)

我发现无论我做什么,线程堆栈大小总是显示为 1024。

我目前 运行 这个来自 VPS 的 .jar 文件具有以下规格:

我的问题是,有没有办法编辑这个线程堆栈大小?

另一个单独的问题:每当我使用 -Xss2g 时,它确实显示它超过了限制,但是有没有办法编辑这个限制?

您可以尝试配置 JDA 并更改默认值以减少内存占用。确保您还使用最新版本的 JDA(当前为 4.2。0_252)。

阅读常见问题解答:How can I reduce the memory footprint of my bot?

我居然想出来了。

我托管的提供商(我将不透露其名称)将我可以同时 运行 的线程数量限制为 1300 个线程。因为他们有固定数量的这个,所以我一直 运行ning 我的线程出现问题,这些错误也显示在问题中。

因为他们锁定了这个金额,我的选择受到限制,所以我决定改用供应商。