From 0f8059888151a2d3905c140cd2f4c3462f747bd4 Mon Sep 17 00:00:00 2001 From: ChomeNS <95471003+ChomeNS@users.noreply.github.com> Date: Sat, 15 Mar 2025 17:14:07 +0700 Subject: [PATCH] fix: small memory leak fix in QueryPlugin refactor: make the transactions and the uuids public for debugging/checking via servereval on production instance --- build-number.txt | 2 +- .../chomens_bot/plugins/QueryPlugin.java | 19 +++++++++++++++---- 2 files changed, 16 insertions(+), 5 deletions(-) diff --git a/build-number.txt b/build-number.txt index 3d2b7eb6..a3ace50d 100644 --- a/build-number.txt +++ b/build-number.txt @@ -1 +1 @@ -1786 \ No newline at end of file +1793 \ No newline at end of file diff --git a/src/main/java/me/chayapak1/chomens_bot/plugins/QueryPlugin.java b/src/main/java/me/chayapak1/chomens_bot/plugins/QueryPlugin.java index 4ed0892c..772b2519 100644 --- a/src/main/java/me/chayapak1/chomens_bot/plugins/QueryPlugin.java +++ b/src/main/java/me/chayapak1/chomens_bot/plugins/QueryPlugin.java @@ -11,13 +11,14 @@ import org.geysermc.mcprotocollib.network.event.session.DisconnectedEvent; import java.util.*; import java.util.concurrent.CompletableFuture; +import java.util.concurrent.TimeUnit; public class QueryPlugin extends Bot.Listener { private final Bot bot; - private long nextTransactionId = 0; - private final Map> transactions = new HashMap<>(); - private final List ids = new ArrayList<>(); + public long nextTransactionId = 0; + public final Map> transactions = new HashMap<>(); + public final List ids = new ArrayList<>(); public QueryPlugin (Bot bot) { this.bot = bot; @@ -59,7 +60,11 @@ public class QueryPlugin extends Bot.Listener { if (children.size() == 1) { future.complete(null); } else { - if (!(children.get(1) instanceof TextComponent)) return true; + if (!(children.get(1) instanceof TextComponent)) { + future.complete(null); + + return true; + } final String stringOutput = ((TextComponent) children.get(1)).content(); @@ -81,6 +86,12 @@ public class QueryPlugin extends Bot.Listener { final UUID id = UUID.randomUUID(); ids.add(id); + // prevent memory leak in case the command returns no output + bot.executor.schedule(() -> { + transactions.remove(transactionId); + ids.remove(id); + }, 5, TimeUnit.SECONDS); + return Triple.of(future, transactionId, id); }