Android Question outofmemoryerror problem

paul allen

Member
Licensed User
Longtime User
i have a large app that loads several large arrays, after loading 6 or 7 time i get this error: java.lang.OutOfMemoryError: Failed to allocate a 55308 byte allocation with 45840 free bytes and 44KB until OOM. How can i clear memory used each time i go to restart loading the arrays?
Thanks.
Paul
 

paul allen

Member
Licensed User
Longtime User
ok, thanks for the help, below is error message full.

java.lang.OutOfMemoryError: Failed to allocate a 82956 byte allocation with 65360 free bytes and 63KB until OOM
at dalvik.system.VMRuntime.newNonMovableArray(Native Method)
at android.graphics.BitmapFactory.nativeDecodeAsset(Native Method)
at android.graphics.BitmapFactory.decodeStream(BitmapFactory.java:620)
at android.graphics.BitmapFactory.decodeResourceStream(BitmapFactory.java:455)
at android.graphics.drawable.Drawable.createFromResourceStream(Drawable.java:1152)
at android.content.res.ResourcesImpl.loadDrawableForCookie(ResourcesImpl.java:720)
at android.content.res.ResourcesImpl.loadDrawable(ResourcesImpl.java:571)
at android.content.res.Resources.getDrawable(Resources.java:767)
at android.content.res.Resources.getDrawable(Resources.java:738)
at android.app.ApplicationPackageManager.getDefaultActivityIcon(ApplicationPackageManager.java:1092)
at com.android.internal.policy.PhoneWindow.installDecor(PhoneWindow.java:2716)
at com.android.internal.policy.PhoneWindow.setContentView(PhoneWindow.java:435)
at com.android.internal.policy.PhoneWindow.setContentView(PhoneWindow.java:426)
at android.app.Activity.setContentView(Activity.java:2434)
at us.videoservice.ctv2.playit.onCreate(playit.java:62)
at android.app.Activity.performCreate(Activity.java:6664)
at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1118)
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2599)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2707)
at android.app.ActivityThread.-wrap12(ActivityThread.java)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1460)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:154)
at android.app.ActivityThread.main(ActivityThread.java:6077)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:865)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:755)
--------- beginning of crash
 
Upvote 0

paul allen

Member
Licensed User
Longtime User
here is another error message.

