A lot of activities, Activity.Finish and OutOfMemory

alwaysbusy

Expert
Licensed User
Longtime User
Sounds like a Guy Ritchie movie title unfortunately it's a problem I have with a project.

I have a client who wants me to build a huge app with a lot of different activities. The previous activity may be completely forgotten once I open another activity. This is the design the client wants and in theory it should work. Additionaly, there must be NO transition effect between the activities, so (unless someone knows a solution for this) CallSubDelayed() cannot be used.

The attached test project contains 16 activities, all with the pretty much the same code. The VBBitmap lib is used to show the memory stats.
Link to VBBitmap lib: http://www.b4x.com/forum/additional-libraries-classes-official-updates/19110-beta-vbbitmap-extended-safer-loadbitmapsample.html#post109992

I use Activity.Finish, clean up all my objects, stop timers etc but still the activity remains in memory.

This is tested on a device with screen res 800x1200 (Galaxy Tab 2) but it happens on all devices eventually. On the Tab each screen takes up about 4MB. Once I move to the next activity, the 4MB is never freed and after some time I get the dreadful out of memory.

B4X:
#Region  Activity Attributes 
   #FullScreen: False
   #IncludeTitle: False
#End Region

Sub Process_Globals
   'These global variables will be declared once when the application starts.
   'These variables can be accessed from all modules.
   
End Sub

Sub Globals
   'These global variables will be redeclared each time the activity is created.
   'These variables can only be accessed from this module.
   Dim ScreenCanvas As Canvas
   Dim ScreenTimer As Timer
   Dim DebugLabel As Label
End Sub

Sub Activity_Create(FirstTime As Boolean)
   'Do not forget to load the layout file created with the visual designer. For example:
   ScreenCanvas.Initialize(Activity)   
   
   DebugLabel.Initialize("")
   Activity.AddView(DebugLabel, 25%x,0,50%x,12.5%x)
   If DebugLabel.IsInitialized Then
      Dim sb As SmartBitmap
      DebugLabel.Text = NumberFormat2(sb.MaxMem, 0, 2, 2, True) & "MB" & CRLF & NumberFormat2(sb.heapsize, 0, 2, 2, True) & "MB"
   End If   
   
   ScreenTimer.Initialize("ScreenTimer", 1000)
   ScreenTimer.Enabled = True
End Sub

Sub Activity_Resume

End Sub

Sub Activity_Pause (UserClosed As Boolean)
   Activity.Finish
End Sub

Sub Activity_Touch (Action As Int, X As Float, Y As Float)
   Select Case Action
      Case Activity.ACTION_UP
         UnloadScreen
         StartActivity(act2)
         modGlobal.SetAnimation("noanim","noanim")         
         Activity.Finish
   End Select
End Sub

Sub ScreenTimer_Tick
   If DebugLabel.IsInitialized Then
      Dim sb As SmartBitmap
      DebugLabel.Text = NumberFormat2(sb.MaxMem, 0, 2, 2, True) & "MB" & CRLF & NumberFormat2(sb.heapsize, 0, 2, 2, True) & "MB"      
   End If   
End Sub

Sub LoadScreen()
   ScreenCanvas.DrawColor(Colors.RGB(Rnd(0,256),Rnd(0,256),Rnd(0,256)))
End Sub

Sub UnloadScreen()
   ScreenTimer.Enabled = False
   ScreenCanvas = Null
End Sub

And the SetAnimation sub:
B4X:
Public Sub SetAnimation(InAnimation As String, OutAnimation As String)
    Dim r As Reflector
    Dim package As String
    Dim In, out As Int
    package = r.GetStaticField("anywheresoftware.b4a.BA", "packageName")
    In = r.GetStaticField(package & ".R$anim", InAnimation)
    out = r.GetStaticField(package & ".R$anim", OutAnimation)
    r.Target = r.GetActivity
    r.RunMethod4("overridePendingTransition", Array As Object(In, out), Array As String("java.lang.int", "java.lang.int"))
End Sub

I do know this type of question has been asked some times on the forum and I've read them all. Any help would be more than welcome.

Thanks in advance
Alwaysbusy
 

Attachments

  • TestLotOfActivities.zip
    19.5 KB · Views: 307

Erel

B4X founder
Staff member
Licensed User
Longtime User
CallSubDelayed doesn't add any transition. It actually uses StartActivity to start the next activity.

Removing the canvas will most probably solve the memory issue. If you it is really required (it is not really required in the code you posted) then you should create one mutable bitmap and use it for all activities.
 
Upvote 0

alwaysbusy

Expert
Licensed User
Longtime User

Thanks for clarifying that! Thought setting ScreenCanvas = Null was recycling the bitmap.
 
Upvote 0
Cookies are required to use this site. You must accept them to continue using the site. Learn more…