websolete
03/09/2022, 2:28 PMtemp = javacast("null","")
vs structdelete(variables,"temp")
?bdw429s
03/09/2022, 2:43 PMwebsolete
03/09/2022, 2:46 PMaliaspooryorik
websolete
03/09/2022, 3:18 PMaliaspooryorik
websolete
03/09/2022, 3:30 PMDavid Buck
03/09/2022, 4:31 PMStructClear(temp)
before deleting the reference, that might at least reduce the size of the object.bdw429s
03/09/2022, 4:38 PMit exits the function it just flags those local vars for GC at the end of the request. i don't believe they're actually 'gone' once the function exits. is this not so?@websolete A few things here • objects aren't proactively "flagged" for GC per se. When the collection runs, it simply scans the heap for objects which have no hard reference to a GC root object. If no references are found "holding" the object, then it's free to be removed including any 'downstream' objects it may have referenced which also have no remaining hard references to a GC root. • There is no relation to the end of an HTTP request and the running of your JVM's GC. Under moderate load, your young collections probably happen every few seconds and your old gen collections probably happen every few minutes. Whatever objects that are found with nothing "holding" on to them are subject to removal. (doesn't mean they will be) • At the end of an HTTP request, the thread (which doesn't go away, but is returned to a pool to service future requests) removes its references to the HTTPServletRequest/Response objects which contain all the data about the request. Adobe and Lucee also use Java's ThreadLocal storage to place the page context, which contains CFML scopes that are request specific. CF engines also remove the thread local storage at the end of the request. • A Thread is a GC root, so anything it references will not be collected. Once the thread removes its pointers to the servlet and page context objects, it is all fair game the next time the GC kicks in. Chances are, most request details and temporary variables will remain in memory for a few seconds to maybe a few minutes before the GC (which is inherently as lazy as possible) bothers to do anything with them. The bigger heap you have, the more lazy the GC will be.
David Buck
03/09/2022, 5:14 PMfileRead()
, for example). Would setting temp = ""
ever have any useful impact on memory usage during the life of a request?bdw429s
03/09/2022, 5:17 PMfileReadLine()
without reading the entire thing into memory. very handy but only applies in some scenarios.David Buck
03/09/2022, 5:31 PMbdw429s
03/09/2022, 5:55 PMa byte array or a stringA string is a byte array internally 😉
A string is a primitive value, right?No, a string is an instance of the
java.lang.String
class. A primitive type in Java is lowercase such as int
, boolean
, long
, char
, etc https://www.baeldung.com/java-primitives (CFML has no concept of a "primitive" type per se)
if GC needs to happen before the size reduction has any impactYes, any objects not GC'd are still taking up space in the heap. Even if you have no references to it any longer in your code, it can still be there and that memory is not usable for a new object until the GC marks it as unused. If you take a heap dump of a running server and analyse it, there will always be a few dozen or hundred thousand "unreachable" objects on the heap at any point in time. Basically just waiting for themselves to be collected. This shouldn't necessarily be alarming though-- it's just how a GC'd language works.
concept of GC is that it just cleans up unreferenced objects every so oftenYep, that's correct. It's worth noting, when you do
var myFile = fileRead( 'hugeFile.txt' );
myFile = "";
you actually create a NEW string object that has an empty string and re-point myVar
to it. So now there's two objects on the heap (the file, and the empty string). But the file is referenced and eligible for collection. Setting to null would skip creating an extra string, but we're really talking about a negligible amount of memory for a single empty string object.
so I'm not sure how it applies to simple values like strings.A java string is an instance of a class just like any other class and it's handled no differently. We call them "simple" values in CFML, but that differentiation is meaningless in Java and doesn't have any bearing on GC. @David Buck
David Buck
03/09/2022, 6:23 PMyou actually create a NEW string object that has an empty string and re-pointWow, I had no idea! Very interesting, and totally different from how I conceptualize it. Thanks for the detailed answers!to it.myVar