I don't think the code you're talking about could cause a Java heap space issue unless you have low memory allocation settings for the server's JVM config and an extremely high volume of requests. Are you running a single instance of the server? Multiple instances behind a load balancer?
So this code
<cfset SetVariable("b.vari.#c.i#",variables[c.i])>
is the same as
<cfset b.vari[c.i] = variables[c.i]>
is technically
<cfset variables.b.vari[c.i] = variables[c.i]>
So you're double-stacking the same data into the variables
scope of the request. Dump the variables
scope after that line and you'll see the same data in there twice.
<cfdump var="#variables#">
BUT, a struct
and other complex objects in CF are passed by reference and not by value. So both variables should be referencing the same memory space that was taken by variables[c.i]
.
You might check the -Xms
(initial memory allocation) and -Xmx
(maximum memory allocation) settings of your CF server's jvm.confg
file. You can either edit this in the JVM settings section of CF admin directly in the file of that name.
If you've never edited those values from the defaults, you might have too little memory allocated for your CF server compared to what's available to the server as a whole. It's often the case that your application's usage eventually crosses a threshold that requires you to add and allocate more RAM to the server to handle the increased load.
While you're in there, check if the server is referencing the version of Java that shipped with your CF installation. You might also be behind on the recommended current secure version of Java for your version of CF.
What are the -Xms and -Xmx parameters when starting JVM?
https://www.cfguide.io/coldfusion-administrator/server-settings-java-jvm/