Android Question Out of memory error BBCodeView

RB Smissaert

Well-Known Member
Licensed User
Longtime User
Showing my application error log (saved to a list and concatenated to a string ready to be processed by a BBCodeView) in a dialog.
This is a large list (just for testing, all errors have been fixed) with some 80 items.
All the error items in the BBCodeView have an item number and these are clickable, so I can either copy the error text to the clipboard
or clear the particular error item from the list.
After showing this text in the BBCodeView and clearing the first item then after doing this some 3 to 6 times (variable) I get an out of memory
error on putting the concatenated list items in the BBCodeView.
There are either of these 2 similar errors:

B4X:
bctextengine$_bcglyphInitialize (java line: 331)
java.lang.OutOfMemoryError: Failed to allocate a 40 byte allocation with 2564328 free bytes and 2504KB until OOM, target footprint 268435456, growth limit 268435456; giving up on allocation because <1% of heap free after GC.
    at b4a.exampleljjll.bctextengine$_bcglyph.Initialize(bctextengine.java:331)
    at b4a.exampleljjll.bctextengine._createglyphfromdefaultcolor(bctextengine.java:835)
    at b4a.exampleljjll.bctextengine._createglyph(bctextengine.java:721)
    at b4a.exampleljjll.bctextengine._createsinglesection(bctextengine.java:976)
    at b4a.exampleljjll.bctextengine._createunbreakable(bctextengine.java:1073)
    at b4a.exampleljjll.bctextengine._handletextrun(bctextengine.java:1790)
    at b4a.exampleljjll.bctextengine._prepare(bctextengine.java:2474)
    at b4a.exampleljjll.bctextengine._prepareforlazydrawing(bctextengine.java:2500)
    at b4a.exampleljjll.bbcodeview._redraw(bbcodeview.java:522)
    at b4a.exampleljjll.bbcodeview._parseanddraw(bbcodeview.java:497)
    at b4a.exampleljjll.bbcodeview._settext(bbcodeview.java:549)
    at b4a.exampleljjll.b4xdialog2$ResumableSub_ShowWithBCTE.resume(b4xdialog2.java:3751)
    at b4a.exampleljjll.b4xdialog2._showwithbcte(b4xdialog2.java:2861)
    at b4a.exampleljjll.b4xmainpage$ResumableSub_ShowErrorLog.resume(b4xmainpage.java:24831)
    at b4a.exampleljjll.b4xmainpage._showerrorlog(b4xmainpage.java:24756)
    at b4a.exampleljjll.b4xmainpage$ResumableSub_ShowAbout.resume(b4xmainpage.java:23897)
    at anywheresoftware.b4a.BA.checkAndRunWaitForEvent(BA.java:275)
    at anywheresoftware.b4a.BA.raiseEvent2(BA.java:215)
    at anywheresoftware.b4a.BA.raiseEvent(BA.java:201)
    at anywheresoftware.b4a.keywords.Common$15.run(Common.java:1804)
    at android.os.Handler.handleCallback(Handler.java:942)
    at android.os.Handler.dispatchMessage(Handler.java:99)
    at android.os.Looper.loopOnce(Looper.java:226)
    at android.os.Looper.loop(Looper.java:313)
    at android.app.ActivityThread.main(ActivityThread.java:8747)
    at java.lang.reflect.Method.invoke(Native Method)
    at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:571)
    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1067)

