java.lang.OutOfMemoryError: Java heap space

I’m running into this when pulling json from a webservice. Any ideas on what I need to do to correct this?

Thanks,

That’s sort of a generic error, but like it says, you’re running out of heap space. Firstly, is there more to the error message? If so, please include it? Secondly, have you measured your memory usage for the server? if so, what did you find? How big is your heap set to? Have you tried increasing it? And finally, how big is the JSON you’re parsing?

Lucee 5.2.3.35 Error (java.io.IOException)
Message	java.lang.OutOfMemoryError:Java heap space
Java Stacktrace	lucee.runtime.exp.NativeException: java.lang.OutOfMemoryError:Java heap space
 	at java.util.Arrays.copyOf(Arrays.java:3332)
 	at java.lang.AbstractStringBuilder.ensureCapacityInternal(AbstractStringBuilder.java:124)
 	at java.lang.AbstractStringBuilder.append(AbstractStringBuilder.java:596)
 	at java.lang.StringBuffer.append(StringBuffer.java:367)
 	at java.io.StringWriter.write(StringWriter.java:94)
 	at lucee.commons.io.IOUtil.copy(IOUtil.java:334)
 	at lucee.commons.io.IOUtil.access$400(IOUtil.java:60)
 	at lucee.commons.io.IOUtil$Copy.run(IOUtil.java:1197)
 Caused by: java.io.IOException: java.lang.OutOfMemoryError:Java heap space
 	... 8 more
 
Timestamp	12/5/17 7:02:39 PM UTC
free -m
             total       used       free     shared    buffers     cached
Mem:           993        920         72          0         24        283
-/+ buffers/cache:        612        381
Swap:            0          0          0
cat /proc/meminfo
MemTotal:        1017324 kB
MemFree:           74224 kB
MemAvailable:     280912 kB
Buffers:           25508 kB
Cached:           290232 kB
SwapCached:            0 kB
Active:           725136 kB
Inactive:         155432 kB
Active(anon):     564832 kB
Inactive(anon):       52 kB
Active(file):     160304 kB
Inactive(file):   155380 kB
Unevictable:           0 kB
Mlocked:               0 kB
SwapTotal:             0 kB
SwapFree:              0 kB
Dirty:                16 kB
Writeback:             0 kB
AnonPages:        564860 kB
Mapped:            44752 kB
Shmem:                60 kB
Slab:              47712 kB
SReclaimable:      37452 kB
SUnreclaim:        10260 kB
KernelStack:        2368 kB
PageTables:         4080 kB
NFS_Unstable:          0 kB
Bounce:                0 kB
WritebackTmp:          0 kB
CommitLimit:      508660 kB
Committed_AS:     814956 kB
VmallocTotal:   34359738367 kB
VmallocUsed:           0 kB
VmallocChunk:          0 kB
AnonHugePages:         0 kB
ShmemHugePages:        0 kB
ShmemPmdMapped:        0 kB
HugePages_Total:       0
HugePages_Free:        0
HugePages_Rsvd:        0
HugePages_Surp:        0
Hugepagesize:       2048 kB
DirectMap4k:       12288 kB
DirectMap2M:     1036288 kB

In Lucee Admin (top right):

Heap
53%

The JVM (Java Virtual Machine) has a heap that is the runtime data area from which memory for all objects are allocated.

Non-Heap
5%

Also, the JVM has memory other than the heap, referred to as non-heap memory. It stores all cfc/cfm templates, java classes, interned Strings and meta-data.

Not sure where to adjust heap size?

For Tomcat create a setenv.bat or setenv.sh depending on OS. Do a set JAVA_OPTS=-Xms1024M -Xmx1024M or whatever memory amount you need. You can hover over the HEAP line in Lucee Admin to see what it is defaulting to.

Looks like you’ve got 1GB of total RAM there. I’m assuming you’re running a VM of some sort? Maybe a micro instance on AWS? Either way, 1GB of RAM is fairly sparse and doesn’t leave you a lot of wiggle room. For instance, setting your heap size to 1024MB won’t help you much here because there isn’t going to be that much RAM available anyway.

If you’re on a VM, I’d up the amount of RAM available to that server to at least 2GB since it’s just a Linux server. If you’re on a micro AWS, you may need to go to a bigger instance to get the best performance - especially if you’re digesting large datasets.

HTH

– Denny

Ya I’m running a micro instance on AWS T2-micro. Trying to see what I can get away with as far as smallest cost. Do you have any recommendations on what instance type Lucee should really run on?

Thanks for the help.

Selecting the proper sized compute instance depends on a lot of factors - how performant is the code, how many concurrent users are anticipated using that code, how quickly you want the site to respond under load, etc. etc.

Lucee can run just fine under a micro instance, for example, so long as the code is well tuned, there aren’t many concurrent users and you don’t mind it responding slowly under heavy load.

micro can be a good starting point, until you run into issues like you have with outOfMemory errors. This is where FusionReactor can really help by showing usage patterns, slow queries and pages, etc. so you can a) adjust code that is performing poorly and b) determine average load and peak load on the system.

The other alternative is to go up one tier at a time, getting more resources (memory, cpu) as needed through trial and error - not the recommended approach, but if you don’t have/can’t install a trial of/can’t afford a license for FR, it’s the path I’d choose - go up to the next tier where you have more RAM first, since that seems to be where your bottleneck is currently, and give it another go.

HTH

– Denny

2 Likes

thanks for the information everyone that was very helpful

I am running a typical windows server 2012 install with IIS. All running the current Lucee as of October 2018 on a clean install.

I run some big image processing tasks in a daily processing system that handles about 3000 images daily which processing takes as long as a half hour. From time to time I get a bigger list and in turn I get a “java.lang.OutOfMemoryError: Java heap space” error.

I have tried a few times to set the correct setting in my setenv.bat file but I have not gotten it right as of yet. Here is the file I am trying today. Can anybody confirm if this is correct?

C:\lucee\tomcat\bin\setenv.bat

rem Start/Stop Script for the CATALINA Server
rem Tomcat memory settings
rem -Xms set initial Java heap size
rem -Xmx set maximum Java heap size

JAVA_OPTS=“-Xms1536m -Xmx1536m”

rem additional JVM arguments can be added to the above line as needed, such as
rem custom Garbage Collection arguments.

In Windows line should be:

set "JAVA_OPTS=-Xms1536m -Xmx1536m"

C:\lucee\tomcat\bin\setenv.bat

rem Tomcat memory settings
rem -Xms set initial Java heap size
rem -Xmx set maximum Java heap size

JAVA_OPTS=-Xms1536m -Xmx1536m

rem additional JVM arguments can be added to the above line as needed, such as
rem custom Garbage Collection arguments.

With that, here is my final, I will report later if it resolves the issue. Thank you @kabutotx

No. On mine I needed it just like I typed. With the set command and everything after in quoutes.

rem Tomcat memory settings
rem -Xms set initial Java heap size
rem -Xmx set maximum Java heap size
set “JAVA_OPTS=-Xms1536m -Xmx1536m”
rem additional JVM arguments can be added to the above line as needed, such as
rem custom Garbage Collection arguments.