java.lang.reflect.InvocationTargetException
at java.lang.reflect.Method.invoke(Native Method)
at anywheresoftware.b4a.DynamicBuilder.build(DynamicBuilder.java:38)
at anywheresoftware.b4a.keywords.LayoutBuilder.loadLayoutHelper(LayoutBuilder.java:349)
at anywheresoftware.b4a.keywords.LayoutBuilder.loadLayoutHelper(LayoutBuilder.java:453)
at anywheresoftware.b4a.keywords.LayoutBuilder.loadLayout(LayoutBuilder.java:148)
at anywheresoftware.b4a.objects.PanelWrapper.LoadLayout(PanelWrapper.java:134)
at us.videoservice.ctv2.shows._builditems(shows.java:436)
at us.videoservice.ctv2.shows._jobdone(shows.java:548)
at java.lang.reflect.Method.invoke(Native Method)
at anywheresoftware.b4a.BA.raiseEvent2(BA.java:213)
at anywheresoftware.b4a.keywords.Common$11.run(Common.java:1178)
at android.os.Handler.handleCallback(Handler.java:751)
at android.os.Handler.dispatchMessage(Handler.java:95)
at android.os.Looper.loop(Looper.java:154)
at android.app.ActivityThread.main(ActivityThread.java:6077)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:865)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:755)
Caused by: java.lang.reflect.InvocationTargetException
at java.lang.reflect.Constructor.newInstance0(Native Method)
at java.lang.reflect.Constructor.newInstance(Constructor.java:430)
at anywheresoftware.b4a.objects.ViewWrapper.buildNativeView(ViewWrapper.java:572)
at anywheresoftware.b4a.objects.ButtonWrapper.build(ButtonWrapper.java:95)
... 18 more
Caused by: java.lang.OutOfMemoryError: OutOfMemoryError thrown while trying to throw OutOfMemoryError; no stack trace available
shows_builditems (java line: 436)
java.lang.RuntimeException: java.lang.RuntimeException: Object should first be initialized (Button).
at anywheresoftware.b4a.keywords.LayoutBuilder.loadLayout(LayoutBuilder.java:170)
at anywheresoftware.b4a.objects.PanelWrapper.LoadLayout(PanelWrapper.java:134)
at us.videoservice.ctv2.shows._builditems(shows.java:436)
at us.videoservice.ctv2.shows._jobdone(shows.java:548)
at java.lang.reflect.Method.invoke(Native Method)
at anywheresoftware.b4a.BA.raiseEvent2(BA.java:213)
at anywheresoftware.b4a.keywords.Common$11.run(Common.java:1178)
at android.os.Handler.handleCallback(Handler.java:751)
at android.os.Handler.dispatchMessage(Handler.java:95)
at android.os.Looper.loop(Looper.java:154)
at android.app.ActivityThread.main(ActivityThread.java:6077)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:865)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:755)
Caused by: java.lang.RuntimeException: Object should first be initialized (Button).
at anywheresoftware.b4a.AbsObjectWrapper.getObject(AbsObjectWrapper.java:67)
at anywheresoftware.b4a.objects.ViewWrapper.innerInitialize(ViewWrapper.java:73)
at anywheresoftware.b4a.objects.ButtonWrapper.innerInitialize(ButtonWrapper.java:39)
at anywheresoftware.b4a.keywords.LayoutBuilder.loadLayoutHelper(LayoutBuilder.java:434)
at anywheresoftware.b4a.keywords.LayoutBuilder.loadLayoutHelper(LayoutBuilder.java:453)
at anywheresoftware.b4a.keywords.LayoutBuilder.loadLayout(LayoutBuilder.java:148)
... 13 more
java.lang.RuntimeException: java.lang.RuntimeException: Object should first be initialized (Button).
java.lang.OutOfMemoryError: Failed to allocate a 76044 byte allocation with 7232 free bytes and 7KB until OOM
at dalvik.system.VMRuntime.newNonMovableArray(Native Method)
at android.graphics.BitmapFactory.nativeDecodeAsset(Native Method)
at android.graphics.BitmapFactory.decodeStream(BitmapFactory.java:620)
at android.graphics.BitmapFactory.decodeResourceStream(BitmapFactory.java:455)
at android.graphics.drawable.Drawable.createFromResourceStream(Drawable.java:1152)
at android.content.res.ResourcesImpl.loadDrawableForCookie(ResourcesImpl.java:720)
at android.content.res.ResourcesImpl.loadDrawable(ResourcesImpl.java:571)
at android.content.res.Resources.getDrawable(Resources.java:767)
at android.content.Context.getDrawable(Context.java:525)
at android.view.View.setBackgroundResource(View.java:18104)
at com.android.internal.app.AlertController.setBackground(AlertController.java:859)
at com.android.internal.app.AlertController.setupView(AlertController.java:561)
at com.android.internal.app.AlertController.installContent(AlertController.java:237)
at android.app.AlertDialog.onCreate(AlertDialog.java:423)
at android.app.Dialog.dispatchOnCreate(Dialog.java:389)
at android.app.Dialog.show(Dialog.java:293)
at anywheresoftware.b4a.Msgbox.msgbox(Msgbox.java:186)
at anywheresoftware.b4a.BA.ShowErrorMsgbox(BA.java:287)
at anywheresoftware.b4a.BA.raiseEvent2(BA.java:251)
at anywheresoftware.b4a.keywords.Common$11.run(Common.java:1178)
at android.os.Handler.handleCallback(Handler.java:751)
at android.os.Handler.dispatchMessage(Handler.java:95)
at android.os.Looper.loop(Looper.java:154)
at android.app.ActivityThread.main(ActivityThread.java:6077)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:865)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:755)
 
Upvote 0

paul allen

Member
Licensed User
Longtime User
First Module
Dim a(300) As String
Dim b(300) As String
Dim c(300) As String
Dim d(300) As String
Second Module
Dim a(300) As String
Dim b(300) As String
Dim c(300) As String
Dim z(300) As String
Third Module
Dim a(200) As String

not all arrays are full at any time but the do reload 6-8 times before the error.
 
Upvote 0

paul allen

Member
Licensed User
Longtime User
Sub parser_EndElement (Url As String, Name As String, Text As StringBuilder)

If Name="a" Then
n = n + 1
a(n) = Text
End If

If Name="t" Then
b(n) = Text
End If
If Name="s" Then z(n) = Text
If Name="f" Then c(n) = Text
'Log(a(n))
'Log(z(n))
m.Clear
End Sub

all arrays are loaded via xml file downloaded.
size varies from 5 items to 120 item per load.

xml file:
<?xml version="1.0" encoding="ISO-8859-1"?>
<menu>
<c>
<t></t>
<f></f>
<s>l</s>
<a></a>
</c>
</menu>
 
Upvote 0

agraham

Expert
Licensed User
Longtime User
Please use code tags when posting code - available in the new forum from the right hand three vertical dots dropdown in the post editor.

Here is nothing obvious in that code and the array use appears quite modest so there is something in the overall structure of your code that is causing the overuse or retention of memory.
 
Upvote 0

paul allen

Member
Licensed User
Longtime User
i've just noticed this error before others:
i am also loading images each time i reload modules.