B4X:
bctextengine_createglyphfromdefaultcolor (java line: 853)
java.lang.OutOfMemoryError: Failed to allocate a 2072 byte allocation with 2090368 free bytes and 2041KB until OOM, target footprint 268435456, growth limit 268435456; giving up on allocation because <1% of heap free after GC.
    at b4a.exampleljjll.bctextengine._createglyphfromdefaultcolor(bctextengine.java:853)
    at b4a.exampleljjll.bctextengine._createglyph(bctextengine.java:721)
    at b4a.exampleljjll.bctextengine._createsinglesection(bctextengine.java:976)
    at b4a.exampleljjll.bctextengine._createunbreakable(bctextengine.java:1073)
    at b4a.exampleljjll.bctextengine._handletextrun(bctextengine.java:1790)
    at b4a.exampleljjll.bctextengine._prepare(bctextengine.java:2474)
    at b4a.exampleljjll.bctextengine._prepareforlazydrawing(bctextengine.java:2500)
    at b4a.exampleljjll.bbcodeview._redraw(bbcodeview.java:522)
    at b4a.exampleljjll.bbcodeview._parseanddraw(bbcodeview.java:497)
    at b4a.exampleljjll.bbcodeview._settext(bbcodeview.java:549)
    at b4a.exampleljjll.b4xdialog2$ResumableSub_ShowWithBCTE.resume(b4xdialog2.java:3751)
    at b4a.exampleljjll.b4xdialog2._showwithbcte(b4xdialog2.java:2861)
    at b4a.exampleljjll.b4xmainpage$ResumableSub_ShowErrorLog.resume(b4xmainpage.java:24830)
    at b4a.exampleljjll.b4xmainpage._showerrorlog(b4xmainpage.java:24758)
    at b4a.exampleljjll.b4xmainpage$ResumableSub_ProcessError.resume(b4xmainpage.java:19759)
    at anywheresoftware.b4a.BA.checkAndRunWaitForEvent(BA.java:275)
    at anywheresoftware.b4a.BA.raiseEvent2(BA.java:215)
    at anywheresoftware.b4a.BA.raiseEvent(BA.java:201)
    at anywheresoftware.b4a.keywords.Common$15.run(Common.java:1804)
    at android.os.Handler.handleCallback(Handler.java:942)
    at android.os.Handler.dispatchMessage(Handler.java:99)
    at android.os.Looper.loopOnce(Looper.java:226)
    at android.os.Looper.loop(Looper.java:313)
    at android.app.ActivityThread.main(ActivityThread.java:8747)
    at java.lang.reflect.Method.invoke(Native Method)
    at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:571)
    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1067)

This is the code that processes the concatenated list items:

B4X:
    If tDP.bPrompt Then
        
        If TextEngine.IsInitialized = False Then
            TextEngine.Initialize(Parent)
        End If
        bbcvPrompt.TextEngine = TextEngine
        
        bbcvPrompt.LazyLoading = True 'but already set so in the designer
        
        bbcvPrompt.mBase.Height = tDP.iScrollViewHeight 'this is the visible height of the old plain prompt view
        bbcvPrompt.mBase.Top = tDP.iPromptTop
        bbcvPrompt.mBase.Width = tDP.iScrollViewWidth 'this is same as tDP.iPromptWidth
        If tDP.bPromptScroll Then
            bbcvPrompt.sv.Height = tDP.iScrollViewHeight
        End If
        
        Dim rectPadding As B4XRect
        rectPadding.Initialize(arrPromptPadding(0), arrPromptPadding(1), arrPromptPadding(2), arrPromptPadding(3))
        bbcvPrompt.Padding = rectPadding
        
        '------------------------------------------------'
        'This is the way to set the BBCodeView background'
        '------------------------------------------------'
        bbcvPrompt.mBase.Color = iPromptColour
        bbcvPrompt.sv.Color = iPromptColour
        bbcvPrompt.sv.ScrollViewInnerPanel.Color = iPromptColour
        
        Log("ShowWithBCTE, before adding text")
        
        If tDP.bcsPrompt Then
            bbcvPrompt.Text = csPrompt 'this won't happen
        Else
            bbcvPrompt.Text = strPrompt '<<<<<<< out of memory error happening here
        End If
        
        Log("ShowWithBCTE, after adding text")
        
        'get rid of the click sound as there will be nil clickable
        '---------------------------------------------------------
        Dim jo As JavaObject = bbcvPrompt.mBase
        jo.RunMethod("setSoundEffectsEnabled", Array(False))
    End If 'If tDP.bPrompt

And this is the code that concatenates the list items:

B4X:
Sub ConcatenateListItems(lst As List, strSeparator As String, bAddIndex As Boolean, bClickableNumbers As Boolean) As String
    
    Dim i As Int
    Dim strResult As String
    Dim strURLStart As String
    Dim strURLEnd As String
    
    If bClickableNumbers Then
        strURLStart = "[url]"
        strURLEnd = "[/url]"
    End If
    
    If bAddIndex Then
        For i = 0 To lst.Size - 1
            If i = 0 Then
                strResult = strURLStart & (i + 1) & ")  " & strURLEnd & lst.Get(i)
            Else
                strResult = strResult & strSeparator & strURLStart & (i + 1) & ")  " & strURLEnd & lst.Get(i)
            End If
        Next
    Else
        For i = 0 To lst.Size - 1
            If i = 0 Then
                strResult = lst.Get(i)
            Else
                strResult = strResult & strSeparator & lst.Get(i)
            End If
        Next
    End If
    
    Return strResult
    
