B4J Question [solved] how to delete a class occurence ?

svanneste

Member
Licensed User
Hello,
I'm lost on a question, probably simple: how can you destroy the occurrence of a class when it is no longer needed.

In the attached example, a class does a semblance of "ping" every second using a sleep. This occurrence is initialized in a sub of Main.
The event in the class starts a new sleep. So it seems to me that the occurrence can no longer be destroyed.


Of course, the question being when using external libs (mqtt for example or okhttp). That one is for simplicity demonstration only.

Thank you very much
 

Attachments

  • test_classes.zip
    1.2 KB · Views: 127

Andrew (Digitwell)

Well-Known Member
Licensed User
Longtime User
Java does its own garbage collection. So you cannot delete an object directly.

Are you seeing memory being consumed with every timer tick and not released or is this just a theoretical question.

StartCounter is recursive which may blow the stack. However, because of the sleep, this is not a problem.

If you use VisualVM, you can check the memory usage.

1637583794183.png


If you want to force a garbage collection, you can do so as follows:

B4X:
public Sub CollectGarbageNow
    If Not(native.IsInitialized) Then
        native.InitializeStatic("java.lang.System")
    End If
   
    native.RunMethod("gc",Null)  
End Sub

EDIT: for spelling
 
Upvote 0

svanneste

Member
Licensed User
Thanks a lot for your reply. To reply to yours : my question is more related to events rised after the occurrence is no more relevant.
So I will have to maintain a table of usable objects.
Thanks for the tip about the garbage collection. It will be very useful, for sure. ?
 
Upvote 0
Top