B4J Question Ever expanding memory usage

cyiwin

Active Member
Licensed User
I'm a little confused how my heap memory usage is on a constant rise. I first increased my max heap memory but that isn't going to be a solution since there seems to be no stopping the usage. I attached a few images from VisualVM. HeapMemoryUsage3.pngHeapDump1.png

I wonder if the cause is all the JSON strings I'm downloading. My app constantly uses HttpJobs to get JSON strings and moves them to variable arrays like this:

B4X:
Sub JobDone(job As HttpJob)
If job.Success = True Then
  Select job.JobName   
    Case "job_GetMarkets"
      GetMarkets = job.GetString
      Get_Markets
  End Select
  job.Release
End If
End Sub

B4X:
Sub Get_Markets
Log(GetMarkets)
'Log("GetMarkets Length = " & sf.Len(GetMarkets) & " characters")

Dim Map1 As Map                                                                    'Success
JSON.Initialize(GetMarkets)
Map1 = JSON.NextObject
Log("Map1 = " & Map1)

Dim M As Map
Market_List.Clear

Market_List = Map1.Get("return")

For i = 0 To Market_List.Size-1
    M = Market_List.Get(i)
    GM_Last_Trade(i) = M.Get("last_trade")
    GM_Market_ID(i) = M.Get("marketid")
    GM_Primary_Coin(i) = M.Get("primary_currency_code")
    GM_Secondary_Coin(i) = M.Get("secondary_currency_code")
    GM_Low_Trade(i) = M.Get("low_trade")
    GM_High_Trade(i) = M.Get("high_trade")
Next

End Sub

I keep using the same string "GetMarkets" to fill the same Arrays from that string but somehow the heap memory keeps increasing. Is there a way to properly terminate a JSON string? Maybe the Arrays are holding on to past information somehow? Maybe the "job.GetString" never refreshes? I'm hoping someone sees a flaw in my method here. It's odd because the same code works from B4A on my phone for days without any issues.
 
Last edited:

cyiwin

Active Member
Licensed User
It uses private keys to access the data. Today I'll try to shrink the program and recreate the problem. If I have success I can email you the results.
 
Upvote 0

cyiwin

Active Member
Licensed User
I've spent a couple of days on this. It's a little odd but the problem seems to be filling/refilling tables. If I run my program with my two tables, it will run out of heap space in about an hour and 13 minutes. If I comment out the tables the program runs over night without problems. I've tried to reproduce the problem by making a program that just fills those tables and does nothing else but it doesn't run out of heap space. It does have a funny looking garbage collection where it will wait till almost the max heap before doing a big collection.

2_Tables.png


The only thing I can think of at this point is maybe this style of garbage collection isn't fast enough to keep up with my program and filling the tables.

Here is an example of how I'm filling a table.
B4X:
Sub View_Top_Changers_Table

Log("Sub: View_Top_Changers_Table")
tbl_TC.Items.Clear
Dim C,V As String
Dim MarketID As Int
Dim Top_Changers_Tally As Int
Top_Changers_Tally = Map_Top_Changers.Size - 1
Log("Map_Top_Changers.Size - 1 = " & Top_Changers_Tally)
tbl_TC.SetColumns(Array As String("Rank", "Coin Pair", "Change", "Volume(BTC)"))

For i = 0 To Top_Changers_Tally
    Dim lbl1, lbl2, lbl3, lbl4 As Label
    lbl1.Initialize("") : lbl2.Initialize("") : lbl3.Initialize("") : lbl4.Initialize("")
    MarketID = Map_Top_Changers.GetKeyAt(i)
    C = NumberFormat2(Map_Top_Changers.GetValueAt(i),1,2,2,False)
    V = NumberFormat2(Map_Trade_Volumes.Get(MarketID),1,4,2,False)
   
    lbl1.Text = i + 1 : lbl2.Text = Get_CoinPair(MarketID) : lbl3.Text = C & "%" : lbl4.Text = V
    tbl_TC.Items.Add(Array As Object(WrapLabel(lbl1,"CENTER"),WrapLabel(lbl2,"CENTER"),WrapLabel(lbl3,"CENTER"),WrapLabel(lbl4,"CENTER")))
    Log("Coin " & (i+1) & " : MarketID " & MarketID & " : " & Get_CoinPair(MarketID) & " : Change = " & C & "% : Volume = " & V)
Next
End Sub

Sub WrapLabel(lbl As Label, Alignment As String) As Pane
   Dim pn1 As AnchorPane
   pn1.Initialize("")
   pn1.AddNode(lbl, 0, 0, -1, -1)
   pn1.FillHorizontally(lbl, 0, 0)
   Dim jo1 = lbl As JavaObject
   jo1.RunMethod("setAlignment", Array As Object(Alignment))
   Return pn1
End Sub

Erel, If you still want my program I'll email it to you but it's 2700 lines of code :eek:
 
Upvote 0

cyiwin

Active Member
Licensed User
Thanks for your help so far Erel.

I've done some more looking into this. My results have been somewhat intermittent and I'm new to VisualVM, but it looks like almost all the memory is being used up by int[]. It looks like it may be coming from the charts I'm using. In my program, I have a few bar graphs that update about every minute or so.

Using the charts framework tutorial and putting in a timer that updates the bar chart every 500 msec, it lasts about 3 minutes before it gives an out of memory error. Almost all the memory is used by int[].

Here's a zip of the charts program with the timer. Maybe the charts weren't intended to be updated on a regular basis? Maybe I'm supposed to clear them somehow?
 

Attachments

  • Charts_Timer.zip
    5.2 KB · Views: 128
Upvote 0

cyiwin

Active Member
Licensed User
There is no memory leak here. You are just adding more and more canvases to the layout.

Add this line before you add the new canvas:
B4X:
paneChart.RemoveAllNodes

That fixed it! Thanks Erel! The memory leak on my main program is still there but very small now. Not sure why I'm the only one having these issues. I'll have to try it out on a different computer later.
 
Upvote 0
Top