Skip to content

Memory leak: WebMessageListenerHolder retains KlaviyoWebView (removeWebMessageListener never called) #430

@esantamari-c-arlo

Description

@esantamari-c-arlo

Checklist

  • I have read the contributing guidelines
  • I have determined whether this bug is also reproducible in a vanilla Android project
  • If possible, I've reproduced the issue using the master branch or latest release of this package.
  • This issue hasn't been addressed in an existing GitHub issue or pull request.

Description

A memory leak occurs when dismissing Klaviyo in-app forms because WebViewCompat.removeWebMessageListener() is never called after registering a WebMessageListener.

As a result, WebMessageListenerHolder retains a reference to KlaviyoWebView, preventing the associated KlaviyoFormsOverlayActivity and its view hierarchy from being garbage-collected.

The issue was reproduced on version 4.1.0. A review of the SDK source code indicates that the missing cleanup is not implemented in newer versions or the current master branch.

Expected behavior

After KlaviyoFormsOverlayActivity is destroyed (form dismissed or unregisterFromInAppForms called), the KlaviyoWebView, its WebMessageListenerHolder, and the hosting Activity should be fully garbage-collected with no retained references.

Actual behavior

After dismissing an in-app form, WebMessageListenerHolder retains KlaviyoWebView, which in turn retains the destroyed KlaviyoFormsOverlayActivity and its entire view hierarchy.

LeakCanary reports the following reference chain:

WebMessageListenerHolder → KlaviyoWebView → KlaviyoFormsOverlayActivity → view hierarchy

Approximately 93 KB (1735 objects) are retained. The retained references persist for the lifetime of the process and accumulate if forms are repeatedly opened and dismissed, increasing the risk of memory pressure or OOM on low-memory devices.

Steps to reproduce

1. Initialize the Klaviyo SDK and register for in-app forms
2. Trigger an in-app form to display
3. Dismiss the in-app form (tap close or CTA)
4. Observe with LeakCanary or a heap dump that KlaviyoWebView is retained by WebMessageListenerHolder

The Klaviyo Android SDK version information

4.1.0

Device Information

Samsung Galaxy S22. Could not reproduce on OnePlus Open or emulators (API 28, 29, 33, 34)

Android Studio Version

Android Studio Otter 3 Feature Drop | 2025.2.3

Android API Level

36 (android 16)

Metadata

Metadata

Assignees

Type

No type

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions