Android Question Admob Memory Leak

marcelo_efn

Member
Licensed User
Hi everyone
My application is getting more memory consumption when I use Admob banners.
I found similar issues reporting memory leak:

https://stackoverflow.com/questions...-destroy-method-inside-a-recyclerview-adapter
https://stackoverflow.com/questions/9558708/admob-memory-leak-avoiding-by-using-empty-activity

A solution presented was to destroy the View Admob with destroy().
Does anyone know how to implement this method for this Admob case?

I'm trying to use jo.RunMethod("destroy", Null) but it's not working. I don't know how to use it.


B4X:
Sub Globals
    Private BannerAd As AdView

End Sub

Sub Activity_Create(FirstTime As Boolean)
    Dim adSize As JavaObject
    adSize.InitializeNewInstance("com.google.android.gms.ads.AdSize", Array(320, 50))
    BannerAd.Initialize2("BannerAd", "ca-app-pub-xxxxxxxxxxxx/xxxxxxxxx", adSize)
    panel1.AddView(BannerAd,(panel1.Width/2) -160dip,0dip,320dip,50dip)
    BannerAd.LoadAd

End Sub

Sub Activity_Pause (UserClosed As Boolean)
    Dim jo As JavaObject = BannerAd
    jo.RunMethod("destroy", Null)

End Sub

I tried using BannerAd.RemoveView, panel1.RemoveAllViews but it didn't work.
.
 

marcelo_efn

Member
Licensed User
Hi @Erel

My application worked fine without Admob. After I added Admob it was very slow.
The application starts fine but as time goes by, it gets very slow. But the application does not abort with errors.
The application has some Activitys and each one has an Admob banner. When navigating between these Activitys, the application is slowing down.
If you remove Admob the application is stable, it works well and the memory too.

I think about destroying the View Admob as a solution. What do you think?
 
Upvote 0

Erel

B4X founder
Staff member
Licensed User
Longtime User
Calling destroy should release the memory. Note that your current code is wrong as it will destroy the ad when the user switches to a different activity or home screen, and it only creates the ad when the activity is created.

Best solution, as always, is to switch to B4XPages. This will allow you to create a single ad and move it between the pages.
 
Upvote 0

marcelo_efn

Member
Licensed User
This code of mine doesn't work. I don't know how to use JavaObject to destroy View Admob.

When the user switches to another Activity, it is better for the Banner to be destroyed, as I use another Banner with another value category.
I work with banners registered at Admob with different values. This generates greater financial gains with Admob. Depending on the Activity, I put a banner that pays more. Therefore, the Banner must be destroyed when the user switches to another Activity.

Please teach me how to destroy the View Admob. Put some example code so I can implement it.

I want to run a test by destroying the View Admob to see if the application will be stable or if there will be other problems.
 
Upvote 0
Top