From 21d4ef254deeee8cae5459c6cf4cfb996fb7040b Mon Sep 17 00:00:00 2001
From: Kai Reinhard <K.Reinhard@micromata.de>
Date: Sun, 06 Jan 2019 23:32:21 +0000
Subject: [PATCH] JProfiler session for optimizing heap space (a big memory leak fixed).

---
 borgbutler-core/src/main/java/de/micromata/borgbutler/BorgCommands.java |   21 +++++++++++++++------
 1 files changed, 15 insertions(+), 6 deletions(-)

diff --git a/borgbutler-core/src/main/java/de/micromata/borgbutler/BorgCommands.java b/borgbutler-core/src/main/java/de/micromata/borgbutler/BorgCommands.java
index 6d3e043..a5c6355 100644
--- a/borgbutler-core/src/main/java/de/micromata/borgbutler/BorgCommands.java
+++ b/borgbutler-core/src/main/java/de/micromata/borgbutler/BorgCommands.java
@@ -34,7 +34,7 @@
         BorgCommand command = new BorgCommand()
                 .setParams("--version")
                 .setDescription("Getting borg version.");
-        JobResult<String> jobResult = execute(command).getResult();
+        JobResult<String> jobResult = getResult(command);
         if (jobResult == null || jobResult.getStatus() != JobResult.Status.OK) {
             return null;
         }
@@ -55,7 +55,7 @@
                 .setCommand("info")
                 .setParams("--json") // --progress has no effect.
                 .setDescription("Loading info of repo '" + repoConfig.getDisplayName() + "'.");
-        JobResult<String> jobResult = execute(command).getResult();
+        JobResult<String> jobResult = getResult(command);
         if (jobResult == null || jobResult.getStatus() != JobResult.Status.OK) {
             return null;
         }
@@ -88,7 +88,7 @@
                 .setCommand("list")
                 .setParams("--json") // --progress has no effect.
                 .setDescription("Loading list of archives of repo '" + repoConfig.getDisplayName() + "'.");
-        JobResult<String> jobResult = execute(command).getResult();
+        JobResult<String> jobResult = getResult(command);
         if (jobResult == null || jobResult.getStatus() != JobResult.Status.OK) {
             log.error("Can't load archives from repo '" + repository.getName() + "'.");
             return;
@@ -130,7 +130,7 @@
                 .setArchive(archive.getName())
                 .setParams("--json", "--log-json", "--progress")
                 .setDescription("Loading info of archive '" + archive.getName() + "' of repo '" + repoConfig.getDisplayName() + "'.");
-        JobResult<String> jobResult = execute(command).getResult();
+        JobResult<String> jobResult = getResult(command);
         if (jobResult == null || jobResult.getStatus() != JobResult.Status.OK) {
             return;
         }
@@ -193,7 +193,9 @@
         if (jobResult == null ||jobResult.getStatus() != JobResult.Status.OK) {
             return null;
         }
-        return job.payload;
+        List<BorgFilesystemItem> items = job.payload;
+        job.cleanUp(); // payload will be released.
+        return items;
     }
 
     /**
@@ -221,10 +223,17 @@
                 .setDescription("Extract content of archive '" + archive.getName()
                         + "' of repo '" + repoConfig.getDisplayName() + "': "
                         + path);
-        JobResult<String> jobResult = execute(command).getResult();
+        JobResult<String> jobResult = getResult(command);
         return restoreDir;
     }
 
+    private static JobResult<String> getResult(BorgCommand command) {
+        BorgJob<Void> job = execute(command);
+        JobResult<String> jobResult = job.getResult();
+        job.cleanUp();
+        return jobResult;
+    }
+
     private static BorgJob<Void> execute(BorgCommand command) {
         Validate.notNull(command);
         return BorgQueueExecutor.getInstance().execute(command);

--
Gitblit v1.10.0