Android Question How to appication stable

fishwolf

Well-Known Member
Licensed User
Longtime User
i have finish a big application, but work fine only for few minuts, it crash for out of memory.

i have read many threads on forum but i don't resolve.

FIRST PROBLEM
with monitor utility i have indentify the first section and problem of memory leak
i have a list of strings and use a canvas object and MeasureStringWidth for truncate the long strings
when i change the orientation of phone i lost about 2 Mb with the only initialization of canvas object

what is the right method for destroy canvas object ?

B4X:
// Create List
CanvasText.Initialize(Activity)
   
   For i = 0 To ValuesItems.Size - 1
     
     // if don't run the function the memory leak is already present
     Label1.Text = TrimWidth(CanvasText, Label1.Text, Label1.Typeface, Label1.TextSize, LabelWidth)
  
  Next

Sub TrimWidth(CanvasText As Canvas, Text As String, Style As Typeface, Size As Int, MaxWidth As Int) As String
Dim Width As Int
   
   
   If Style.IsInitialized = False Then
       Style = Typeface.DEFAULT
   End If
   
   Width = CanvasText.MeasureStringWidth(Text, Style, Size)
   If Width >= MaxWidth Then
     Do Until Width < MaxWidth
       Text = Text.SubString2(0, Text.Length - 1)
       Width = CanvasText.MeasureStringWidth(Text, Style, Size)
     Loop
     Text = Text.SubString2(0, Text.Length - 3) & "..."
     Return Text
   Else
     Return Text
   End If
   
End Sub
 

fishwolf

Well-Known Member
Licensed User
Longtime User
Try to find the problematic component. See if this error happens if you don't add the action bar.
The memory leak problem is into admod banner :(
if i remove, memory heap is stable

do you think that the problem is into google library (i have update to may 2014) or into wrapper ?
 
Upvote 0

Periklis Koutsogiannis

Active Member
Licensed User
Longtime User
The AdMob wrapper is just a ViewWrapper of the native AdView component. I don't think that the wrapper is the problem.
 
Upvote 0

fishwolf

Well-Known Member
Licensed User
Longtime User
Try this:
1. Add this line to Process_Globals:
B4X:
Private oldAdView As JavaObject
2. Add this code in Activity_Create:
B4X:
If oldAdView.IsInitialized Then
     oldAdView.RunMethod("destroy", Array())
   End If
   Fn.CreateAdMob(Main.gAdId) '<-- original code
   Dim o As Object = Fn.m_AdBanner
   oldAdView = o
no, doesn't work fine
 
Upvote 0

fishwolf

Well-Known Member
Licensed User
Longtime User
Do you get the same error?
yes
B4X:
Create
Downsampling image due to lack of memory: 2
java.lang.OutOfMemoryError
   at android.os.Parcel.nativeReadString(Native Method)
   at android.os.Parcel.readString(Parcel.java:1473)
   at bwe.n(SourceFile:204)
   at aex.a(SourceFile:83)
   at afa.a(SourceFile:104)
   at aer.a(SourceFile:174)
   at aer.a(SourceFile:151)
   at afw.run(SourceFile:14)
   at afy.run(SourceFile:30)
   at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1076)
   at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:569)
   at java.lang.Thread.run(Thread.java:856)

Downsampling image due to lack of memory: 4

java.lang.OutOfMemoryError
   at android.os.Parcel.nativeReadString(Native Method)
   at android.os.Parcel.readString(Parcel.java:1473)
   at bwe.n(SourceFile:204)
   at aex.a(SourceFile:83)
   at afa.a(SourceFile:104)
   at aer.a(SourceFile:174)
   at aer.a(SourceFile:151)
   at afw.run(SourceFile:14)
   at afy.run(SourceFile:30)
   at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1076)
   at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:569)
   at java.lang.Thread.run(Thread.java:856)
 
Upvote 0

fishwolf

Well-Known Member
Licensed User
Longtime User
i have resolved memory lead od admob with

B4X:
Sub Pause
   If m_AdBanner.IsInitialized == True Then
     m_AdBanner.Pause
   End If
End Sub

Sub Resume
   If m_AdBanner.IsInitialized == True Then
     m_AdBanner.Resume
   End If
End Sub

but other application sections have little memory leak
 
Upvote 0

fishwolf

Well-Known Member
Licensed User
Longtime User
I have discovery that in each activities where i use admod the resources (bitmap, table, list) create a memory leak.

the larger the resources, the more memory is consumed

Any idea ?
 
Upvote 0

fishwolf

Well-Known Member
Licensed User
Longtime User
I have discovery that in each activities where i use admod the resources (bitmap, table, list) create a memory leak.

the larger the resources, the more memory is consumed

Any idea ?

i find into monitor this message.

B4X:
The Google Play services resources were not found. Check your project configuration to ensure that the resources are included.

i already add this line into main activities
#AdditionalRes: C:\Users\Administrator\AppData\Local\Android\android-sdk\extras\google\google_play_services\libproject\google-play-services_lib\res, com.google.android.gms

and

B4X:
Could not find class 'android.support.v4.app.FragmentActivity', referenced from method com.google.android.gms.common.GooglePlayServicesUtil.b
 
Last edited:
Upvote 0

fishwolf

Well-Known Member
Licensed User
Longtime User
You should call Pause and Resume from all of the activities.

This message is not related. The resources are found. If they are not found you will get an error about a missing resource.

I also encountered this message. Not sure why it shows.

I already done :)

I have add the "Locandine" feature into app, it display a image, each execution create a memory leak of 2-3 Mb

If i remove admob, memory leak is not present
B4X:
  'COMMENT THIS LINE 
  'Fn.CreateAdMob(Main.gAdId)
 

Attachments

  • MemoryLeakV3.zip
    395.5 KB · Views: 202
Upvote 0

fishwolf

Well-Known Member
Licensed User
Longtime User
ok, with this code work file without memory leak

Thanks Erel :)

B4X:
Sub Process_Globals

   Private DestroyObjects As JavaObject
   
End Sub

Sub Activity_Resume
   
   Fn.CreateAdMob(Main.gAdId)

End Sub

Sub Activity_Pause (UserClosed As Boolean)
   
   If DestroyObjects.IsInitialized Then
     DestroyObjects.RunMethod("destroy", Array())
   End If

   Dim OldObject As Object = Fn.m_AdBanner
   DestroyObjects = OldObject
   
End Sub
 
Upvote 0
Top