Getting java OutOfMemoryError when uploading PDF to SQL

The file is 19,611 KB. Is this considered a large file. I am using the demo PDF upload from the example. Should I increase the amount of memory for the client in the configuration?

How are you uploading the PDF, and what is your client max memory set to? 20mB isn’t a huge amount, but is 15% of a 128mB heap space. If your clients are set that low, then it might be worth bumping them up, but it’s also potentially a problem with our implementation of a scripting method, or your code.

My client is set at the default, 32M max 256M.

Here is my code:

# Read the file as bytes.
				data = system.file.readFileAsBytes(path)
				
				# Ask the user to enter in a filename.
				# Will grab just the filename and extension from the path as a suggestion.
				comments = system.gui.inputBox("Enter file comments", path.split('\\')[-1])
				name = path.split('\\')[-1]
				
				numFiles = system.db.runScalarPrepQuery("SELECT count(id) FROM iTest.dbo.documents WHERE DeviationID = ? AND ignitionField = ?", [DeviationID, ignitionField])
				Version = numFiles + 1
				print "numFiles="+str(numFiles)
				
				# Insert the data and name into the database.
				system.db.runPrepUpdate("INSERT INTO documents (name, data, DeviationID, Version, ignitionTable,ignitionField,comments,Project, actor, timestamp, ahost )\
				VALUES (?,?,?,?,?,?,?,?,?,?,?)",\
				[name,data, DeviationID, Version, ignitionTable, ignitionField, comments, Project, actor, timestamp, ahost])

And you get the out of memory error on the last line, with the runPrepUpdate to insert the data? Can you post the full stack trace of the error? The only part that I could see consuming a fair chunk of memory is when the client has to serialize the PDF, which is already held in memory, into base64 and then hold that base64 in a string to send to the gateway.

If this is the only area where this is a problem, you could potentially work around the base64 serialization requirement by using a gateway message handler and system.util.sendMessage; instead of calling runPrepUpdate at the end of your script, you would send a message to the gateway with the raw byte[] of your file contents, and then the gateway message handler would call system.db.runPrepUpdate.

That’s probably more work than you need to do; unless you’ve got very low-end hardware to run clients on, a bump to 512mB max memory should be more than enough to handle this and still won’t exclude any 32-bit machines from running your project.

Yes, on runPrepUpdate. Also just noticed that I get the error in the client, not in Designer.

I kept doubling the memory until I got to 128/1024 and it uploaded in the client.

Here is the stack trace:

Yes, on the runPrepUpdate.

Traceback (most recent call last):
  File "<event:mouseClicked>", line 1, in <module>
  File "<module:project.Attachments>", line 164, in attachmentIconLabel
  File "<module:project.Attachments>", line 258, in addFieldAttachment
               at java.util.Arrays.copyOf(Unknown Source)

               at java.lang.AbstractStringBuilder.ensureCapacityInternal(Unknown Source)

               at java.lang.AbstractStringBuilder.append(Unknown Source)

               at java.lang.StringBuilder.append(Unknown Source)

               at com.inductiveautomation.ignition.client.gateway_interface.messages.MessageBuilder.appendToBuilder(MessageBuilder.java:81)

               at com.inductiveautomation.ignition.client.gateway_interface.messages.MessageBuilder.appendToBuilder(MessageBuilder.java:86)

               at com.inductiveautomation.ignition.client.gateway_interface.messages.MessageBuilder.arg(MessageBuilder.java:47)

               at com.inductiveautomation.ignition.client.gateway_interface.messages.RunPrepStmtMessage.queryArgs(RunPrepStmtMessage.java:32)

               at com.inductiveautomation.ignition.client.script.ClientDBUtilities._runPrepStmt(ClientDBUtilities.java:209)

               at com.inductiveautomation.ignition.common.script.builtin.AbstractDBUtilities.runPrepUpdate(AbstractDBUtilities.java:256)

               at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)

               at sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source)

               at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source)

               at java.lang.reflect.Method.invoke(Unknown Source)


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

               at org.python.core.Py.JavaError(Py.java:495)
               at org.python.core.Py.JavaError(Py.java:488)
               at org.python.core.PyReflectedFunction.__call__(PyReflectedFunction.java:188)
               at com.inductiveautomation.ignition.common.script.ScriptManager$ReflectedInstanceFunction.__call__(ScriptManager.java:431)
               at org.python.core.PyObject.__call__(PyObject.java:404)
               at org.python.core.PyObject.__call__(PyObject.java:408)
               at org.python.pycode._pyx24.addFieldAttachment$4(<module:project.Attachments>:334)
               at org.python.pycode._pyx24.call_function(<module:project.Attachments>)
               at org.python.core.PyTableCode.call(PyTableCode.java:165)
               at org.python.core.PyBaseCode.call(PyBaseCode.java:149)
               at org.python.core.PyFunction.__call__(PyFunction.java:327)
               at org.python.pycode._pyx24.attachmentIconLabel$3(<module:project.Attachments>:184)
               at org.python.pycode._pyx24.call_function(<module:project.Attachments>)
               at org.python.core.PyTableCode.call(PyTableCode.java:165)
               at org.python.core.PyBaseCode.call(PyBaseCode.java:134)
               at org.python.core.PyFunction.__call__(PyFunction.java:317)
               at org.python.pycode._pyx23.f$0(<event:mouseClicked>:1)
               at org.python.pycode._pyx23.call_function(<event:mouseClicked>)
               at org.python.core.PyTableCode.call(PyTableCode.java:165)
               at org.python.core.PyCode.call(PyCode.java:18)
               at org.python.core.Py.runCode(Py.java:1275)
               at com.inductiveautomation.ignition.common.script.ScriptManager.runCode(ScriptManager.java:636)
               at com.inductiveautomation.factorypmi.application.binding.action.ActionAdapter.runActions(ActionAdapter.java:180)
               at com.inductiveautomation.factorypmi.application.binding.action.ActionAdapter.invoke(ActionAdapter.java:271)
               at com.inductiveautomation.factorypmi.application.binding.action.RelayInvocationHandler.invoke(RelayInvocationHandler.java:57)
               at com.sun.proxy.$Proxy22.mouseClicked(Unknown Source)
               at java.awt.Component.processMouseEvent(Unknown Source)
               at javax.swing.JComponent.processMouseEvent(Unknown Source)
               at java.awt.Component.processEvent(Unknown Source)
               at java.awt.Container.processEvent(Unknown Source)
               at java.awt.Component.dispatchEventImpl(Unknown Source)
               at java.awt.Container.dispatchEventImpl(Unknown Source)
               at java.awt.Component.dispatchEvent(Unknown Source)
               at java.awt.LightweightDispatcher.retargetMouseEvent(Unknown Source)
               at java.awt.LightweightDispatcher.processMouseEvent(Unknown Source)
               at java.awt.LightweightDispatcher.dispatchEvent(Unknown Source)
               at java.awt.Container.dispatchEventImpl(Unknown Source)
               at java.awt.Window.dispatchEventImpl(Unknown Source)
               at java.awt.Component.dispatchEvent(Unknown Source)
               at java.awt.EventQueue.dispatchEventImpl(Unknown Source)
               at java.awt.EventQueue.access$500(Unknown Source)
               at java.awt.EventQueue$3.run(Unknown Source)
               at java.awt.EventQueue$3.run(Unknown Source)
               at java.security.AccessController.doPrivileged(Native Method)
               at java.security.ProtectionDomain$JavaSecurityAccessImpl.doIntersectionPrivilege(Unknown Source)
               at java.security.ProtectionDomain$JavaSecurityAccessImpl.doIntersectionPrivilege(Unknown Source)
               at java.awt.EventQueue$4.run(Unknown Source)
               at java.awt.EventQueue$4.run(Unknown Source)
               at java.security.AccessController.doPrivileged(Native Method)
               at java.security.ProtectionDomain$JavaSecurityAccessImpl.doIntersectionPrivilege(Unknown Source)
               at java.awt.EventQueue.dispatchEvent(Unknown Source)
               at java.awt.EventDispatchThread.pumpOneEventForFilters(Unknown Source)
               at java.awt.EventDispatchThread.pumpEventsForFilter(Unknown Source)
               at java.awt.EventDispatchThread.pumpEventsForHierarchy(Unknown Source)
               at java.awt.EventDispatchThread.pumpEvents(Unknown Source)
               at java.awt.EventDispatchThread.pumpEvents(Unknown Source)
               at java.awt.EventDispatchThread.run(Unknown Source)
