High Memory Usage - AdMob on Android (55 MB)

1,522 views
Skip to first unread message

Vairavan Srinivasan

unread,
Mar 5, 2017, 2:23:11 PM3/5/17
to Google Mobile Ads SDK Developers
Hello,

   I have a free and paid version of the app. The free version uses a Banner ad at the bottom. The app is simple and has just one activity.
The adview is created using application context and added to the view hierarchy.

  • Google Play services SDK version you're integrating with -> com.google.android.gms:play-services-ads:10.2.0
  • Version of the Google Play services apk installed on your device -> 9.8.75 (470-133986348)
  • model and OS version of the device that exhibits the issue -> Emulator, API 25, Skin Nexus 6P
  • Your SDK integration code, if relevant
  
  Use case is just to launch the activity and press back to finish the activity ( after the first ad is displayed ) and initiate GC via Android Studio's Memory Monitor and collect memory info.

Paid version (without ad)

                  Pss  Private  Private  SwapPss     Heap     Heap     Heap
Total Dirty Clean Dirty Size Alloc Free
------ ------ ------ ------ ------ ------ ------
Native Heap 14387 14328 0 0 19968 16386 3581
Dalvik Heap 2040 1816 0 0 9393 7345 2048
Dalvik Other 773 772 0 0
Stack 408 408 0 0
Ashmem 4 4 0 0
Other dev 12 0 12 0
.so mmap 1422 132 136 0
.apk mmap 376 0 52 0
.ttf mmap 43 0 0 0
.dex mmap 4523 16 3816 0
.oat mmap 2599 0 584 0
.art mmap 2010 1384 256 0
Other mmap 15 4 0 0
Unknown 1517 1516 0 0
TOTAL 30129 20380 4856 0 29361 23731 5629

App Summary
Pss(KB)
------
Java Heap: 3456
Native Heap: 14328
Code: 4736
Stack: 408
Graphics: 0
Private Other: 2308
System: 4893

TOTAL: 30129 TOTAL SWAP PSS: 0

Objects
Views: 0 ViewRootImpl: 0
AppContexts: 1 Activities: 0
Assets: 2 AssetManagers: 2
Local Binders: 15 Proxy Binders: 18
Parcel memory: 5 Parcel count: 22
Death Recipients: 0 OpenSSL Sockets: 2
WebViews: 0

SQL
MEMORY_USED: 0
PAGECACHE_OVERFLOW: 0 MALLOC_SIZE: 62

 
Free version (with banner ad)  

                  Pss  Private  Private  SwapPss     Heap     Heap     Heap
Total Dirty Clean Dirty Size Alloc Free
------ ------ ------ ------ ------ ------ ------
Native Heap 30474 30396 0 0 41088 32210 8877
Dalvik Heap 2531 2232 0 0 9914 7866 2048
Dalvik Other 1302 1300 0 0
Stack 800 800 0 0
Ashmem 160 132 0 0
Other dev 12 0 12 0
.so mmap 1809 148 232 0
.apk mmap 23780 96 21340 0
.ttf mmap 57 0 4 0
.dex mmap 8261 24 7036 0
.oat mmap 4788 0 1684 0
.art mmap 2823 2068 280 0
Other mmap 1077 4 1004 0
Unknown 7251 7248 0 0
TOTAL 85125 44448 31592 0 51002 40076 10925

App Summary
Pss(KB)
------
Java Heap: 4580
Native Heap: 30396
Code: 30564
Stack: 800
Graphics: 0
Private Other: 9700
System: 9085

TOTAL: 85125 TOTAL SWAP PSS: 0

Objects
Views: 6 ViewRootImpl: 0
AppContexts: 1 Activities: 0
Assets: 3 AssetManagers: 2
Local Binders: 30 Proxy Binders: 26
Parcel memory: 8 Parcel count: 32
Death Recipients: 0 OpenSSL Sockets: 0
WebViews: 2

SQL
MEMORY_USED: 0
PAGECACHE_OVERFLOW: 0 MALLOC_SIZE: 62

    The memory increase for the free app is ~55 MB ( Total Pss ), ~24 MB ( private dirty ) and ~27 MB ( private clean ).
No amount of GC reduces this memory despite the fact that the activity has been destroyed. The meminfo doesn't list any activity.
However, it does claim 6 views and 2 web views. I do call destroy() on my AdView when the activity is destroyed.

   The significant difference come from Native heap & apk mmap. How could i possibly debug this?
At some point my app launches a foreground service and users see this memory usage in running services and i get quite a few low ratings just because of the memory usage.

  Could anyone give some pointers?

- Vairavan

Ivan Bautista (Mobile Ads SDK Team)

unread,
Mar 6, 2017, 2:49:16 AM3/6/17
to Google Mobile Ads SDK Developers
Hi Vairavan,

To help us investigate further, could you provide us the following:
  • Sample app where issue is reproducible 
  • Copy of the hprof file for when the leak was encountered
  • Code snippets of your Mobile Ads SDK implementaiton
Regards,
Ivan Bautista
Mobile Ads SDK Team

Vairavan Srinivasan

