Android Question Cycling through tasks with timers - memory leak?

togax

Member
Licensed User
I've written an app that polls an FTP server checking for a new data file and if it's there the app downloads it. The app also uploads a data file to the FTP server if one is created by my app. It all works fine. The app uses FTP.List, FTP.DownloadFile, FTP.UploadFile and FTP.DeleteFile in combination with their corresponding completed events to check the result and decide what task to perform next. I also use a short timer in case the connection stalls for whatever reason, to close the connection and try again much later. The stripped down code below is not a true reflection of the basic structure of my original app but it's close enough and does illustrate my point. If left running it will show the amount of free memory steadily decreasing albeit slightly. What, if anything is wrong with the structure of the code? Is there a better way to keep cycling through various sequential tasks endlessly (until of course the user pauses or kills the app)?

B4X:
Sub Process_Globals
    'These global variables will be declared once when the application starts.
    'These variables can be accessed from all modules.
    Dim Timer1 As Timer
    Dim NextTask As Int
    Dim debug1 As Int
    Dim r As Reflector    'from library Reflection

End Sub

Sub Service_Create
    'This is the program entry point.
    'This is a good place to load resources that are not specific to a single activity.
    debug1 = 1
    Timer1.Initialize("TimeOut1",5000)    'main timer
    Timer1.Enabled=False
   
End Sub

Sub Service_Start (StartingIntent As Intent)
    Log("Service_Start")
    MainLoop(0)
End Sub

Sub MainLoop (Task As Int)
    If debug1 = 1 Then Log("MainLoop Task = " & Task)
    If Task = 0 Then
        'do something once off only
        NextTask = 1: Timer1.Interval = 10: Timer1.Enabled=True
    else If Task = 1 Then    '1st task
        'do something else
        NextTask = 2: Timer1.Interval = 10: Timer1.Enabled=True
    else if Task = 2 Then    '2nd task
        'do something else
        NextTask = 3: Timer1.Interval = 10: Timer1.Enabled=True
    else if Task = 3 Then    '3rd task
        'go back to the first task
        NextTask = 1: Timer1.Interval = 5000: Timer1.Enabled=True
    End If
End Sub

Sub TimeOut1_Tick
    Timer1.Enabled=False
    If debug1 = 1 Then Log("TimeOut1; NextTask = " & NextTask)
    r.Target = r.RunStaticMethod("java.lang.Runtime", "getRuntime", Null, Null)
    Log("Free Memory = " & (r.RunMethod("freeMemory")/(1024*1024)) & " MB")
    MainLoop(NextTask)
End Sub

'Return true to allow the OS default exceptions handler to handle the uncaught exception.
Sub Application_Error (Error As Exception, StackTrace As String) As Boolean
    Return True
End Sub

Sub Service_Destroy

End Sub
 

togax

Member
Licensed User
Do you get an out of memory error eventually?
Actually no. I've decided to let it run for a while with much shorter time-outs. The free memory value got close to about 1.9 MB and it suddenly jumped back to around 9 MB then proceeded to trickle down again. So I presume the system is performing some memory cleanup when resources are getting low. Would that be correct? Do you see anything silly or bad in my sample code I posted earlier? Much appreciated for your assistance.
 
Upvote 0

togax

Member
Licensed User
Thanks Erel that's all I need to know. I just wanted to make sure my sample code is not causing a memory leak, in particular the way I am re-entering the MainLoop routine. My real app is a lot more complex but it follows a similar means to execute repeated tasks. It's working fine (so far).
 
Upvote 0
Top