Android Question "Kill" unused objects

yaniv hanya

Active Member
Licensed User
How can I kill objects?
I will explain.
I am building an application based on one activity whose center is a panel to which I load the various screens.
For each screen I create the object that runs it.
When I load a screen, I remove the current screen
B4X:
viewPnl.RemoveAllViews

but its object remains alive.
So after several times the app crashes from the next exception.
Downsampling image due to lack of memory.
Downsampling image due to lack of memory.
Downsampling image due to lack of memory: 2
java.lang.OutOfMemoryError: Failed to allocate a 235140 byte allocation with 121104 free bytes and 118KB until OOM
at dalvik.system.VMRuntime.newNonMovableArray(Native Method)
at android.graphics.Bitmap.nativeCreate(Native Method)
at android.graphics.Bitmap.createBitmap(Bitmap.java:879)
at android.graphics.Bitmap.createBitmap(Bitmap.java:856)
at android.graphics.Bitmap.createBitmap(Bitmap.java:823)
at anywheresoftware.b4a.objects.drawable.CanvasWrapper.Initialize(CanvasWrapper.java:81)
at anywheresoftware.b4a.objects.B4XCanvas.Initialize(B4XCanvas.java:37)
at b4a.example.homescreeno._drawdiamond2(homescreeno.java:149)
at b4a.example.homescreeno._screenclock_tick(homescreeno.java:446)
at java.lang.reflect.Method.invoke(Native Method)
at anywheresoftware.b4a.shell.Shell.runMethod(Shell.java:732)
at anywheresoftware.b4a.shell.Shell.raiseEventImpl(Shell.java:348)
at anywheresoftware.b4a.shell.Shell.raiseEvent(Shell.java:255)
at java.lang.reflect.Method.invoke(Native Method)
at anywheresoftware.b4a.ShellBA.raiseEvent2(ShellBA.java:144)
at anywheresoftware.b4a.objects.Timer$TickTack.run(Timer.java:105)
at android.os.Handler.handleCallback(Handler.java:836)
at android.os.Handler.dispatchMessage(Handler.java:103)

I know I can create each object once and then display it over and over
but I have about 30 screens that most will rarely be used so I prefer not to waste memory.
What's the way to do that?
Attached is an example project
 

Attachments

  • objectsTest.zip
    59.6 KB · Views: 239

Erel

B4X founder
Staff member
Licensed User
Longtime User
Objects are released when there are no live references to the objects. This will not happen immediately.

This code is bad:
B4X:
Sub drawdiamond2(percent As Int, width As Float, height As Float,  R As Int, G As Int, B As Int) As B4XBitmap 'ignore
    Dim cnv As B4XCanvas
    Dim myimage As B4XView = xui.CreatePanel("")
    myimage.SetLayoutAnimated(0,0,0,width,height)
    cnv.Initialize(myimage)
You should instead create the canvas once and reuse it.
 
Upvote 0

yaniv hanya

Active Member
Licensed User
so there is nothing activ i can do to make it happen?
how can I prevent th crush that happens in the atached project
when playing to many times with the buttons?
 
Upvote 0

yaniv hanya

