Android Question Lifetime of objects

obscure

Member
Licensed User
Longtime User
Howdy!

First off, I'm not after the most efficient way to plot
a pixel. The following code snippets should help
illustrate what I'm thinking and both do the same
thing.
Here we go:
B4X:
Sub Activity_Create(FirstTime As Boolean)
    For a=0 To 50
        putPixel(Rnd(0,20),Rnd(0,20))
    Next
End Sub

Sub putPixel(xPos As Int, yPos As Int)
    Dim reflection As Reflector
    reflection.Target = mainCanvas.Bitmap
    reflection.RunMethod4("setPixel", Array As Object(xPos,yPos,0xffffffff), Array As String("java.lang.int","java.lang.int","java.lang.int"))
End Sub
From my understanding, with each and every call to the
putPixel function, we're instantiating a new Reflector
object. With the end of the function, this object internally
gets marked as not needed anymore and Java's garbage collector
takes care of it, whenever it feels like. Well, this might
take a while and fill up the memory with lots of useless
objects. Or even worse, do this objects remain in the system
memory?
To cut a long story short, would it be better to instantiate a
'global' Reflector object once and reuse it.
A little something like this:
B4X:
Sub Globals
    Dim globalReflector As Reflector
End Sub

Sub Activity_Create(FirstTime As Boolean)
    For a=0 To 50
        putPixel(Rnd(0,20),Rnd(0,20))
    Next
End Sub

Sub putPixel(xPos As Int, yPos As Int)
    globalReflector.Target = mainCanvas.Bitmap
    globalReflector.RunMethod4("setPixel", Array As Object(xPos,yPos,0xffffffff), Array As String("java.lang.int","java.lang.int","java.lang.int"))
End Sub

regards,
obscure
 

obscure

Member
Licensed User
Longtime User
Hi Klaus.

Sure, I know about DrawPoint but as I said I'm after something else. ;)
I just typed this off my fingers to picture the use of a Reflector object.
I really just need to know if it's better to instantiate a local object inside the
function or reuse a global object throughout the application. (this applies to all
kind of objects - not just a Reflector)
In the example above I might need the Reflector object for different tasks inside
the application beside just drawing pixels, hence each function would have it's own
local object.
Anyway, thanks for your reply!

cheers,
obscure
 
Last edited:
Upvote 0
Top