Android Question Application run slow after.....

micro

Well-Known Member
Licensed User
Longtime User
Hi
I developed a program that runs on an industrial device with Android, it works fine.
If I enable these lines of code after a few hours the memory used increases from 30 to about 40 Mega and the application runs very slowly as if it was locked.
Else the server is not running, then the sub active are:
Sub TimerEth_Tick (raised every two seconds)
Sub SendObject

B4X:
#Region ETHERNET
Sub TimerEth_Tick
    TimerEth.Enabled = False
    Dim val As Int
    Dim mapeth As Map
    mapeth.Initialize
    mapeth.Put("ip", myip)
   mapeth.Put("format1", format1str)
   mapeth.Put("format2", format2str)
   mapeth.Put("format", format)
   mapeth.Put("locked", lock)
   mapeth.Put("operator", op)
   mapeth.Put("lastmem", mem)
   mapeth.Put("oramem", oramem)
   mapeth.Put("media", media)
   SendObject(mapeth)
   countsendeth = countsendeth + 1
   If countsendeth > 4 Then
        countsendeth = 0
        format1lb.Text = ""
        format2lb.Text = ""
        operatorlb.Text = ""
        operator = ""
        format = 0
        mina = 0
        maxa = 0
        img1.Visible = False
        img2.Visible = False
        img3.Visible = False
        bar1.Visible = False
        bar2.Visible = False
        bar3.Visible = False
        conn = False
        arrowlb.Visible = False
   End If
   TimerEth.Enabled = True
End Sub
Sub SendObject (Obj As Object)
    Dim raf As RandomAccessFile
    raf.Initialize(File.DirInternal, "temp", False)
   raf.WriteB4XObject(Obj, 0)'raf.WriteObject(Obj, True, 0)
   Dim size As Int = raf.CurrentPosition
   Dim data(size) As Byte
   raf.CurrentPosition = 0
   Do While raf.CurrentPosition < size
     raf.ReadBytes(data, raf.CurrentPosition, size - raf.CurrentPosition, raf.CurrentPosition)
   Loop
   Dim j As HttpJob
   j.Initialize("send object", Me)
   j.PostBytes(linkserv, data)
   raf.Flush 'added but not needed
   raf.Close 'added but not needed
   j.Release 'added but not needed
   If File.Exists(File.DirInternal, "temp") = True Then File.Delete(File.DirInternal, "temp") 'added but not needed
End Sub

Sub ReadObject (In As InputStream) As Object
Try
   Dim out As OutputStream
   out.InitializeToBytesArray(0)
   File.Copy2(In, out)
   Dim raf2 As RandomAccessFile
   raf2.Initialize3(out.ToBytesArray, False)
   Dim res As Object = raf2.ReadB4XObject(0) 'raf2.ReadObject(0)
   raf2.Flush
   raf2.Close
   Return res
Catch As Exception
End Try
End Sub

Sub JobDone(j As HttpJob)
Dim ricaricaval As Boolean
   If j.Success Then
     Dim result As Map
     result.Initialize
     result = ReadObject(j.GetInputStream)
     If result.IsInitialized = False Then
         j.Release
         Return
    End If
       mapresult = result
Else
     'Log("Error: " & j.ErrorMessage)
   End If
   j.Release
End Sub
#End Region

If I disable The TimerEth, the App work well for days.
Android 4.1
 

micro

Well-Known Member
Licensed User
Longtime User
Note that you don't need to disable and reenable the timer in this sub. The timer will not tick until the code execution completes.
Ok
You are releasing the HttpJob before it completes. You should only release it in JobDone.
I added this line only just to see if he slowed and slowed anyway.
however the server is off, then JobDone is never raised.
Does it slow down if you comment the SendObject line?
waiting for my test....

thanks
 
Upvote 0

micro

Well-Known Member
Licensed User
Longtime User
Hi Erel
I commented the line SendObject and the app work fine.
Than the problem is in the Sub SendObject.
How can I solve it?
Thanks
 
Upvote 0

Erel

B4X founder
Staff member
Licensed User
Longtime User
Zhe timer will pause while the tick Sub is running?
The timer sends messages to the message queue. The message queue will only be processed after the current code execution completes.

however the server is off, then JobDone is never raised.
JobDone should always fire eventually.

You are testing the performance in Release mode, right?

What is the value of the 'size' variable?

Does it throw an out of memory error eventually?
 
Upvote 0

micro

Well-Known Member
Licensed User
Longtime User
You are testing the performance in Release mode, right?
Yes
What is the value of the 'size' variable?
I send olny a map (mapeth)
mapeth.Put("ip", myip) 'ip string
mapeth.Put("format1", format1str) ' 10 char Ascii max
mapeth.Put("format2", format2str) '10 char Ascii max
mapeth.Put("format", format) '10 char Ascii max
mapeth.Put("locked", lock) ' boolean
mapeth.Put("operator", op) ' 16 char Ascii max
mapeth.Put("lastmem", mem) '10 char Ascii max
mapeth.Put("oramem", oramem) '8 char Ascii max
mapeth.Put("media", media) '4 char Ascii max
Does it throw an out of memory error eventually?
No, only run very slow.

when sending a map with SendObject, i do not receive anything because the server is off
then JobDone is never raised (checked in debug mode).
 
Upvote 0

sorex

Expert
Licensed User
Longtime User
try adding a global trigger variable (posting in the example)

set it to true just before the post and reset it again when the post has done and add a check for it.

stuff might go crazy and queuing a lot of post requests.

B4X:
   Dim j As HttpJob
   if posting=false then
    posting=true
    j.Initialize("send object", Me)
    j.PostBytes(linkserv, data)
   end if
   raf.Flush 'added but not needed
   raf.Close 'added but not needed
'   j.Release 'added but not needed
   If File.Exists(File.DirInternal, "temp") = True Then File.Delete(File.DirInternal, "temp") 'added but not needed
End Sub

Sub JobDone(j As HttpJob)
Dim ricaricaval As Boolean
posting=false
   If j.Success Then
 
Upvote 0
Cookies are required to use this site. You must accept them to continue using the site. Learn more…