Hey Guys,
I want to see what type of code would cause memory leaks?
Appreciate if you can post some example code that causes memory leaks..
Thanks,
Mahesh
This is related to OScript (General Callback Scripts). I heard from my friend saying there is a memory leak issues within their custom modules. So I am interested in exploring what could be a scenario that would cause memory leaks..
In general, a memory leak occurs if objects or data remain in memory between server requests, outside of the context of a managed cache.
For example, if you stuff data into global variables, that data would persist from request to request and would not be torn down when the request handler instance is destroyed.
Similarly, if you create a temp object and do not destroy it, that temp object will also remain.
Along these lines, storing data against the base definition of an object instead of against an instance of the object (which can then be torn down) is generally a no-no. Since the base definition stays in memory between requests, this could be a source of a memory leak. Another thing to keep an eye out for is an unbounded cache. Although technically not a memory leak, an unbounded cache can result in uncontrolled object growth that eventually kills the host = a.k.a. a software "cancer".
One final obscure one is through the use of circular references. The Assoc datatype is a structure that is passed by reference and manages references. If you create an Assoc A that contains a reference to another Assoc B which similarly also has a reference back to Assoc A, those assocs will not be cleaned up. The reason for this is because objects are cleaned from memory based on reference counts. The moment the Oscript VM determines that no further references exist to an object, the object is destroyed. Normally, these assocs would be destroyed when the script defining them finishes executing as the assocs go out of scope (lose their references). However, since the assocs have induced a reference to each other that lives outside of the scope of the script, these assocs will be orphaned in memory, resulting in a memory leak. Cheers, Kyle