Android Question Failed to allocate memory error

sconlon

Active Member
Licensed User
Longtime User
I am downloading a very large (93k lines) webpage (using Erel's modified DownLoadService ) and during processing it I get the following error

"java.lang.OutOfMemoryError: Failed to allocate a 4722424 byte allocation with 3343520 free bytes and 3265KB until OOM, target footprint 536870912, growth limit 536870912".

In the manifest I have included the line

B4X:
SetApplicationAttribute(android:largeHeap,"true")

The processing of the webpage involves searching for specified stings and displaying them. The error does not occur at the same place each time but apparently randomly.
 

sconlon

Active Member
Licensed User
Longtime User
The full error message is :

java.lang.OutOfMemoryError: Failed to allocate a 4722408 byte allocation with 3388440 free bytes and 3309KB until OOM, target footprint 536870912, growth limit 536870912
at java.lang.String.fastSubstring(Native Method)
at java.lang.String.substring(String.java:2030)
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.BA$2.run(BA.java:370)
at android.os.Handler.handleCallback(Handler.java:883)
at android.os.Handler.dispatchMessage(Handler.java:100)
at android.os.Looper.loop(Looper.java:237)
at android.app.ActivityThread.main(ActivityThread.java:7857)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:493)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1076)


And the processing code is :

B4X:
                content=Job.GetString
                Job.Release
                content = content.SubString(content.IndexOf("Showing all"))
                Do While content.Contains("picknumber")
                    str = "<h4>"
                    content = content.SubString(content.IndexOf("<h4>") + str.Length)
                    dmDate = content.substring2(0,content.IndexOf("</"))
                    For cnt = 0 To 6
                        content = content.SubString(content.IndexOf("picknumber"))
                        content = content.SubString(content.IndexOf("value=""") + 7)
                        pos1 = content.IndexOf("""")
                        nums(cnt) = content.substring2(0,pos1)
                        content = content.SubString(pos1)
                    Next
                    strsql = "INSERT INTO results VALUES ('" & draw & "','" & dmDate & "','2','" & nums(0) & "','" & nums(1) & "','" & nums(2) & "','" & nums(3) & "','" & nums(4) & "','" & nums(5) & "','" & nums(6) & "')"
                    sqlLotto.ExecNonQuery(strsql)
                    str = "picknumber"
                    content = content.SubString(content.IndexOf("picknumber"))
                    For cnt = 0 To 6
                        content = content.SubString(content.IndexOf("picknumber"))
                        content = content.SubString(content.IndexOf("value=""") + 7)
                        pos1 = content.IndexOf("""")
                        nums(cnt) = content.substring2(0,pos1)
                        content = content.SubString(pos1)
                    Next
                    strsql = "INSERT INTO results VALUES ('" & draw & "','" & dmDate & "','2','" & nums(0) & "','" & nums(1) & "','" & nums(2) & "','" & nums(3) & "','" & nums(4) & "','" & nums(5) & "','" & nums(6) & "')"
                    sqlLotto.ExecNonQuery(strsql)
                    draw = draw + 1
                    Log("Draw: " & draw & " on " & dmDate)
                Loop

I know the code is not very efficient, it needs reviewing! In this case the error happened on draw = 2
 
Upvote 0

Erel

B4X founder
Staff member
Licensed User
Longtime User
strsql = "INSERT INTO results VALUES ('" & draw & "','" & dmDate & "','2','" & nums(0) & "','" & nums(1) & "','" & nums(2) & "','" & nums(3) & "','" & nums(4) & "','" & nums(5) & "','" & nums(6) & "')"
This falls under the "very bad code": [B4X] "Code Smells" - common mistakes and other tips
Making many inserts without creating a transaction or better using batch insert is also a big mistake.

You don't really all these substrings. Instead keep a running variable that stores the index of the lower limit and use IndexOf2.
 
Upvote 0

sconlon

Active Member
Licensed User
Longtime User
So I first commented out the writing to the database and got the same error. So I guess the error relates to the string searching.
 
Upvote 0
Top