When I rotate the phone, the app consumes more ram in each rotation

D

Deleted member 30048

Guest
Every time I rotate the phone, the application consumes more and more ram

When the application is run for the first time, the app consumes 18 mb of ram. If I rotate the phone the app consumes 27 mb, if I rotate again 36 mb, 49, 57, 72, ...

The same thing happens when I open a layout, back to the main layout, open again the layout, back again to main layout. The app increasingly consumes more ram.

Android does this on purpose and then automatically clean the ram or the problem is mine because I do not clean the activity correctly?

This is my code:

B4X:
Sub Process_Globals
   Dim Sound = True As Boolean
   Dim Vibrate = True As Boolean
End Sub

Sub Globals
   Dim Adview1 As AdView
   Dim Panel1 As Panel
   Dim Beep As PhoneVibrate
End Sub

Sub Activity_Create(FirstTime As Boolean)
   'Load the background
   Dim Background As BitmapDrawable
   If (Activity.height > Activity.Width) Then
      Background.Initialize(LoadBitmap(File.DirAssets, "background.png"))
   Else
      Background.Initialize(LoadBitmap(File.DirAssets, "backgroundlandscape.png"))
   End If
   Background.Gravity = Gravity.FILL
   Activity.Background = Background

   'Create the menu
   Activity.AddMenuItem("Sound", "SoundMenu")
   Activity.AddMenuItem("Vibration", "VibrationMenu")
   
   'Load principal screen
   LoadLayoutToPanel("FirstLayout")
   
   'Load ads
   Load_Ads
End Sub

Sub Activity_Resume

End Sub

'User click to button1, open the second layout
Sub Button1_Click
   LoadLayoutToPanel("SecondLayout")
End Sub

'User click to back in the second layout, open the first layout.
Sub Back_Click
   LoadLayoutToPanel("FirstLayout")
End Sub

Sub Load_Ads
   Adview1.Initialize2("Ad", "xxxx", Adview1.SIZE_SMART_BANNER)
   Dim height As Int
   If GetDeviceLayoutValues.ApproximateScreenSize < 6 Then
       'phones
      If 100%x > 100%y Then height = 32dip Else height = 50dip
   Else
       'tablets
       height = 90dip
   End If
   Activity.AddView(Adview1, 0dip, 100%y - height, 100%x, height)
   Adview1.LoadAd
End Sub

'LOAD A LAYOUT IN A PANEL.
Sub LoadLayoutToPanel (layout As String)
   If Adview1.IsInitialized Then
      RemoveView (Adview1) 'Remove all views except adview.
   Else
      Activity.RemoveAllViews 'Remove all views
   End If
   Panel1.Initialize("") 
   Activity.AddView(Panel1, 0, 0, 100%x, 100%y)
   Panel1.LoadLayout(layout)
   Panel1.SendToback 'If I don't send to back the panel. The panel hides the ads.
   Panel1.Color = Colors.Transparent
End Sub

'REMOVE ALL VIEWS EXCEPT ADVIEW.
Sub RemoveView (V As View)
    For i = 0 To Activity.NumberOfViews - 1
        If Activity.GetView(i) <> V Then
          Activity.RemoveViewAt(i)
        End If
    Next
End Sub

Sub SoundMenu_Click
   If Sound == True Then
      Sound = False
   Else
      Sound = True
   End If
End Sub

Sub VibrationMenu_Click
   If Vibrate == True Then
      Vibrate = False
   Else
      Vibrate = True
      Beep.Vibrate(700) 'Vibrate 700 ms.
   End If   
End Sub

My last question, I do not want to use several activities because every time I open a new activity reloads the ads, so my project loads the layouts in panels. Is correct the code I use to clean the layouts?

Thanks a lot and sorry for my english!
 

Erel

B4X founder
Staff member
Licensed User
Longtime User
1. You can load the two bitmaps once and store them as process global variables. Then use the loaded bitmaps instead of loading them again.
2. 18mb is a lot. Seems like you are loading very large images. Use LoadBitmapSample instead.

3. This code is (probably) wrong:
B4X:
Sub RemoveView (V As View)
    For i = 0 To Activity.NumberOfViews - 1
        If Activity.GetView(i) <> V Then
           Activity.RemoveViewAt(i)
        End If
    Next
End Sub

Instead call: Panel1.RemoveView. It will remove the panel with all its views from the activity.

4. Usually it is better to use multiple activities even if you show ads.
 
Upvote 0
D

Deleted member 30048

Guest
Thanks Erel, I will use LoadBitmapSample and panel1.removeallviews but, is normal that every time I rotate the phone,the application consumes more ram?
 
Upvote 0

jgmdavies

Member
Licensed User
Longtime User
@cbc551

Hi,

One reason that your RemoveView code is wrong is that when you remove the ith view, you then go on to look at the i+1th, i.e., it's an ascending loop. But the item now at i+1 was previously at i+2, so you're missing items. To get round this problem you would need a descending loop.

Anyway, Erel's the man, so I would follow his suggestion!

HTH
Jim
 
Upvote 0
D

Deleted member 30048

Guest
Hello jgmdavies and thank you for you suggestion.

Well, I've used panel1.removeallviews and I think that it not is best. I say this because I have put in the next line "log(Activity.NumberofViews)" and every time that I load a new layout, the log tell me that exist + 1 view. I don't know if you understand me because my english is not so good.

In other words, when I open FirstLayout I have 2 views (Layout and addview), If I open SecondLayout I have 3 views, if I back to FirstLayout I have 4, ...

Finally, I used a mix and the log always tell me that exist only 2 views:

B4X:
Sub LoadLayoutToPanel (layout As String)
   'Remove the panel like says Erel.
   If Panel1.IsInitialized Then 
      Panel1.RemoveAllViews 
   End If
   'Later remove the views.
    If Adview1.IsInitialized Then
        RemoveView (Adview1) 'Remove all views except adview.
    Else
        Activity.RemoveAllViews 'Remove all views
    End If
    Panel1.Initialize("") 
    Activity.AddView(Panel1, 0, 0, 100%x, 100%y)
    Panel1.LoadLayout(layout)
    Panel1.SendToback 'If I don't send to back the panel. The panel hides the ads.
    Panel1.Color = Colors.Transparent
End Sub

'REMOVE ALL VIEWS EXCEPT ADVIEW.
Sub RemoveView (V As View)
   'I change the code like says jgmdavies
    For i = Activity.NumberOfViews - 1 To 0 Step -1
        If Activity.GetView(i) <> V Then
           Activity.RemoveViewAt(i)
        End If
    Next
End Sub

Thanks and best regards!
 
Upvote 0
Top