End Sub

The strange thing is that although the string to be passed to the BBCodeView is getting smaller (by clearing the first error item) the error only appears after some 3 to 6
runs. I had a good search for this problem in the forum, but couldn't find anything helpful.
Any ideas what might be the problem here?

RBS
 

RB Smissaert

Well-Known Member
Licensed User
Longtime User
Hard to say without a small project that reproduces it.
Thanks, will do.
I can see that something is consuming more and more memory by looking at the memory state of the phone (Samsung S23) with this code:

B4X:
Sub ShowMemory(bLogOnly As Boolean) As ResumableSub
    
    Dim r As Reflector
    Dim iFreeMemory As Int
    Dim iMaxMemory As Int
    Dim iTotalMemory As Int
    Dim iUsedMemory   As Int
    Dim iTotalFreeMemory As Int

    Dim strPrompt As String

    r.Target = r.RunStaticMethod("java.lang.Runtime", "getRuntime", Null, Null)
    
    iFreeMemory = r.RunMethod("freeMemory") / (1024 * 1024)
    iMaxMemory = r.RunMethod("maxMemory") / (1024 * 1024)
    iTotalMemory = r.RunMethod("totalMemory") / (1024 * 1024)
    
    iUsedMemory   = iTotalMemory - iFreeMemory
    iTotalFreeMemory   = iMaxMemory - iUsedMemory
    
    If bLogOnly Then
        Return iTotalFreeMemory
    End If
    
    strPrompt =         "Max Memory  = " & iMaxMemory & " Mb" &  _
                 CRLF & "Total Free        = " & iTotalFreeMemory & " Mb" & _
                 CRLF & "Total Memory = " & iTotalMemory & " Mb" & _      
                 CRLF & "Used Memory = " & iUsedMemory & " Mb" & _
                 CRLF & "Free Memory  = " & iFreeMemory& " Mb"
    
    Dim rs As ResumableSub = Dialog.Show(mvPageRoot, Array As Object("OK", "To clipboard"), _
                             "Show Memory", "", strPrompt, _
                             -1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, Null, False, 1)
    Wait For (rs) Complete (strResult As String)

    If strResult = "To clipboard" Then
        cClipBoard.setText(strPrompt)
    End If
    
    Return -1
    
End Sub

When I log this at the Sub that adds the string to the BBCodeView and run this repeatedly then I get this log, demonstrating there
is a memory problem somewhere:

B4X:
MeasureTextHeightBBCodeView, iContentHeight: 36885
Total free memory: 206 Mb
ShowWithBCTE, before adding text
ShowWithBCTE, after adding text

MeasureTextHeightBBCodeView, iContentHeight: 36581
Total free memory: 167 Mb
ShowWithBCTE, before adding text
ShowWithBCTE, after adding text

MeasureTextHeightBBCodeView, iContentHeight: 36098
Total free memory: 111 Mb
ShowWithBCTE, before adding text
ShowWithBCTE, after adding text

MeasureTextHeightBBCodeView, iContentHeight: 35615
Total free memory: 81 Mb
ShowWithBCTE, before adding text
ShowWithBCTE, after adding text

MeasureTextHeightBBCodeView, iContentHeight: 35073
Total free memory: 49 Mb
ShowWithBCTE, before adding text
ShowWithBCTE, after adding text

MeasureTextHeightBBCodeView, iContentHeight: 34652
Total free memory: 19 Mb
ShowWithBCTE, before adding text
ShowWithBCTE, after adding text

MeasureTextHeightBBCodeView, iContentHeight: 34231
Total free memory: 28 Mb
ShowWithBCTE, before adding text
ShowWithBCTE, after adding text

MeasureTextHeightBBCodeView, iContentHeight: 33927
Total free memory: 3 Mb
ShowWithBCTE, before adding text
>>> crashes here <<<

