ratish
11 years agoVisitor
help to understand memory management
Hi
Sharing you a sample design pattern i have implemented towards an object oriented framework design. Need some help to understand if the object allocation and deallocation done in code does help the GarbageCollector to free up memory accordingly.
Below pasted is illustration of How I defined a class and used it in main.brs. Controller.brs holds a class with an id, an array object and two methods addListener and OnClose.
Inside main.brs's Main() i am creating an instance of Controller by Calling InitializeController() which returns me the 'this' associative array holding id, Listeners and the functions.
I can use the returned 'this' as controller object and access its elements using dot operator.
controllerObj1.id, controllerObj1.addListener(), controllerObj1.OnClose() etc.
In main() once i am done with controllerObj1, i am calling controllerObj1.OnClose() which in turn will release all element in controllerObj1.
Would like to understand if the Garbage collector would get triggered to free up memory allocated by the elements id, Listeners when we do controllerObj1.OnClose().
Or just calling controllerObj1 = invalid serve the purpose of releasing the elements?
Please ignore any typo in syntax as this was typed down for illustration and its not a working code.
Controller.brs
--------------------
Function InitializeController()
this = {
id:"controller1"
Listeners:CreateObject("roAssociativeArray")
addListener:CTRL_AddListener
onClose:CTRL_OnClose
}
return this
End Function
Function CTRL_AddListener(keyName, value)
m.Listeners.AddReplace(keyName, value)
End Function
Function CTRL_OnClose()
for each obj in m
if (type(m[obj]) <> "roFunction") or (type(m[obj]) = "roFunction" and obj <> "onclose") then
m[obj] = invalid
m.Delete(obj)
end if
next
End Function
Main.brs
----------
sub Main(argArr = invalid as Dynamic )
controllerObj1 = InitializeController()
.
.
*****some operetions with the controllerObj1 *******
controllerObj1 .addListener("listener1", listenerObj1)
controllerObj1 .addListener("listener2", listenerObj2)
'release the controllerObj1
controllerObj1 .OnClose()
controllerObj1 = invalid
end Sub
Sharing you a sample design pattern i have implemented towards an object oriented framework design. Need some help to understand if the object allocation and deallocation done in code does help the GarbageCollector to free up memory accordingly.
Below pasted is illustration of How I defined a class and used it in main.brs. Controller.brs holds a class with an id, an array object and two methods addListener and OnClose.
Inside main.brs's Main() i am creating an instance of Controller by Calling InitializeController() which returns me the 'this' associative array holding id, Listeners and the functions.
I can use the returned 'this' as controller object and access its elements using dot operator.
controllerObj1.id, controllerObj1.addListener(), controllerObj1.OnClose() etc.
In main() once i am done with controllerObj1, i am calling controllerObj1.OnClose() which in turn will release all element in controllerObj1.
Would like to understand if the Garbage collector would get triggered to free up memory allocated by the elements id, Listeners when we do controllerObj1.OnClose().
Or just calling controllerObj1 = invalid serve the purpose of releasing the elements?
Please ignore any typo in syntax as this was typed down for illustration and its not a working code.
Controller.brs
--------------------
Function InitializeController()
this = {
id:"controller1"
Listeners:CreateObject("roAssociativeArray")
addListener:CTRL_AddListener
onClose:CTRL_OnClose
}
return this
End Function
Function CTRL_AddListener(keyName, value)
m.Listeners.AddReplace(keyName, value)
End Function
Function CTRL_OnClose()
for each obj in m
if (type(m[obj]) <> "roFunction") or (type(m[obj]) = "roFunction" and obj <> "onclose") then
m[obj] = invalid
m.Delete(obj)
end if
next
End Function
Main.brs
----------
sub Main(argArr = invalid as Dynamic )
controllerObj1 = InitializeController()
.
.
*****some operetions with the controllerObj1 *******
controllerObj1 .addListener("listener1", listenerObj1)
controllerObj1 .addListener("listener2", listenerObj2)
'release the controllerObj1
controllerObj1 .OnClose()
controllerObj1 = invalid
end Sub