6

I am trying to place ads inside my app. According to Admob Documentation I have to initialize Mobile Ads SDK

MobileAds.initialize(this, "YOUR_ADMOB_APP_ID");

This causes spike in high ram usage in code.

enter image description here

But if i remove this line then ram usage drops & and this line of code doesn't seem to have any affect on servering ads inside the app.

enter image description here

Also when requesting ad from admob ram usage again spike up and causes 3-4 GC events on app startup. I believe this is memory leak.

Here's how i am requesting ad in onCreate method

AdRequest request = null;
        if (BuildConfig.DEBUG) {
            //Facebook Audience Network
            List<String> testDeviceId = new ArrayList<>();
            testDeviceId.add("TESTID");//Redmi Note 3
            testDeviceId.add("TESTID");//Moto G 1st Gen

            AdSettings.addTestDevices(testDeviceId);

            //Google Ad-mob
            request = new AdRequest.Builder()
                    .addTestDevice("TESTID")//Redmi Note 3
                    .addTestDevice("TESTID")//Mot G 1st Gen
                    .build();
        } else {
            request = new AdRequest.Builder()
                    .build();
        }

        AdView mAdView = findViewById(R.id.adView);
        mAdView.loadAd(request);

When loading this banner ads several GC event are kicked in. If i don't load ads GC event are never kicked in.

enter image description here

Is this behavior normal with admob? How can i resolve this?

Skyyy
  • 1,539
  • 2
  • 23
  • 60

6 Answers6

1

Google AdView has WebView with a lot of animation inside. It will heats up all mobile CPU. AdView take 30% of CPU.

Solution : You can also add custom listeners to destroy after some time and recreate in order to handle it even better. Serverside there is also a parameter telling the app ad how soon should ask for a new ad, I am not sure if it exist in all cases but it is there for DFP accounts.
here is the easiest way i would suggest

new Handler(new Handler.Callback() {
@Override
public boolean handleMessage(Message msg) {
  if (!isBeingDestroyed) {
      final AdRequest adRequest = new AdRequest();
      final AdView adView = (AdView) findViewById(R.id.ad);
      adView.loadAd(adRequest);
  }
}).sendEmptyMessageDelayed(0, 1000); 

Here is the link that provide complete solution for this.

Hope it will help you.

Hemant Parmar
  • 3,924
  • 7
  • 25
  • 49
0

Yes, that behaviour is normal. AdView is a dynamic WebView which consumes about 50mb RAM. Most of the memory leaks occur when you rotate screen and instance of previous Activity is attached to an element like listener or thread. Here are some examples. To check whether your app leaks or not you can use LeakCanary or Android Studio.

To check leaks in Android Studio

  1. Start Memory Profiler
  2. Select Memory and "Dump Java Heap"
  3. Export file as .hprof file
  4. Drag .hprof file to Android Studio and look for Analyzer Tasks and push run button to check if your activity leaks.
Thracian
  • 43,021
  • 16
  • 133
  • 222
0

Your app is still within an acceptable limit of RAM usage for most devices.

Marcus
  • 164
  • 1
  • 13
0

You can put android:largeHeap="true" in your AndroidManifest.xml file, so that your users won't get affected.

0

You are absolutely right. MobileAds.initilize() can be removed, and the application will work, BUT. When you request the first load of ads, the initilize() method will be called under the hood. I recommend that you call this method as soon as possible. For example, in your Application class. Also, starting from version Admob 21.0.0, you can optimize the initialization process. Just add the following lines to your manifest

 <meta-data
        android:name="com.google.android.gms.ads.flag.OPTIMIZE_INITIALIZATION"
        android:value="true"/>
    <meta-data
        android:name="com.google.android.gms.ads.flag.OPTIMIZE_AD_LOADING"
        android:value="true"/>
-1

i dont know if this will help but if you care about ram usage and you use ads in many activities you could start the ad mob from application class and that case the ad will only initialized once

pola alper
  • 194
  • 1
  • 10