Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Memory Leak from the AWSAppSyncClient #13

Closed
dy08 opened this issue May 22, 2018 · 5 comments
Closed

Memory Leak from the AWSAppSyncClient #13

dy08 opened this issue May 22, 2018 · 5 comments
Labels
investigating This issue is being investigated

Comments

@dy08
Copy link

dy08 commented May 22, 2018

What happened:
When working off from the official sample app, aws-mobile-appsync-events-starter-android,
I kept getting memory leaks from the AWSAppSyncClient instance,
which is declared as a static volatile variable in ClientFactory.java.

In com.amazonaws.postsapp:1.0:1.
* com.amazonaws.postsapp.PostsActivity has leaked:
* GC ROOT static com.amazonaws.postsapp.ClientFactory.client
* references com.amazonaws.mobileconnectors.appsync.AWSAppSyncClient.mApolloClient
* references com.apollographql.apollo.ApolloClient.subscriptionManager
* references com.amazonaws.mobileconnectors.appsync.subscription.RealSubscriptionManager.subscriptionsById
* references java.util.HashMap.table
* references array java.util.HashMap$HashMapEntry[].[11]
* references java.util.HashMap$HashMapEntry.value
* references com.amazonaws.mobileconnectors.appsync.subscription.SubscriptionObject.listeners
* references java.util.HashSet.backingMap
* references java.util.HashMap.table
* references array java.util.HashMap$HashMapEntry[].[0]
* references java.util.HashMap$HashMapEntry.key
* references com.amazonaws.postsapp.PostsActivity$3.this$0 (anonymous implementation of com.amazonaws.mobileconnectors.appsync.AppSyncSubscriptionCall$Callback)
* leaks com.amazonaws.postsapp.PostsActivity instance

AppSync SDK Versions tried : 2.6.16 & 2.6.19

What I have tried:

  • Setting the AWSAppSyncClient to null on Activity's onDestroy method.
    --> This solved the memory leak from the AwsAppSyncClient instance.
    --> But I got more memory leaks from
    1. AppSyncOfflineMutationManager, which references mContext
In com.amazonaws.postsapp:1.0:1.
* com.amazonaws.postsapp.PostsActivity has leaked:
* GC ROOT android.os.HandlerThread.<Java Local>
* references com.amazonaws.mobileconnectors.appsync.AppSyncOfflineMutationManager$NetworkUpdateHandler.this$0
* references com.amazonaws.mobileconnectors.appsync.AppSyncOfflineMutationManager.mContext
* leaks com.amazonaws.postsapp.PostsActivity instance
  1. ThreadPoolExecutor in ApolloClient.Builder's SubscriptionManager, which references Activity

(I guess I can pass in a custom Executor when instantiating an AWSAppSyncClient instance, but I couldn't figure out quite how to do this)

In com.amazonaws.postsapp:1.0:1.
* com.amazonaws.postsapp.PostsActivity has leaked:
* GC ROOT thread java.lang.Thread.<Java Local> (named 'Apollo Dispatcher')
* references java.util.concurrent.ThreadPoolExecutor.threadFactory
* references com.apollographql.apollo.ApolloClient$Builder$1.this$0 (anonymous implementation of java.util.concurrent.ThreadFactory)
* references com.apollographql.apollo.ApolloClient$Builder.subscriptionManager
* references com.amazonaws.mobileconnectors.appsync.subscription.RealSubscriptionManager.subscriptionsById
* references java.util.HashMap.table
* references array java.util.HashMap$HashMapEntry[].[4]
* references java.util.HashMap$HashMapEntry.value
* references com.amazonaws.mobileconnectors.appsync.subscription.SubscriptionObject.listeners
* references java.util.HashSet.backingMap
* references java.util.HashMap.table
* references array java.util.HashMap$HashMapEntry[].[0]
* references java.util.HashMap$HashMapEntry.key
* references com.amazonaws.postsapp.PostsActivity$3.this$0 (anonymous implementation of com.amazonaws.mobileconnectors.appsync.AppSyncSubscriptionCall$Callback)
* leaks com.amazonaws.postsapp.PostsActivity instance

Some parts of the code in the SDK hold on to the references to the Context.

  • Most of these seem to come from the Handler or Thread related classes

I can't think of a good way to clear these references for the garbage collection to work correctly when the application is finished. Would making the SDK receive a WeakReference of Context be a solution to this problem?

If there are errors in the way I set up my project or my understanding of the code is incorrect,
please let me know.

@minbi
Copy link
Contributor

minbi commented May 31, 2018

Thanks for the detailed report. We will look into removing these references as much as possible.

@minbi minbi added the investigating This issue is being investigated label May 31, 2018
@minbi
Copy link
Contributor

minbi commented Jun 14, 2018

We should have patched the issues here in version 2.6.20. Re-open if necessary

@minbi minbi closed this as completed Jun 14, 2018
@dy08
Copy link
Author

dy08 commented Jun 26, 2018

Hello,
I have been getting memory leaks from 2.6.20 as well.
One of the lines point to AppSyncOfflineMutationInterceptor.java line # 223
this.mContext =context;
I don't see any reuse of this variable, so please take time to see if this can be removed.

@minbi
Copy link
Contributor

minbi commented Jun 28, 2018

We will look into removing this in the next release.

@minbi
Copy link
Contributor

minbi commented Jun 28, 2018

Please see version 2.6.21

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
investigating This issue is being investigated
Projects
None yet
Development

No branches or pull requests

2 participants