unread,
Mar 6, 2017, 3:36:38 AM3/6/17
to Google Mobile Ads SDK Developers
Hi Ivan,

   Here is a sample app in GitHub (https://github.com/Ethan1983/TestAdMobAds2). It has a free and paid product flavors, please replace the ad unit id in adview_layout.xml and MainActivity. In this case, hprof doesn't have a leak. It is just that the overall memory usage in high (native and apk mmap). Note that i'm using application context to inflate the AdView.
Using activity context would cause a leak of the activity.

Here is the code snippet,

public class MainActivity extends AppCompatActivity {

private AdView mAdView;

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);

final ViewGroup adViewContainer = ( ViewGroup ) findViewById( R.id.adview_container );

MobileAds.initialize(getApplicationContext(), "ca-app-pub-4073123456879230~86334354");

// Avoid activity context as AdView internal has a static reference to the activity
mAdView = (AdView) LayoutInflater.from( getApplicationContext() ).inflate(
R.layout.adview_layout,
adViewContainer,
true /*attachToRoot*/ ).findViewById(R.id.adView);

AdRequest request;

if( BuildConfig.DEBUG )
{
final String android_id = Settings.Secure.getString(this.getContentResolver(), Settings.Secure.ANDROID_ID);
final String deviceId = MainActivity.md5(android_id).toUpperCase();

request = new AdRequest.Builder()
.addTestDevice(AdRequest.DEVICE_ID_EMULATOR)
.addTestDevice(deviceId)
.build();
}
else
{
request = new AdRequest.Builder().build();
}

try
{
mAdView.loadAd(request);
}
catch( AndroidRuntimeException e )
{
final Throwable cause = e.getCause();
if( cause!= null && cause.getMessage().contains( "com.android.webview" ) )
{
// WebView is being updated via Play Store. Ignore Adrequest for this session.
}
else
{
throw e;
}
}
}

@Override
protected void onResume()
{
super.onResume();

try
{
mAdView.resume();
}
catch( AndroidRuntimeException e )
{
final Throwable cause = e.getCause();
if( cause!= null && cause.getMessage().contains( "com.android.webview" ) )
{
// WebView is being updated via Play Store. Ignore Adrequest for this session.
}
else
{
throw e;
}
}
}

@Override
protected void onPause()
{
mAdView.pause();
super.onPause();
}

@Override
protected void onDestroy()
{
(( ViewGroup ) mAdView.getParent()).removeAllViews();
mAdView.destroy();
super.onDestroy();
}

Ivan Bautista (Mobile Ads SDK Team)

unread,
Mar 6, 2017, 5:49:49 AM3/6/17
to Google Mobile Ads SDK Developers
Hi Vairavan,

Thank you for bringing this to our attention. We will let the team know and update you.

Vairavan Srinivasan

unread,
Mar 12, 2017, 3:29:40 PM3/12/17
to Google Mobile Ads SDK Developers
Hi Ivan,

   Just wondering, if there is any recommendation for this usage?

Ivan Bautista (Mobile Ads SDK Team)

unread,
Mar 13, 2017, 2:53:24 AM3/13/17
to Google Mobile Ads SDK Developers
Hi Vairavan,

We have no updates nor any recommended workarounds yet from the team but rest assured that we will update this thread as soon as we get one.

Ivan Bautista (Mobile Ads SDK Team)

unread,
Mar 22, 2017, 6:47:55 AM3/22/17
to Google Mobile Ads SDK Developers
Hi Vairavan,

The high memory usage is because the SDK is using WebViews and WebView, in general, is expensive in memory consumption. In the meminfo you provided, the View and WebView objects that are still alive even after finishing the app are just expected behavior of how Android keeps the activity in memory even after finishing the app.

Eventually, the app process will be killed especially if memory is needed elsewhere but this is handled by Android and no extra handling should be done on your end.

Vairavan Srinivasan

unread,
Mar 23, 2017, 2:42:06 AM3/23/17
to Google Mobile Ads SDK Developers
Hi Ivan,

  Thanks for the update. In my case, i used the application context for the AdView and the activity is actually garbage collected. The meminfo doesn't list any activities (just that the views aren't GCed). Besides, the app actually starts a foreground service and the memory is not reclaimed and users tend to notice this memory usage.

- Vairavan

Andrey Permyakov

unread,
Mar 23, 2018, 4:35:28 PM3/23/18
to Google Mobile Ads SDK Developers
I have the same problem. How can I remove it?

четверг, 23 марта 2017 г., 9:42:06 UTC+3 пользователь Vairavan Srinivasan написал:

mobileadssdk-a...@google.com

unread,
Mar 25, 2018, 11:55:54 PM3/25/18
to Andrey Permyakov, Google Mobile Ads SDK Developers
Hi Andrey,

This issue was previously reported to us and was deemed as working as intended. The team was not able to find any evidence that the Mobile Ads SDK was causing these issues and would most likely come from Android's WebView.

That said, if you can provide to us the following information (preferably using the Reply Privately to Author option), then we may be able to investigate this issue further:
  • Sample application/project we can use to replicate the issue, as well as the steps to replicate
  • A Trace Log (.hprof) file showing the issue
Regards,
Joshua Lagonera
Mobile Ads SDK Team

--

---
You received this message because you are subscribed to the Google Groups "Google Mobile Ads SDK Developers" group.
To unsubscribe from this group and stop receiving emails from it, send an email to google-admob-ads-sdk+unsub...@googlegroups.com.
To post to this group, send email to google-admob-ads-sdk@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/google-admob-ads-sdk/2fbf5e51-58b9-4bdd-91b4-1672ff2e3a09%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.
Reply all
Reply to author
Forward
0 new messages