Caused by: java.lang.OutOfMemoryError: Java heap space
               at java.util.Arrays.copyOf(Unknown Source)
               at java.lang.AbstractStringBuilder.ensureCapacityInternal(Unknown Source)
               at java.lang.AbstractStringBuilder.append(Unknown Source)
               at java.lang.StringBuilder.append(Unknown Source)
               at com.inductiveautomation.ignition.client.gateway_interface.messages.MessageBuilder.appendToBuilder(MessageBuilder.java:81)
               at com.inductiveautomation.ignition.client.gateway_interface.messages.MessageBuilder.appendToBuilder(MessageBuilder.java:86)
               at com.inductiveautomation.ignition.client.gateway_interface.messages.MessageBuilder.arg(MessageBuilder.java:47)
               at com.inductiveautomation.ignition.client.gateway_interface.messages.RunPrepStmtMessage.queryArgs(RunPrepStmtMessage.java:32)
               at com.inductiveautomation.ignition.client.script.ClientDBUtilities._runPrepStmt(ClientDBUtilities.java:209)
               at com.inductiveautomation.ignition.common.script.builtin.AbstractDBUtilities.runPrepUpdate(AbstractDBUtilities.java:256)
               at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
               at sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source)
               at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source)
               at java.lang.reflect.Method.invoke(Unknown Source)
               at org.python.core.PyReflectedFunction.__call__(PyReflectedFunction.java:186)
               at com.inductiveautomation.ignition.common.script.ScriptManager$ReflectedInstanceFunction.__call__(ScriptManager.java:431)
               at org.python.core.PyObject.__call__(PyObject.java:404)
               at org.python.core.PyObject.__call__(PyObject.java:408)
               at org.python.pycode._pyx24.addFieldAttachment$4(<module:project.Attachments>:334)
               at org.python.pycode._pyx24.call_function(<module:project.Attachments>)
               at org.python.core.PyTableCode.call(PyTableCode.java:165)
               at org.python.core.PyBaseCode.call(PyBaseCode.java:149)
               at org.python.core.PyFunction.__call__(PyFunction.java:327)
               at org.python.pycode._pyx24.attachmentIconLabel$3(<module:project.Attachments>:184)
               at org.python.pycode._pyx24.call_function(<module:project.Attachments>)
               at org.python.core.PyTableCode.call(PyTableCode.java:165)
               at org.python.core.PyBaseCode.call(PyBaseCode.java:134)
               at org.python.core.PyFunction.__call__(PyFunction.java:317)
               at org.python.pycode._pyx23.f$0(<event:mouseClicked>:1)
               at org.python.pycode._pyx23.call_function(<event:mouseClicked>)
               at org.python.core.PyTableCode.call(PyTableCode.java:165)
               at org.python.core.PyCode.call(PyCode.java:18)

Ignition v7.9.10 (b2018112821)
Java: Oracle Corporation 1.8.0_231