Android Question CPU over 98%

Dey

Active Member
Licensed User
Longtime User
Hi everyone,
I have a B4XPage app that stays active all day
installed on various PCs with the LDPLayer emulator.
On some of them, I've noticed severe slowdowns. I exit the app and restart it, and it resolves the issue.
At this point, I created a timer that queries the RAM and CPU status every 5 seconds.

After several minutes, on some PCs, I see the CPU go above 98%
the RAM is stable.
Example:
Just started:
[MONITOR] RAM Used: 11MB | CPU Activity: 26.82%
[MONITOR] RAM Used: 12MB | CPU Activity: 14.85%
[MONITOR] RAM Used: 9MB | CPU Activity: 20.82%
After hours:
[MONITOR] RAM Used: 17MB | CPU Activity: 52.59%
[MONITOR] RAM Used: 14MB | CPU Activity: 82.41%
[MONITOR] RAM Used: 22MB | CPU Activity: 98.9%
[MONITOR] RAM Used: 39MB | CPU Activity: 98.12%
[MONITOR] RAM Used: 30MB | CPU Activity: 98.62%
[MONITOR] RAM Used: 12MB | CPU Activity: 85.57%
[MONITOR] RAM Used: 15MB | CPU Activity: 90.01%
[MONITOR] RAM Used: 18MB | CPU Activity: 98.44%
[MONITOR] RAM Used: 17MB | CPU Activity: 99.05%

Sub to read RAM and CPU

B4X:
Private Sub cpuTimer_Tick
    Dim joRuntime As JavaObject
    joRuntime = joRuntime.InitializeStatic("java.lang.Runtime").RunMethod("getRuntime", Null)
    Dim totalMemory As Long = joRuntime.RunMethod("totalMemory", Null)
    Dim freeMemory As Long = joRuntime.RunMethod("freeMemory", Null)
    Dim usedMemory As Int = (totalMemory - freeMemory) / 1024 / 1024
   '  Correct CPU Monitoring
    Dim joDebug As JavaObject
    Dim joSystemClock As JavaObject
    
' Actual CPU time used by the process's threads (in nanoseconds)
    Dim currentCpuTime As Long = joDebug.InitializeStatic("android.os.Process").RunMethod("getElapsedCpuTime", Null) * 1000000

' Real time elapsed (system clock)
    Dim currentTime As Long = joSystemClock.InitializeStatic("android.os.SystemClock").RunMethod("elapsedRealtimeNanos", Null)
    Dim deltaPerc As Double = 0
    If lastSampleTime > 0 Then
        Dim cpuDiff As Long = currentCpuTime - lastCpuTime
        Dim timeDiff As Long = currentTime - lastSampleTime
                
        ' Real percentage calculation: (CPU_Time_Used / Total_Time_Elapsed) * 100
        If timeDiff > 0 Then
            deltaPerc = (cpuDiff / timeDiff) * 100
        End If
    End If
    lastSampleTime = currentTime
    lastCpuTime = currentCpuTime
    ' Output
Log($"[MONITOR] RAM Used: ${usedMemory}MB | CPU Activity: ${NumberFormat(deltaPerc, 1, 2)}%"$)
Utility.SaveLog($"Starter.cpuTimer_Tick
[MONITOR] RAM Used: ${usedMemory}MB | CPU Activity: ${NumberFormat(deltaPerc, 1, 2)}%"$)

End Sub


I don't encounter this problem with PC debugging and emulation.

SQLite database only on PC with B4J App and RDC Connector

Any suggestions on how to debug?
Thanks
 

emexes

Expert
Licensed User
Longtime User
I'd start first by adding some profiling, to measure the frequency and execution time of various Subs, and see what the stats are when the program is running normally and when the program slows up. Don't have to measure every Sub - can start with the outer higher-level Subs, and then dig down into whichever of those seems to include the problem.

This looks like it might even do most of the instrumenting already for you:


so perhaps give that a burl first before/rather than we roll our own duplicate of it.
 
Upvote 0
Cookies are required to use this site. You must accept them to continue using the site. Learn more…