Bug? [RESOLVED] OOM websockets

Jmu5667

Well-Known Member
Licensed User
Longtime User
Hello

I have recently started getting OOM errors via websockets. I am using B4A 14.2. From IDE -

B4X:
*** Service (svc_wsh_login) Create ***
10/15/2026 15:17:40.454 - ATLAS CM - svc_wsh_login::Service_Create()
*** Service (svc_wsh_login) Create completed ***
*** Service (svc_wsh_login) Started ***
** Service (svc_wsh_login) Start **
10/15/2026 15:17:40.461 - ATLAS CM - svc_wsh_login::Service_Start::Connecting to wss://isledemo.cm.raw.aspectlink.com:443/login
10/15/2026 15:17:40.468 - ATLAS CM - WebSocketHandler::Connect to wss://isledemo.cm.raw.aspectlink.com:443/login
*** Service (svc_wsh_login) Started completed ***
java.lang.OutOfMemoryError: Failed to allocate a 16 byte allocation with 0 free bytes and 0B until OOM, target footprint 134220848, growth limit 134217728
    at android.os.ThreadLocalWorkSource.setUid(ThreadLocalWorkSource.java:68)
    at android.os.Looper.loop(Looper.java:212)
    at android.app.ActivityThread.main(ActivityThread.java:7386)
    at java.lang.reflect.Method.invoke(Native Method)
    at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:492)
    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:980)
java.lang.OutOfMemoryError: OutOfMemoryError thrown while trying to throw an exception; no stack trace available

I have attached the logcat file.

regards

John.
 

Attachments

  • device.txt
    175 KB · Views: 23

Jmu5667

Well-Known Member
Licensed User
Longtime User
Hello

I have a login service, svc_wsh_login, which use's the websocket, I will attach it. I have a websockethandeler class that all our app use, I have also attached it. If I can provide anymore specific information please ask. I am trying different methods to try to track down what is causing the issue. I have yesterday also modified the manifest to include - SetApplicationAttribute(android:largeHeap, "true")

thanks in advance

John.
 

Attachments

  • svc_wsh_login.bas
    14.3 KB · Views: 19
  • WebSocketHandler.bas
    2.6 KB · Views: 22

Jmu5667

Well-Known Member
Licensed User
Longtime User
Good Morning

To anser your question - After many disconnections

Please find attached heap trace based on a status event being sent to the server. The service gets a connection sends the data and closes the connection and terminates the service. The cm_memX files are snapshots during the process and shows the Parcel memory growth. I have attached the server the class's it uses.

Today I will try and figure out which part of the process is at fault.

Regards

John.
 

Attachments

  • cm_mem_1.txt
    3.1 KB · Views: 6
  • cm_mem_2.txt
    3.1 KB · Views: 6
  • cm_mem_3.txt
    3.1 KB · Views: 5
  • heap_trace_per_socket_session.txt
    90.8 KB · Views: 4
  • svc_status_event.bas
    7 KB · Views: 4
  • cls_status_events.bas
    3.5 KB · Views: 5
  • cls_cache.bas
    1.8 KB · Views: 6
  • cls_battery.bas
    4.4 KB · Views: 4

Jmu5667

Well-Known Member
Licensed User
Longtime User
Quick update, I just test open, connected, and close to the server without sending any data, and there are not socket oom issues. attached is the socket session showing memory management and GC.

I will nowcheck the class's to find the offending object.
 

Attachments

  • cm_mem_4.txt
    3.1 KB · Views: 3
  • heap_trace_per_socket_session2.txt
    159.8 KB · Views: 3
  • cm_mem_5.txt
    3.1 KB · Views: 3

Jmu5667

Well-Known Member
Licensed User
Longtime User
Hello

I have repeated the previous test but this time I have sent data the OOM occurs. This is send method, you can see I am sending very little data, imei is 15 bytes long.

B4X:
Sub send_event()
            
    Dim data As Map
    
    Dim data As Map
    data.Initialize
    data.Put("imei",Starter.appset.imei)
    data.Put("report",Starter.appset.imei)
    ws.SendEventToServer("Status_Report_AES", data)
    
    StopService(Me) <- This is for checking the OOM error
    Return
    
    Try
                
        Dim ser As B4XSerializator
        sEvent = clsSE.get_cached_event
        If sEvent.IsInitialized Then
            ser.ConvertObjectToBytesAsync(sEvent, "ser")
            Wait For (ser) ser_ObjectToBytes (Success As Boolean, Bytes() As Byte)
            If Success Then
                ' // 2021.06.01 - AES - encrypt data with host address
                Dim c As B4XCipher, b() As Byte
                b = c.Encrypt(Bytes,Starter.appset.sm.dns)
        
                Dim ecb64 As String = su.EncodeBase64(b)
                Dim imei As String = Starter.appset.imei
                Dim data As Map
                data.Initialize
                data.Put("imei",imei)
                data.Put("report",ecb64)
                ws.SendEventToServer("Status_Report_AES", data)                
            Else
                mod_functions.writelog("svc_status_event::ser_ObjectToBytes() FAILED")
            End If
        Else
            mod_functions.writelog("svc_status_event::send_event() finished")
            StopService(Me)
        End If
    Catch
        mod_functions.writelog("svc_status_event::send_event() error - " & LastException.Message)
        StopService(Me)
    End Try
    
End Sub

I hope this is of help.

Regards

John.
 

Attachments

  • cm_mem_11.txt
    3.1 KB · Views: 6
  • cm_mem_12.txt
    3.1 KB · Views: 5
  • cm_mem_13.txt
    3.1 KB · Views: 5
  • device.txt
    173.7 KB · Views: 5
  • heap_trace_per_socket_session3.txt
    29.3 KB · Views: 5

Jmu5667

Well-Known Member
Licensed User
Longtime User
One of our other apps Atlas SOS, send in status messages, but it keeps the svr_status_event service running (for a totaly different reason), and does not have the OOM issue. I just test it. So, I guess the solution for this will be the same. I will update the post when I have it resolved. Is there a reason behavour when the service is stopped ?
 
Cookies are required to use this site. You must accept them to continue using the site. Learn more…