Downsampling image due to lack of memory.
Downsampling image due to lack of memory: 2
Downsampling image due to lack of memory.
Downsampling image due to lack of memory: 2
Downsampling image due to lack of memory.
Downsampling image due to lack of memory: 2
java.lang.reflect.InvocationTargetException
at java.lang.reflect.Method.invoke(Native Method)
at anywheresoftware.b4a.DynamicBuilder.build(DynamicBuilder.java:38)
at anywheresoftware.b4a.keywords.LayoutBuilder.loadLayoutHelper(LayoutBuilder.java:349)
at anywheresoftware.b4a.keywords.LayoutBuilder.loadLayoutHelper(LayoutBuilder.java:453)
at anywheresoftware.b4a.keywords.LayoutBuilder.loadLayout(LayoutBuilder.java:148)
at anywheresoftware.b4a.objects.PanelWrapper.LoadLayout(PanelWrapper.java:134)
at us.videoservice.ctv2.shows._builditems(shows.java:436)
at us.videoservice.ctv2.shows._jobdone(shows.java:550)
at java.lang.reflect.Method.invoke(Native Method)
at anywheresoftware.b4a.BA.raiseEvent2(BA.java:213)
at anywheresoftware.b4a.keywords.Common$11.run(Common.java:1178)
at android.os.Handler.handleCallback(Handler.java:751)
at android.os.Handler.dispatchMessage(Handler.java:95)
at android.os.Looper.loop(Looper.java:154)
at android.app.ActivityThread.main(ActivityThread.java:6077)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:865)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:755)
Caused by: java.lang.reflect.InvocationTargetException
at java.lang.reflect.Constructor.newInstance0(Native Method)
at java.lang.reflect.Constructor.newInstance(Constructor.java:430)
at anywheresoftware.b4a.objects.ViewWrapper.buildNativeView(ViewWrapper.java:572)
at anywheresoftware.b4a.objects.ButtonWrapper.build(ButtonWrapper.java:95)
... 18 more
 
Upvote 0

paul allen

Member
Licensed User
Longtime User
ok, i have narrowed it down to the fact that i am also download an image to put in the list each time i renew the list.
i have turned off: 'CallSubDelayed2(ImageDownloader, "Download", m), and no errors.
is there a buffer i need to clear each time to release the memory?
thanks. Paul
 
Upvote 0

paul allen

Member
Licensed User
Longtime User
Routline to get jpg from web
---------------------------------------------------------
Sub BuildItems
'If links.Size = 0 Then Return
'clv.Clear
Dim m As Map
m.Initialize
For x = 1 To n
Dim p As Panel
p.Initialize("")
clv.Add(p, 60dip, 4)
p.LoadLayout("ListItem")
Label1.Text = a(x)
Button1.Text = x

m.Put(ImageView1, "http://www.videoservic.com/ctv2/icons/" & b(x) & ".jpg")
Next
CallSubDelayed2(ImageDownloader, "Download", m)
End Sub
-----------------------------------------------------------------------
routine to get load jpg file

#Region Service Attributes
#StartAtBoot: False
#End Region

Sub Process_Globals
Private cache As Map
Private tasks As Map
Private ongoingTasks As Map
End Sub

Sub Service_Create
tasks.Initialize
cache.Initialize
ongoingTasks.Initialize
End Sub

Sub Service_Start (StartingIntent As Intent)

End Sub

Sub Service_Destroy

End Sub

Sub Download (ImageViewsMap As Map)
For i = 0 To ImageViewsMap.Size - 1
tasks.Put(ImageViewsMap.GetKeyAt(i), ImageViewsMap.GetValueAt(i))
Dim link As String = ImageViewsMap.GetValueAt(i)
If cache.ContainsKey(link) Then
Dim iv As ImageView = ImageViewsMap.GetKeyAt(i)
iv.SetBackgroundImage(cache.Get(link))
Else If ongoingTasks.ContainsKey(link) = False Then
ongoingTasks.Put(link, "")
Dim j As HttpJob
j.Initialize(link, Me)
j.Download(link)
End If
Next
End Sub

Sub JobDone(Job As HttpJob)
ongoingTasks.Remove(Job.JobName)
If Job.Success Then
Dim bmp As Bitmap = Job.GetBitmap
cache.Put(Job.JobName, bmp)
If tasks.IsInitialized Then
For i = 0 To tasks.Size - 1
Dim link As String = tasks.GetValueAt(i)
If link = Job.JobName Then
Dim iv As ImageView = tasks.GetKeyAt(i)
iv.SetBackgroundImage(bmp)
End If
Next
End If
Else
Log("Error downloading image: " & Job.JobName & CRLF & Job.ErrorMessage)
End If
Job.Release
End Sub
Sub ActivityIsPaused
tasks.Clear
End Sub

--------------------------------------------------------------
 
Upvote 0

paul allen

Member
Licensed User
Longtime User
Found it problem FYI:

Job.Release
cache.Clear <----------- added this to end of job.
thanks for trying to help
Paul
 
Upvote 0
Top