Java heap space when running asynchronous script

Hi,

I’m getting the following error when running a script from the client.

The avaiable memory for the client is set to the maximum value, 4096M, in the Project Properties. On the other hand my equipment has a 24 GB RAM which is quite far from reaching the 100% usage when I launch the script.

Traceback (most recent call last):
File “event:internalFrameActivated”, line 33, in longProcess
File “module:CHE”, line 10, in pred
java.lang.OutOfMemoryError: Java heap space
java.lang.OutOfMemoryError: java.lang.OutOfMemoryError: Java heap space

at org.python.core.Py.JavaError(Py.java:552)
at org.python.core.Py.JavaError(Py.java:543)
at org.python.core.PyReflectedFunction.__call__(PyReflectedFunction.java:190)
at com.inductiveautomation.ignition.common.script.ScriptManager$ReflectedInstanceFunction.__call__(ScriptManager.java:521)
at org.python.core.PyObject.__call__(PyObject.java:480)
at org.python.core.PyObject.__call__(PyObject.java:484)
at org.python.pycode._pyx10.pred$1(<module:CHE>:23)
at org.python.pycode._pyx10.call_function(<module:CHE>)
at org.python.core.PyTableCode.call(PyTableCode.java:171)
at org.python.core.PyBaseCode.call(PyBaseCode.java:139)
at org.python.core.PyFunction.__call__(PyFunction.java:413)
at org.python.pycode._pyx9.longProcess$1(<event:internalFrameActivated>:57)
at org.python.pycode._pyx9.call_function(<event:internalFrameActivated>)
at org.python.core.PyTableCode.call(PyTableCode.java:171)
at org.python.core.PyBaseCode.call(PyBaseCode.java:308)
at org.python.core.PyFunction.function___call__(PyFunction.java:471)
at org.python.core.PyFunction.__call__(PyFunction.java:466)
at org.python.core.PyFunction.__call__(PyFunction.java:456)
at org.python.core.PyFunction.__call__(PyFunction.java:451)
at com.inductiveautomation.ignition.common.script.ScriptManager.runFunction(ScriptManager.java:815)
at com.inductiveautomation.ignition.client.script.DesignerSystemUtilities.lambda$_invokeAsyncImpl$1(DesignerSystemUtilities.java:126)
at java.base/java.lang.Thread.run(Unknown Source)

Caused by: java.lang.OutOfMemoryError: Java heap space

Ignition v8.0.12 (b2020042115)
Java: Azul Systems, Inc. 11.0.6

What’s the script doing that uses so much memory? It sounds like it needs some tweaking

Share your code. We can’t tell what might be happening without it.

Hi,

This is the function:

> def pred(estacion):
> 	
> 	#la estación de Logroño es A280L67QRIO2
> 	
> 	api_key="eyJ0eXAiOiJKV1QiLCJhbGciOiJIUzI1NiJ9.eyJ0eXAiOiJKV1QiLCJhbGciOiJIUzI1NiIsImlhdCI6MTU2MzQzODkyNTg4LCJkYXRhIjp7ImlkIjoxLCJuYW1lIjoiam9zZS5ncmFuZXJvIn19.ayJb-jGbEKg9ifEMy1fflJbI9rnnTv6ouHPLRHO3LwE"
> 	path="http://www.saihebro.com/saihebro/api.php?prevision=prevision_completa&apikey="+api_key
> 
> 
> 	source=system.net.httpGet(path)
> 	obj = system.util.jsonDecode(source)
> 	
> 	data=[]
> 	
> 	for lista in obj['datos']:
> 		if lista['MS_TAG']==estacion:
> 			t_stamp=system.date.parse(lista['MS_FECHA_HORA']['date'])
> 			if t_stamp>system.date.now():
> 				data.append([t_stamp,lista['MS_VALOR']])
> 			
> 	headers=["t_stamp","value"]
> 	flow_forecast=system.dataset.toDataSet(headers,data)
> 	
> 	return flow_forecast

And this is the script from which we invoke it:

import CHE as m

try:
	system.gui.getParentWindow(event).getComponentForPath('Root Container').vis_timer=1

	def longProcess(rootContainer = event.source.parent.parent):
		import system
		
		system.gui.getParentWindow(event).getComponentForPath('Root Container').Data=m.pred('A280L67QRIO2')
			
		system.gui.getParentWindow(event).getComponentForPath('Root Container').vis_timer=0
					
	system.util.invokeAsynchronous(longProcess)
		
except:
	print("Error de comunicación con CHE")
	pass

Remove that API key ASAP, you shouldn’t be sharing that with anyone everyone.
Also, what line is this: File “module:CHE”, line 10, in pred in the code you supplied?

Hi Jose,

Please edit your comment so the code blocks have the proper formatting marks (three reverse single quotes on a line by themselves, above and below), so it looks like this in the forum’s edit panel:
```
pasted code lines
```

The number of lines of code posted don’t match the line numbers in your original post’s error listing. Please clearly identify which line of code is failing (in the “pred” function).

That said, I suspect it is in jsonDecode, which can be very resource-intensive when the data from the service is malformed.

I already modified it, the key is not valid :smirk:

CHE is a library I created where the function “pred” is defined.

Hi Phil,

I’ve already edited the text, the two lines mentioned in the warning message are the following:

Line 33 in longProcess:

event.source.parent.parent.Data=m.pred('A280L67QRIO2')

Line 10 in pred:

	obj = system.util.jsonDecode(source)

Thank you.

I think we’ll need to see the payload being passed to jsonDecode.

Hi Kevin,

Please find enclosed the file.

Payload.zip (217.0 KB)

Yeah, I think @pturmel guessed somewhat correctly on this… you’re trying to parse 10MB of JSON and it’s running out of memory while doing so.

I’m not really sure what the workaround here is. You’d have to parse the JSON in a streaming manner somehow…

Ah, also, the payload you attached is not valid JSON. I don’t know if you truncated it intentionally or what, but it terminates early.

Thank you Kevin,

I truncated it by mistake.

I can’t figure out how to solve it.

Can you upload the non-truncated version? Might give us something to play around with and test.

1 Like

Hi Kevin,

Here you have.Payload.zip (461.9 KB)

Okay… now you’ve uploaded a word document for some reason?

Yes indeed, Kevin, I guess that because of the size, I couldn’t paste it in a txt file.

For anyone playing along I’ve attached a plain text file containing all the JSON (formatted / pretty printed).

payload_f.json.zip (544.9 KB)

Modifying your script to read the payload as a file, I had no issue.

I don’t know what your use case is, but you may want to consider processing this entirely on the gateway, and output all the sensor values to dataset tags, instead of running an api call every time and filtering out the ones you want.

Thank you Jordan, it is a valid workaround for our Central gateway but not for the Edge gateways where this information must be available.