Active Member
Licensed User
I fixed it.
The crash I'm talking about happens when you click too many times The buttons
then create multiple instances of each object and the app crashes
I saw that the main problem was homeScreen object when i created only him the aap crushed after 4 instances
the SetTime object it took 34 instances to crush with this log
art/runtime/indirect_reference_table.cc:116] JNI ERROR (app bug): weak global reference table overflow (max=51200)
art/runtime/indirect_reference_table.cc:116] weak global reference table dump:
art/runtime/indirect_reference_table.cc:116] Last 10 entries (of 51200):
art/runtime/indirect_reference_table.cc:116] 51199: 0x169733d0 android.view.RenderNode
art/runtime/indirect_reference_table.cc:116] 51198: 0x16973478 android.view.RenderNode
art/runtime/indirect_reference_table.cc:116] 51197: 0x16973b38 android.view.RenderNode
art/runtime/indirect_reference_table.cc:116] 51196: 0x16973e08 android.view.RenderNode
art/runtime/indirect_reference_table.cc:116] 51195: 0x16973ee0 android.view.RenderNode
art/runtime/indirect_reference_table.cc:116] 51194: 0x1696d1c0 android.view.RenderNode
art/runtime/indirect_reference_table.cc:116] 51193: 0x1696d250 android.view.RenderNode
art/runtime/indirect_reference_table.cc:116] 51192: 0x1696d268 android.view.RenderNode
art/runtime/indirect_reference_table.cc:116] 51191: 0x1696d418 android.view.RenderNode
art/runtime/indirect_reference_table.cc:116] 51190: 0x1696d490 android.view.RenderNode
art/runtime/indirect_reference_table.cc:116] Summary:
art/runtime/indirect_reference_table.cc:116] 3 of byte[] (360 elements) (3 unique instances)
art/runtime/indirect_reference_table.cc:116] 3 of byte[] (1216 elements) (3 unique instances)
art/runtime/indirect_reference_table.cc:116] 1 of byte[] (1728 elements)
art/runtime/indirect_reference_table.cc:116] 5 of byte[] (2304 elements) (5 unique instances)
art/runtime/indirect_reference_table.cc:116] 4 of byte[] (2520 elements) (4 unique instances)
art/runtime/indirect_reference_table.cc:116] 1 of byte[] (3720 elements)
art/runtime/indirect_reference_table.cc:116] 1 of byte[] (4320 elements)
art/runtime/indirect_reference_table.cc:116] 9 of byte[] (5184 elements) (9 unique instances)
art/runtime/indirect_reference_table.cc:116] 27 of byte[] (6724 elements) (27 unique instances)
art/runtime/indirect_reference_table.cc:116] 1 of byte[] (8064 elements)
art/runtime/indirect_reference_table.cc:116] 1 of byte[] (8112 elements)
art/runtime/indirect_reference_table.cc:116] 2 of byte[] (8712 elements) (2 unique instances)
art/runtime/indirect_reference_table.cc:116] 2 of byte[] (8836 elements) (2 unique instances)
art/runtime/indirect_reference_table.cc:116] 17 of byte[] (9216 elements) (17 unique instances)
art/runtime/indirect_reference_table.cc:116] 3 of byte[] (11664 elements) (3 unique instances)
art/runtime/indirect_reference_table.cc:116] 2 of byte[] (11808 elements) (2 unique instances)
art/runtime/indirect_reference_table.cc:116] 3 of byte[] (19872 elements) (3 unique instances)
art/runtime/indirect_reference_table.cc:116] 1 of byte[] (20592 elements)
art/runtime/indirect_reference_table.cc:116] 3 of byte[] (20736 elements) (3 unique instances)
art/runtime/indirect_reference_table.cc:116] 4 of byte[] (27900 elements) (4 unique instances)
art/runtime/indirect_reference_table.cc:116] 1 of byte[] (51750 elements)
art/runtime/indirect_reference_table.cc:116] 5 of byte[] (67600 elements) (5 unique instances)
art/runtime/indirect_reference_table.cc:116] 20 of java.lang.DexCache (20 unique instances)
art/runtime/indirect_reference_table.cc:116] 2 of dalvik.system.PathClassLoader (1 unique instances)
art/runtime/indirect_reference_table.cc:116] 51079 of android.view.RenderNode (51079 unique instances)
art/runtime/indirect_reference_table.cc:116]
art/runtime/runtime.cc:403] Runtime aborting...
art/runtime/runtime.cc:403] Aborting thread:
art/runtime/runtime.cc:403] "main" prio=5 tid=1 Runnable
art/runtime/runtime.cc:403] | group="" sCount=0 dsCount=0 obj=0x759869d8 self=0x777764ea00
art/runtime/runtime.cc:403] | sysTid=10360 nice=0 cgrp=default sched=0/0 handle=0x777bb5da98
art/runtime/runtime.cc:403] | state=R schedstat=( 39515072255 8256694554 23345 ) utm=3521 stm=430 core=1 HZ=100
art/runtime/runtime.cc:403] | stack=0x7fcae49000-0x7fcae4b000 stackSize=8MB
art/runtime/runtime.cc:403] | held mutexes= "abort lock" "JNI weak global reference table lock" "mutator lock"(shared held)
art/runtime/runtime.cc:403] native: #00 pc 000000000047c2f4 /system/lib64/libart.so (_ZN3art15DumpNativeStackERNSt3__113basic_ostreamIcNS0_11char_traitsIcEEEEiP12BacktraceMapPKcPNS_9ArtMethodEPv+220)
art/runtime/runtime.cc:403] native: #01 pc 000000000047c2f0 /system/lib64/libart.so (_ZN3art15DumpNativeStackERNSt3__113basic_ostreamIcNS0_11char_traitsIcEEEEiP12BacktraceMapPKcPNS_9ArtMethodEPv+216)
art/runtime/runtime.cc:403] native: #02 pc 000000000045044c /system/lib64/libart.so (_ZNK3art6Thread9DumpStackERNSt3__113basic_ostreamIcNS1_11char_traitsIcEEEEbP12BacktraceMap+944)
art/runtime/runtime.cc:403] native: #03 pc 000000000043e44c /system/lib64/libart.so (_ZNK3art10AbortState10DumpThreadERNSt3__113basic_ostreamIcNS1_11char_traitsIcEEEEPNS_6ThreadE+56)
art/runtime/runtime.cc:403] native: #04 pc 000000000043e26c /system/lib64/libart.so (_ZNK3art10AbortState4DumpERNSt3__113basic_ostreamIcNS1_11char_traitsIcEEEE+576)
art/runtime/runtime.cc:403] native: #05 pc 0000000000431e44 /system/lib64/libart.so (_ZN3art7Runtime5AbortEv+144)
art/runtime/runtime.cc:403] native: #06 pc 00000000000e54f8 /system/lib64/libart.so (_ZN3art10LogMessageD2Ev+1208)
art/runtime/runtime.cc:403] native: #07 pc 000000000024c124 /system/lib64/libart.so (_ZN3art22IndirectReferenceTable3AddEjPNS_6mirror6ObjectE+308)
art/runtime/runtime.cc:403] native: #08 pc 00000000002efb60 /system/lib64/libart.so (_ZN3art9JavaVMExt16AddWeakGlobalRefEPNS_6ThreadEPNS_6mirror6ObjectE+80)
art/runtime/runtime.cc:403] native: #09 pc 0000000000350050 /system/lib64/libart.so (_ZN3art3JNI16NewWeakGlobalRefEP7_JNIEnvP8_jobject+596)
art/runtime/runtime.cc:403] native: #10 pc 00000000000e3580 /system/lib64/libandroid_runtime.so (???)
art/runtime/runtime.cc:403] native: #11 pc 0000000000c8a59c /system/framework/arm64/boot-framework.oat (Java_android_view_RenderNode_nCreate__Ljava_lang_String_2+152)
art/runtime/runtime.cc:403] at android.view.RenderNode.nCreate(Native method)
art/runtime/runtime.cc:403] at android.view.RenderNode.<init>(RenderNode.java:137)
art/runtime/runtime.cc:403] at android.view.RenderNode.create(RenderNode.java:161)
art/runtime/runtime.cc:403] at android.view.View.<init>(View.java:4055)
art/runtime/runtime.cc:403] at android.view.View.<init>(View.java:4170)
art/runtime/runtime.cc:403] at android.view.ViewGroup.<init>(ViewGroup.java:603)
art/runtime/runtime.cc:403] at android.view.ViewGroup.<init>(ViewGroup.java:599)
art/runtime/runtime.cc:403] at android.view.ViewGroup.<init>(ViewGroup.java:595)
art/runtime/runtime.cc:403] at android.view.ViewGroup.<init>(ViewGroup.java:591)
art/runtime/runtime.cc:403] at anywheresoftware.b4a.BALayout.<init>(BALayout.java:19)
art/runtime/runtime.cc:403] at anywheresoftware.b4a.objects.PanelWrapper.innerInitialize(PanelWrapper.java:45)
art/runtime/runtime.cc:403] at anywheresoftware.b4a.objects.ViewWrapper.Initialize(ViewWrapper.java:67)
art/runtime/runtime.cc:403] at anywheresoftware.b4a.objects.B4XViewWrapper$XUI.CreatePanel(B4XViewWrapper.java:722)
art/runtime/runtime.cc:403] at b4a.example3.customlistview._createpanel(customlistview.java:237)
art/runtime/runtime.cc:403] at b4a.example3.customlistview._insertatimpl(customlistview.java:554)
art/runtime/runtime.cc:403] at b4a.example3.customlistview._insertat(customlistview.java:538)
art/runtime/runtime.cc:403] at b4a.example3.customlistview._add(customlistview.java:71)
art/runtime/runtime.cc:403] at b4a.example.setdatetime2._createitem(setdatetime2.java:285)
art/runtime/runtime.cc:403] at b4a.example.setdatetime2$ResumableSub_InitializeDayWv.resume(setdatetime2.java:898)
art/runtime/runtime.cc:403] at b4a.example.setdatetime2._initializedaywv(setdatetime2.java:758)
art/runtime/runtime.cc:403] at b4a.example.setdatetime2$ResumableSub_InitializeMounthWv.resume(setdatetime2.java:1574)
art/runtime/runtime.cc:403] at anywheresoftware.b4a.shell.DebugResumableSub$DelegatableResumableSub.resumeAsUserSub(DebugResumableSub.java:48)
art/runtime/runtime.cc:403] at java.lang.reflect.Method.invoke!(Native method)
art/runtime/runtime.cc:403] at anywheresoftware.b4a.shell.Shell.runMethod(Shell.java:732)
art/runtime/runtime.cc:403] at anywheresoftware.b4a.shell.Shell.raiseEventImpl(Shell.java:348)
art/runtime/runtime.cc:403] at anywheresoftware.b4a.shell.Shell.raiseEvent(Shell.java:255)
art/runtime/runtime.cc:403] at java.lang.reflect.Method.invoke!(Native method)
art/runtime/runtime.cc:403] at anywheresoftware.b4a.ShellBA.raiseEvent2(ShellBA.java:144)
art/runtime/runtime.cc:403] at anywheresoftware.b4a.BA.raiseEvent(BA.java:176)
art/runtime/runtime.cc:403] at anywheresoftware.b4a.shell.DebugResumableSub$DelegatableResumableSub.resume(DebugResumableSub.java:43)
art/runtime/runtime.cc:403] at anywheresoftware.b4a.keywords.Common$13.run(Common.java:1705)
art/runtime/runtime.cc:403] at android.os.Handler.handleCallback(Handler.java:836)
art/runtime/runtime.cc:403] at android.os.Handler.dispatchMessage(Handler.java:103)
art/runtime/runtime.cc:403] at android.os.Looper.loop(Looper.java:203)
art/runtime/runtime.cc:403] at android.app.ActivityThread.main(ActivityThread.java:6251)
art/runtime/runtime.cc:403] at java.lang.reflect.Method.invoke!(Native method)
art/runtime/runtime.cc:403] at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1063)
art/runtime/runtime.cc:403] at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:924)
art/runtime/runtime.cc:403] Dumping all threads without appropriate locks held: thread list lock
art/runtime/runtime.cc:403] All threads:
art/runtime/runtime.cc:403] DALVIK THREADS (18):
art/runtime/runtime.cc:403] "main" prio=5 tid=1 Runnable
art/runtime/runtime.cc:403] | group="" sCount=0 dsCount=0 obj=0x759869d8 self=0x777764ea00
art/runtime/runtime.cc:403] | sysTid=10360 nice=0 cgrp=default sched=0/0 handle=0x777bb5da98
art/runtime/runtime.cc:403] | state=R schedstat=( 39588419028 8272291010 23616 ) utm=3523 stm=435 core=0 HZ=100
art/runtime/runtime.cc:403] | stack=0x7fcae49000-0x7fcae4b000 stackSize=8MB
art/runtime/runtime.cc:403] | held mutexes= "abort lock" "JNI weak global reference table lock" "mutator lock"(shared held)
art/runtime/runtime.cc:403] native: #00 pc 000000000047c2f4 /system/lib64/libart.so (_ZN3art15DumpNativeStackERNSt3__113basic_ostreamIcNS0_11char_traitsIcEEEEiP12BacktraceMapPKcPNS_9ArtMethodEPv+220)
art/runtime/runtime.cc:403] native: #01 pc 000000000047c2f0 /system/lib64/libart.so (_ZN3art15DumpNativeStackERNSt3__113basic_ostreamIcNS0_11char_traitsIcEEEEiP12BacktraceMapPKcPNS_9ArtMethodEPv+216)
art/runtime/runtime.cc:403] native: #02 pc 000000000045044c /system/lib64/libart.so (_ZNK3art6Thread9DumpStackERNSt3__113basic_ostreamIcNS1_11char_traitsIcEEEEbP12BacktraceMap+944)
art/runtime/runtime.cc:403] native: #03 pc 0000000000467fe0 /system/lib64/libart.so (_ZN3art14DumpCheckpoint3RunEPNS_6ThreadE+832)
art/runtime/runtime.cc:403] native: #04 pc 00000000004602ac /system/lib64/libart.so (_ZN3art10ThreadList13RunCheckpointEPNS_7ClosureE+476)
art/runtime/runtime.cc:403] native: #05 pc 000000000045fe9c /system/lib64/libart.so (_ZN3art10ThreadList4DumpERNSt3__113basic_ostreamIcNS1_11char_traitsIcEEEEb+848)
art/runtime/runtime.cc:403] native: #06 pc 000000000043e27c /system/lib64/libart.so (_ZNK3art10AbortState4DumpERNSt3__113basic_ostreamIcNS1_11char_traitsIcEEEE+592)
 

Attachments

  • objectsTest.zip
    59.3 KB · Views: 215
Last edited:
Upvote 0
Top