Android Question android.database.CursorWindowAllocationException: Cursor window allocation of 2097152 bytes failed.

AHilberink

Active Member
Licensed User
Longtime User
Hi,

I got this exception on a 2Gb Android10 tablet and not on a 4Gb Android14. It is not always on the same function. I checked all Cursors are closed after use. What can I do to prevent this from happening or do I have to request tablets with 4Gb or more memory?

The complete message:
android.database.CursorWindowAllocationException: Cursor window allocation of 2097152 bytes failed. # Open Cursors=701 (# cursors opened by this proc=701)

One of the functions where it happens:
B4X:
Sub VullenContainers
    ContainerList.Initialize
    ContainerNaam.Initialize
    ContainerType.Initialize
    TarraList.Initialize
    AantalList.Initialize
    CheckList.Initialize
    ContainerList.Clear
    ContainerNaam.Clear
    ContainerType.Clear
    TarraList.Clear
    AantalList.Clear
    CheckList.Clear
        
    Try
        Dim SenderFilter As Object = Main.SQL1.ExecQueryAsync("SQL","SELECT Code, Type FROM "&Main.DBContainers&" ORDER BY Type, Code", Null)
        Wait For (SenderFilter) SQL_QueryComplete (Success As Boolean, cr As Cursor)
        If Success Then
            For i = 0 To cr.RowCount - 1
                cr.Position=i
                Dim cont As Cursor
                cont = Main.SQL1.ExecQuery("SELECT Omschrijving, Tarra FROM "&Main.DBContainers&" WHERE Type="&cr.GetString("Type")&$" AND Code=""$&cr.GetString("Code")&$"""$)
                cont.Position=0
                ContainerList.Add(cr.GetString("Code"))
                ContainerNaam.Add(cont.GetString("Omschrijving"))
                TarraList.Add(cont.GetString("Tarra"))
                ContainerType.Add(cr.GetString("Type"))
                AantalList.Add("")
                CheckList.Add(False)
                If(Hoofdscherm.Terug=True) Then
                    For x = 0 To Hoofdscherm.ConTerug.Size-1
                        If(Hoofdscherm.ConTerug.Get(x)=cr.GetString("Code")) Then
                            AantalList.Set(i,Hoofdscherm.AantalConTerug.Get(x))
                            CheckList.Set(i,True)
                            Exit
                        End If
                    Next
                Else
                    For x = 0 To Hoofdscherm.Containers.Size-1
                        If(Hoofdscherm.Containers.GetKeyAt(x)=cr.GetString("Code")) Then
                            AantalList.Set(i,Hoofdscherm.AantalContainer.Get(x))
                            CheckList.Set(i,True)
                            Exit
                        End If
                    Next
                End If
                cont.Close
            Next
        End If
    Catch   
        Starter.ZendError("module=VullenContainers&error=("&cr.RowCount&")"&LastException)
    End Try
    cr.Close
    LaadContainers("")
    EigenFuncties.ResetUserFontScale(Activity)
End Sub

This function loads 700 rows. As you can see I tried to split the dataload into 2 Queries.

I hope someone can advice me where to start.

Kind regards,
André
 

AHilberink

Active Member
Licensed User
Longtime User
The amount of data you can query in one select is limited to 2mb in Android.

change it to read less

Hi DonManfred,

Thanks for your reply. How can I count for 2Mb?

My 700 rows has only a string of 8 charaters and a integer of 4. That's why I split it to the ForNext to has a seperate Cursor for every row with the description (omschrijving in my source code). This way I tried to stay below the 2Mb per Cursor.

Is above thinking incorrect? Please let know how to split the query into smaller parts on another way.
Is this limit of 2mb for all versions? Why does my newer tablet works and the older one not if it is?

Kind regards,
André
 
Upvote 0

teddybear

Well-Known Member
Licensed User
Even though you split the dataload into 2 queries,it is still an associated query, the consumption is Cartesian Product.
Try to split the dataload into two individual queries.
Step 1. SELECT Code, Type into a conditional list, and then close the sql
Step2. SELECT Omschrijving, Tarra using the list as condition
 
Last edited:
Upvote 0
Top