# Java heap space when running asynchronous script

**URL:** <https://forum.inductiveautomation.com/t/java-heap-space-when-running-asynchronous-script/34997>\
**Category:** Ignition\
**Created:** [May 24, 2020, 9:19am UTC](https://forum.inductiveautomation.com/t/java-heap-space-when-running-asynchronous-script/34997 "2020-05-24T09:19:35Z")\
**Posts on this page:** 20\
**Page:** 1

<div class="post-metadata">

**Author:** ![jose.granero](https://sea1.discourse-cdn.com/inductiveautomation/user_avatar/forum.inductiveautomation.com/jose.granero/32/11190_2.png) [@jose.granero](https://forum.inductiveautomation.com/u/jose.granero)\
**Post date:** [May 24, 2020, 9:19am UTC](https://forum.inductiveautomation.com/t/java-heap-space-when-running-asynchronous-script/34997/1 "2020-05-24T09:19:35Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![nminchin](https://sea1.discourse-cdn.com/inductiveautomation/user_avatar/forum.inductiveautomation.com/nminchin/32/6911_2.png) [@nminchin](https://forum.inductiveautomation.com/u/nminchin)\
**Post date:** [May 24, 2020, 12:44pm UTC](https://forum.inductiveautomation.com/t/java-heap-space-when-running-asynchronous-script/34997/2 "2020-05-24T12:44:26Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![pturmel](https://sea1.discourse-cdn.com/inductiveautomation/user_avatar/forum.inductiveautomation.com/pturmel/32/3295_2.png) [@pturmel](https://forum.inductiveautomation.com/u/pturmel)\
**Post date:** [May 24, 2020, 1:53pm UTC](https://forum.inductiveautomation.com/t/java-heap-space-when-running-asynchronous-script/34997/3 "2020-05-24T13:53:07Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![jose.granero](https://sea1.discourse-cdn.com/inductiveautomation/user_avatar/forum.inductiveautomation.com/jose.granero/32/11190_2.png) [@jose.granero](https://forum.inductiveautomation.com/u/jose.granero)\
**Post date:** [May 24, 2020, 9:10pm UTC](https://forum.inductiveautomation.com/t/java-heap-space-when-running-asynchronous-script/34997/4 "2020-05-24T21:10:04Z")

</div>

Hi,

This is the function:

```auto
> 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`

```auto
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

```

---

<div class="post-metadata">

**Author:** ![nminchin](https://sea1.discourse-cdn.com/inductiveautomation/user_avatar/forum.inductiveautomation.com/nminchin/32/6911_2.png) [@nminchin](https://forum.inductiveautomation.com/u/nminchin)\
**Post date:** [May 24, 2020, 10:01pm UTC](https://forum.inductiveautomation.com/t/java-heap-space-when-running-asynchronous-script/34997/5 "2020-05-24T22:01:13Z")

</div>

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?

---

<div class="post-metadata">

**Author:** ![pturmel](https://sea1.discourse-cdn.com/inductiveautomation/user_avatar/forum.inductiveautomation.com/pturmel/32/3295_2.png) [@pturmel](https://forum.inductiveautomation.com/u/pturmel)\
**Post date:** [May 24, 2020, 11:41pm UTC](https://forum.inductiveautomation.com/t/java-heap-space-when-running-asynchronous-script/34997/6 "2020-05-24T23:41:42Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![jose.granero](https://sea1.discourse-cdn.com/inductiveautomation/user_avatar/forum.inductiveautomation.com/jose.granero/32/11190_2.png) [@jose.granero](https://forum.inductiveautomation.com/u/jose.granero)\
**Post date:** [May 25, 2020, 6:35am UTC](https://forum.inductiveautomation.com/t/java-heap-space-when-running-asynchronous-script/34997/7 "2020-05-25T06:35:26Z")

</div>

I already modified it, the key is not valid 😏

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

---

<div class="post-metadata">

**Author:** ![jose.granero](https://sea1.discourse-cdn.com/inductiveautomation/user_avatar/forum.inductiveautomation.com/jose.granero/32/11190_2.png) [@jose.granero](https://forum.inductiveautomation.com/u/jose.granero)\
**Post date:** [May 25, 2020, 6:47am UTC](https://forum.inductiveautomation.com/t/java-heap-space-when-running-asynchronous-script/34997/8 "2020-05-25T06:47:33Z")

</div>

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:

```auto
	obj = system.util.jsonDecode(source)

```

Thank you.

---

<div class="post-metadata">

**Author:** ![Kevin.Herron](https://sea1.discourse-cdn.com/inductiveautomation/user_avatar/forum.inductiveautomation.com/kevin.herron/32/7259_2.png) [@Kevin.Herron](https://forum.inductiveautomation.com/u/Kevin.Herron)\
**Post date:** [May 25, 2020, 12:59pm UTC](https://forum.inductiveautomation.com/t/java-heap-space-when-running-asynchronous-script/34997/9 "2020-05-25T12:59:12Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![jose.granero](https://sea1.discourse-cdn.com/inductiveautomation/user_avatar/forum.inductiveautomation.com/jose.granero/32/11190_2.png) [@jose.granero](https://forum.inductiveautomation.com/u/jose.granero)\
**Post date:** [May 25, 2020, 1:51pm UTC](https://forum.inductiveautomation.com/t/java-heap-space-when-running-asynchronous-script/34997/10 "2020-05-25T13:51:13Z")

</div>

Hi Kevin,

Please find enclosed the file.

[Payload.zip](https://forum.inductiveautomation.com/uploads/short-url/lwfoPqAVizdUYHmUEc0jJ2qIlZk.zip) (217.0 KB)

---

<div class="post-metadata">

**Author:** ![Kevin.Herron](https://sea1.discourse-cdn.com/inductiveautomation/user_avatar/forum.inductiveautomation.com/kevin.herron/32/7259_2.png) [@Kevin.Herron](https://forum.inductiveautomation.com/u/Kevin.Herron)\
**Post date:** [May 25, 2020, 1:55pm UTC](https://forum.inductiveautomation.com/t/java-heap-space-when-running-asynchronous-script/34997/11 "2020-05-25T13:55:11Z")

</div>

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…

---

<div class="post-metadata">

**Author:** ![Kevin.Herron](https://sea1.discourse-cdn.com/inductiveautomation/user_avatar/forum.inductiveautomation.com/kevin.herron/32/7259_2.png) [@Kevin.Herron](https://forum.inductiveautomation.com/u/Kevin.Herron)\
**Post date:** [May 25, 2020, 2:03pm UTC](https://forum.inductiveautomation.com/t/java-heap-space-when-running-asynchronous-script/34997/12 "2020-05-25T14:03:08Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![jose.granero](https://sea1.discourse-cdn.com/inductiveautomation/user_avatar/forum.inductiveautomation.com/jose.granero/32/11190_2.png) [@jose.granero](https://forum.inductiveautomation.com/u/jose.granero)\
**Post date:** [May 25, 2020, 2:24pm UTC](https://forum.inductiveautomation.com/t/java-heap-space-when-running-asynchronous-script/34997/13 "2020-05-25T14:24:04Z")

</div>

Thank you Kevin,

I truncated it by mistake.

I can’t figure out how to solve it.

---

<div class="post-metadata">

**Author:** ![Kevin.Herron](https://sea1.discourse-cdn.com/inductiveautomation/user_avatar/forum.inductiveautomation.com/kevin.herron/32/7259_2.png) [@Kevin.Herron](https://forum.inductiveautomation.com/u/Kevin.Herron)\
**Post date:** [May 25, 2020, 2:27pm UTC](https://forum.inductiveautomation.com/t/java-heap-space-when-running-asynchronous-script/34997/14 "2020-05-25T14:27:20Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![jose.granero](https://sea1.discourse-cdn.com/inductiveautomation/user_avatar/forum.inductiveautomation.com/jose.granero/32/11190_2.png) [@jose.granero](https://forum.inductiveautomation.com/u/jose.granero)\
**Post date:** [May 25, 2020, 3:02pm UTC](https://forum.inductiveautomation.com/t/java-heap-space-when-running-asynchronous-script/34997/15 "2020-05-25T15:02:05Z")

</div>

Hi Kevin,

Here you have.[Payload.zip](https://forum.inductiveautomation.com/uploads/short-url/bIZIImQNBUofMkZgeqKoePImoa4.zip) (461.9 KB)

---

<div class="post-metadata">

**Author:** ![Kevin.Herron](https://sea1.discourse-cdn.com/inductiveautomation/user_avatar/forum.inductiveautomation.com/kevin.herron/32/7259_2.png) [@Kevin.Herron](https://forum.inductiveautomation.com/u/Kevin.Herron)\
**Post date:** [May 25, 2020, 4:51pm UTC](https://forum.inductiveautomation.com/t/java-heap-space-when-running-asynchronous-script/34997/16 "2020-05-25T16:51:38Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![jose.granero](https://sea1.discourse-cdn.com/inductiveautomation/user_avatar/forum.inductiveautomation.com/jose.granero/32/11190_2.png) [@jose.granero](https://forum.inductiveautomation.com/u/jose.granero)\
**Post date:** [May 25, 2020, 5:18pm UTC](https://forum.inductiveautomation.com/t/java-heap-space-when-running-asynchronous-script/34997/17 "2020-05-25T17:18:29Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![Kevin.Herron](https://sea1.discourse-cdn.com/inductiveautomation/user_avatar/forum.inductiveautomation.com/kevin.herron/32/7259_2.png) [@Kevin.Herron](https://forum.inductiveautomation.com/u/Kevin.Herron)\
**Post date:** [May 25, 2020, 5:24pm UTC](https://forum.inductiveautomation.com/t/java-heap-space-when-running-asynchronous-script/34997/18 "2020-05-25T17:24:25Z")

</div>

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

[payload\_f.json.zip](https://forum.inductiveautomation.com/uploads/short-url/eofGETUUSptGbf9phA07Ru0VU2Y.zip) (544.9 KB)

---

<div class="post-metadata">

**Author:** ![JordanCClark](https://sea1.discourse-cdn.com/inductiveautomation/user_avatar/forum.inductiveautomation.com/jordancclark/32/83362_2.png) [@JordanCClark](https://forum.inductiveautomation.com/u/JordanCClark)\
**Post date:** [May 26, 2020, 11:09am UTC](https://forum.inductiveautomation.com/t/java-heap-space-when-running-asynchronous-script/34997/19 "2020-05-26T11:09:38Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![jose.granero](https://sea1.discourse-cdn.com/inductiveautomation/user_avatar/forum.inductiveautomation.com/jose.granero/32/11190_2.png) [@jose.granero](https://forum.inductiveautomation.com/u/jose.granero)\
**Post date:** [May 26, 2020, 12:09pm UTC](https://forum.inductiveautomation.com/t/java-heap-space-when-running-asynchronous-script/34997/20 "2020-05-26T12:09:06Z")

</div>

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