B4J Question SONGenerator Java Heap Space Out of Memory

rwblinn

Well-Known Member
Licensed User
Hi,

using the JSONGenerator in a Timer_Tick causes after running several hours a Java Heap Space Out of Memory Error.

Any hints how this can be resolved appreciated.

Code Snippets
B4X:
Private Sub ScanTimer_Tick
...
    loadchart_event(Null)
...
End Sub

B4X:
' Create google line chart on the webpage
Sub loadchart_event(Params As Map)
    Dim cols As List
    cols.Initialize
    Dim rows As List
    rows.Initialize
    Dim options As Map
    options.Initialize
    '
    CreateLineChartData(cols, rows)
    CreateLineChartOptions(options)
    '   
    Dim jg As JSONGenerator
    jg.Initialize(CreateMap("data": CreateMap ("cols": cols, "rows": rows), "options": options))
    ws.RunFunction("drawCharts", Array As String(jg.ToString))
End Sub
B4J Error Log
B4X:
java.lang.RuntimeException: java.lang.OutOfMemoryError: Java heap space
    at anywheresoftware.b4a.BA.raiseEvent2(BA.java:114)
    at anywheresoftware.b4a.BA$3.run(BA.java:178)
    at anywheresoftware.b4a.keywords.SimpleMessageLoop.runMessageLoop(SimpleMessageLoop.java:30)
    at anywheresoftware.b4a.StandardBA.startMessageLoop(StandardBA.java:26)
    at anywheresoftware.b4j.object.WebSocketModule$Adapter$ThreadHandler.run(WebSocketModule.java:195)
    at java.util.concurrent.Executors$RunnableAdapter.call(Executors.java:511)
    at java.util.concurrent.FutureTask.run(FutureTask.java:266)
    at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1142)
    at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:617)
    at java.lang.Thread.run(Thread.java:744)
Caused by: java.lang.OutOfMemoryError: Java heap space
    at org.json.JSONObject.<init>(JSONObject.java:112)
    at anywheresoftware.b4j.objects.collections.JSONParser$JSONGenerator.convertMapToJO(JSONParser.java:168)
    at anywheresoftware.b4j.objects.collections.JSONParser$JSONGenerator.convertListToJA(JSONParser.java:201)
    at anywheresoftware.b4j.objects.collections.JSONParser$JSONGenerator.convertMapToJO(JSONParser.java:179)
    at anywheresoftware.b4j.objects.collections.JSONParser$JSONGenerator.convertListToJA(JSONParser.java:201)
    at anywheresoftware.b4j.objects.collections.JSONParser$JSONGenerator.convertMapToJO(JSONParser.java:179)
    at anywheresoftware.b4j.objects.collections.JSONParser$JSONGenerator.convertMapToJO(JSONParser.java:173)
    at anywheresoftware.b4j.objects.collections.JSONParser$JSONGenerator.Initialize(JSONParser.java:132)
    at rwblinn.webtemp.webtemp._loadchart_event(webtemp.java:338)
    at rwblinn.webtemp.webtemp._scantimer_tick(webtemp.java:446)
    at sun.reflect.GeneratedMethodAccessor1.invoke(Unknown Source)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
    at java.lang.reflect.Method.invoke(Method.java:483)
    at anywheresoftware.b4a.BA.raiseEvent2(BA.java:93)
    ... 9 more
 

rwblinn

Well-Known Member
Licensed User
There are three projects there. Which one is the problematic one? How can I reproduce it?
Hi Erel,

it is with all 3. To reproduce it requires TinkerForge components. Let me see if I can make a version with simulates the TinkerForge components and then come back to you.
 

rwblinn

Well-Known Member
Licensed User
That is correct -- might be (but not sure) an issue with the JSONGenerator not releasing memory after being defined and initialized over and over in the sub:
B4X:
Dim jg As JSONGenerator
jg.Initialize(CreateMap("data": CreateMap ("cols": cols, "rows": rows), "options": options))
 
Top