Will have a good look to find out where this memory problem might occur, but if I can't find it will
post a demo project.

RBS
 
Upvote 0

RB Smissaert

Well-Known Member
Licensed User
Longtime User
Thanks, will do.
I can see that something is consuming more and more memory by looking at the memory state of the phone (Samsung S23) with this code:

B4X:
Sub ShowMemory(bLogOnly As Boolean) As ResumableSub
   
    Dim r As Reflector
    Dim iFreeMemory As Int
    Dim iMaxMemory As Int
    Dim iTotalMemory As Int
    Dim iUsedMemory   As Int
    Dim iTotalFreeMemory As Int

    Dim strPrompt As String

    r.Target = r.RunStaticMethod("java.lang.Runtime", "getRuntime", Null, Null)
   
    iFreeMemory = r.RunMethod("freeMemory") / (1024 * 1024)
    iMaxMemory = r.RunMethod("maxMemory") / (1024 * 1024)
    iTotalMemory = r.RunMethod("totalMemory") / (1024 * 1024)
   
    iUsedMemory   = iTotalMemory - iFreeMemory
    iTotalFreeMemory   = iMaxMemory - iUsedMemory
   
    If bLogOnly Then
        Return iTotalFreeMemory
    End If
   
    strPrompt =         "Max Memory  = " & iMaxMemory & " Mb" &  _
                 CRLF & "Total Free        = " & iTotalFreeMemory & " Mb" & _
                 CRLF & "Total Memory = " & iTotalMemory & " Mb" & _     
                 CRLF & "Used Memory = " & iUsedMemory & " Mb" & _
                 CRLF & "Free Memory  = " & iFreeMemory& " Mb"
   
    Dim rs As ResumableSub = Dialog.Show(mvPageRoot, Array As Object("OK", "To clipboard"), _
                             "Show Memory", "", strPrompt, _
                             -1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, Null, False, 1)
    Wait For (rs) Complete (strResult As String)

    If strResult = "To clipboard" Then
        cClipBoard.setText(strPrompt)
    End If
   
    Return -1
   
End Sub

When I log this at the Sub that adds the string to the BBCodeView and run this repeatedly then I get this log, demonstrating there
is a memory problem somewhere:

B4X:
MeasureTextHeightBBCodeView, iContentHeight: 36885
Total free memory: 206 Mb
ShowWithBCTE, before adding text
ShowWithBCTE, after adding text

MeasureTextHeightBBCodeView, iContentHeight: 36581
Total free memory: 167 Mb
ShowWithBCTE, before adding text
ShowWithBCTE, after adding text

MeasureTextHeightBBCodeView, iContentHeight: 36098
Total free memory: 111 Mb
ShowWithBCTE, before adding text
ShowWithBCTE, after adding text

MeasureTextHeightBBCodeView, iContentHeight: 35615
Total free memory: 81 Mb
ShowWithBCTE, before adding text
ShowWithBCTE, after adding text

MeasureTextHeightBBCodeView, iContentHeight: 35073
Total free memory: 49 Mb
ShowWithBCTE, before adding text
ShowWithBCTE, after adding text

MeasureTextHeightBBCodeView, iContentHeight: 34652
Total free memory: 19 Mb
ShowWithBCTE, before adding text
ShowWithBCTE, after adding text

MeasureTextHeightBBCodeView, iContentHeight: 34231
Total free memory: 28 Mb
ShowWithBCTE, before adding text
ShowWithBCTE, after adding text

MeasureTextHeightBBCodeView, iContentHeight: 33927
Total free memory: 3 Mb
ShowWithBCTE, before adding text
>>> crashes here <<<

Will have a good look to find out where this memory problem might occur, but if I can't find it will
post a demo project.

RBS
Solved this now.
The BBCodeView Layout wasn't cleared when the clickable link was clicked as no button was clicked in the dialog.
Adding some code to the Sub that processes the clickable link click solved it all:

B4X:
Sub bbcvPrompt_LinkClicked(strURL As String)

    bbcvPrompt.Initialize(mParent, "bbcvPrompt")
    pnlDialog1.RemoveViewFromParent
    
    'for either copying the error text or clearing the error from the list
    cMP.ProcessError(strURL)
    
End Sub

RBS
 
Upvote 0
Top