App memory leaks with Apium 2.0

Hi everyone!
I have a following problem:

I used appium java client 7.6.0. and appium 1.22.2
After every test I need to close my app and then start it again.
And for this I used method androidDriver.launchApp() in @AfterMethod (testNg) which actually do the same as I describe above.

Now I update java client to 8.0.0 and appium to 2.0.0-beta.30
launchApp() method is no more present in java client library (deprecated)
Here I find Github methods like terminateApp() and activateApp()

Are these two methods do the same as one launchApp?
Because now very often instead of openning app when I use activateApp() method another app - Leaks openning instead.

This problem was never appears with appium client 7 and appium 1.22

Is this is a proper way to close and restart app using terminateApp() and activateApp() in appium 8.0?

Here is video: https://fex.net/ru/s/vrb2pa8
And this is code of afterMethod

 @AfterMethod(alwaysRun = true)
  public void closeApp() {
    if (BaseActivity.getDriver() != null) {
      AndroidDriver wrappedAndroidDriver = WrappedDriver.getWrappedAndroidDriver();
      boolean isAppSuccessfullyTerminated = AndroidAppHelper.terminateAndroidApp(
          wrappedAndroidDriver);
      if (isAppSuccessfullyTerminated) {
        AndroidAppHelper.activateAndroidApp(wrappedAndroidDriver);
        WaitHelper.waitUntilAndroidAppWillBeActivated(wrappedAndroidDriver);
      } else {
        throw new IllegalStateException("Application was terminated with errors");
      }
    }
  }

I use just quit driver after test.

ok, thanks I nee to change logic to check your advice, because now I create driver in @BeforeTest method, and this driver lives during some scope of tests methods, and in @AfterTest I quite driver.

In @AfterMethod I just restart my app
I will try your approach

I start driver in ‘beforeMethod’ and quit in ‘afterMethod’

Yes, this is works

But I have measure execution time above method where I create one driver for several test methods (up to 50), and in afterMethod I just try to terminate app and then activate it,

and new one where I create driver in beforeMethod, and kill it in afterMethod().

I dont have memory leaks any more (with new aproach), but execution time is:
old method - 2.5 seconds
new method - 8.5 seconds

And this is huge duffirents for hundreds of tests.
Still dont get it why we have memory leaks when invoke terminateApp() and activateApp():upside_down_face:

Driver start took time. e.g. with our app fast driver start takes 5-7 sec and with our 900 tests … yes takes time. we run with 12 Android phones to speedup.
Why not use activateApp and terminateApp by package name? Or even try pure adb command to kill and start?

I am not sure that I understand this part

Why not use activateApp and terminateApp by package name?

Because that is how I do now, with new appium client I can not just invoke activateApp() or terminateApp() without parameter (bundle id)

My methods look like this

public static boolean terminateAndroidApp(AndroidDriver driver) {
    log.info("Terminating android app...");
    return driver.terminateApp("org.wikipedia.dev");
  }

 public static void activateAndroidApp(AndroidDriver driver) {
    log.info("Activating android app...");
    driver.activateApp("org.wikipedia.dev");
  }

I will try to use pure adb command

Btw you may check server logs to compare. Appium also sends adb command to activate app.