Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

High ram usage with admob

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?

like image 721
Skyyy Avatar asked Jan 22 '18 15:01

Skyyy


1 Answers

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.

like image 82
Hemant Parmar Avatar answered Oct 22 '22 00:10

Hemant Parmar