Android Question java.lang.OutOfMemoryError

Sofiya

Member
Licensed User
i facing java.lang.OutOfMemoryError in my page activity, while i am running the project i got this error, what are the reason behind this error

B4X:
billing$ResumableSub_job4resume (java line: 1362)
java.lang.OutOfMemoryError
    at java.lang.AbstractStringBuilder.enlargeBuffer(AbstractStringBuilder.java:95)
    at java.lang.AbstractStringBuilder.append0(AbstractStringBuilder.java:115)
    at java.lang.StringBuilder.append(StringBuilder.java:250)
    at anywheresoftware.b4a.objects.streams.File$TextReaderWrapper.ReadAll(File.java:590)
    at anywheresoftware.b4a.samples.httputils2.httpjob._getstring2(httpjob.java:220)
    at anywheresoftware.b4a.samples.httputils2.httpjob._getstring(httpjob.java:207)
    at b4a.example.billing$ResumableSub_job4.resume(billing.java:1362)
    at anywheresoftware.b4a.BA.checkAndRunWaitForEvent(BA.java:240)
    at anywheresoftware.b4a.BA.raiseEvent2(BA.java:180)
    at anywheresoftware.b4a.keywords.Common$11.run(Common.java:1151)
    at android.os.Handler.handleCallback(Handler.java:587)
    at android.os.Handler.dispatchMessage(Handler.java:92)
    at android.os.Looper.loop(Looper.java:123)
    at android.app.ActivityThread.main(ActivityThread.java:3647)
    at java.lang.reflect.Method.invokeNative(Native Method)
    at java.lang.reflect.Method.invoke(Method.java:507)
    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:839)
 

Sofiya

Member
Licensed User
B4X:
sub job4()
'Log("Entering job1")
   Dim http4 As HttpJob
    http4.Initialize("Billing",Me)
    http4.Download("http://192.168.1.4/posapi/result/getresultdata")
   wait for (http4) JobDone(http4 As HttpJob)
    If http4.Success Then
        Select http4.JobName
            Case "Billing"
'                Log(job1.GetString)
                Dim parser As JSONParser
                Dim response As String = http4.GetString
                parser.Initialize(response)
'                Log(response)
                valuesdata.Initialize
                valuesdata2.Initialize
'                Dim rows1 As List
                Dim str As String
                Dim val As Int
                rows.Initialize
                rows = parser.NextArray
                str = rows.Size
                val = str
                For i = 0 To val - 1
'                    Dim m1 As Map
                    m = rows.Get(i)
                    If Table.code = m.Get("poscode") Then
                        valuesdata.Add(m.Get("ItemName"))
                        valuesdata2.Add(m.Get("Itemcode"))
                        ACEdtCode.SetItems(valuesdata2)
                        ACEdtItem.SetItems(valuesdata)
                    End If
                Next
        End Select
   End If
'   Log("Exiting job1")
End Sub

while my page load above coding which feed the data from recordset to grid i facing
java.lang.OutOfMemoryError
 
Upvote 0

Sofiya

Member
Licensed User
i getting back the data from the backend SQL Server through API C#, i attach api code to retrieve date from database given below

B4X:
 public class ResultController : Controller
    {
        // GET: Outlet

        public ActionResult Index()
        {
            return View();
        }
        private List<Result> GetResult()
        {
            var resultList = new List<Result>();

            string cs = ConfigurationManager.ConnectionStrings["Model6"].ConnectionString;
            using (SqlConnection con = new SqlConnection(cs))
            {
                SqlCommand cmd = new SqlCommand();
                cmd.Connection = con;
                cmd.CommandType = CommandType.StoredProcedure;
                cmd.CommandText = "DKTresultProc";
                con.Open();

                SqlDataReader rdr = cmd.ExecuteReader();
                while (rdr.Read())
                {
                    Result r = new Result();
                    r.ItemPckey = rdr.GetInt32(rdr.GetOrdinal("ItemPckey"));
                    r.Itemcode = rdr["ItemCode"].ToString();
                    r.ItemName = rdr["ItemName"].ToString();
                    r.Rate = Convert.ToDecimal(rdr["Rate"]);
                    r.poscode = Convert.ToInt32(rdr["poscode"]);
                    r.itemcat = rdr["ItemCategory"].ToString();
                    resultList.Add(r);
                }
            }
            return resultList;
        }
        public JsonResult GetResultData()
        {
            var rs = GetResult();
            return Json(rs, JsonRequestBehavior.AllowGet);
        }
    }



And B4A code which retrieve the data from api and further procedure given below

B4X:
    #Region  Activity Attributes
    #FullScreen: False
    #IncludeTitle: True
#End Region

Sub Process_Globals
    'These global variables will be declared once when the application starts.
    'These variables can be accessed from all modules.
    
End Sub

Sub Globals
    'These global variables will be redeclared each time the activity is created.
    'These variables can only be accessed from this module.
    
    Private EdtTableNo As EditText
    Private EdtKOT As EditText
    Private EdtSession As EditText
    Private EdtPax As EditText
    Private BtnKot As Button
    Private BtnBill As Button
    Private ACEdtCode As AutoCompleteEditText
    Private ACEdtItem As AutoCompleteEditText
    Private BtnDown As Button
    Private BtnTrnsfr As Button
    Private BtnDisBill As Button
    Private EdtRate As EditText
    Private EdtQty As EditText
    Private Pnl As Panel
'    Dim nomi() As String
'    Private LV As ListView
    Private Label1 As Label
    Private Label2 As Label
    Private Label5 As Label
    Private Label6 As Label
    Private Label7 As Label
    Private Label8 As Label
    Private Label4 As Label
    Private Label3 As Label
    Dim Grid As xnGrid
    Dim k As Double
    Dim valuesdata, valuesdata2 As List
    Dim rows As List
    Dim m As Map
'    Dim print As wifiprint
    Private BtnDlt As Button
'    Private HorizontalScrollView1 As HorizontalScrollView
'    Dim pdf As PdfDocument
'    Dim print As Printer
End Sub

Sub Activity_Create(FirstTime As Boolean)
    'Do not forget to load the layout file created with the visual designer. For example:
    Activity.LoadLayout("billing")
    Activity.Title = "POS-KOT & Billing"
    m.Initialize
'    print.Initialize("")
'    Print1
'    pdf.Initialize
'    pdf.Initialize("KOTPDF", b)
'    If print.PrintSupported Then
'        print.PrintPdf("Billing", File.DirDefaultExternal, "print_job.pdf")
'    End If

'    Pnl.Color = Colors.RGB(147,112,219)
'    print.initialize

'    Pnl.Top = Activity.Height - Pnl.Height

    Pnl.Height = 100%y
    Pnl.Width = 100%x

    Dim job_1, job_2, job_3 As HttpJob
    job_1.Initialize("Billing", Me)
    job_2.Initialize("job_4", Me)
    job_3.Initialize("job_3", Me)
    job_1.Download("http://192.168.1.4/posapi/result/getresultdata")
    job_2.PostString("http://192.168.1.4/posapi/session/getsessiondata","close=false")
    job_3.PostString("http://192.168.1.4/posapi/kotno/updatedata", "kotno=12, posarea=30")

    Dim t As Int : t = 5dip
    Dim h As Int : h = 45dip

    t = t + h

    If Grid.IsInitialized = False Then
        
        Grid.Initialize("Grid")
        Grid.RowHeight = 25dip
  
        Grid.HeaderHeight = 33dip
  
        Grid.HeaderTextSize = 12
        Grid.RowTextSize = 12
  
        Grid.HeaderTextColor = Colors.Black
        Grid.RowTextColor = Colors.Black
        
        Grid.HeaderTypeface = Typeface.DEFAULT_BOLD
        
        Grid.RowEvenColor = Colors.Transparent
        Grid.RowOddColor = Colors.Transparent
        
        Grid.HeaderColor = Colors.Transparent
        Grid.SelectedEvenColor = Colors.Blue
        Grid.SelectedOddColor = Colors.Blue

        Dim cc(6) As xnGridCol

        cc(0).Initialize2 ("Item Code", ACEdtCode.Text, 50dip, Gravity.TOP + Gravity.LEFT)
        Grid.ColAppend (cc(0))

        cc(1).Initialize2 ("Item Name", ACEdtItem.Text, 105dip, Gravity.TOP + Gravity.CENTER_HORIZONTAL)
        Grid.ColAppend (cc(1))

        cc(2).Initialize2 ("Qty.", EdtQty.Text , 50dip , Gravity.TOP + Gravity.CENTER_HORIZONTAL)
        Grid.ColAppend (cc(2))

        cc(3).Initialize2 ("Rate", EdtRate.Text, 50dip, Gravity.TOP + Gravity.CENTER_HORIZONTAL)
        Grid.ColAppend (cc(3))
        
        cc(4).Initialize2 ("Amt.", "", 50dip, Gravity.TOP + Gravity.CENTER_HORIZONTAL)
        Grid.ColAppend (cc(4))
        
        cc(5).Initialize2 ("Item Category", m.Get("itemcat"), 105dip, Gravity.TOP + Gravity.CENTER_HORIZONTAL)
        Grid.ColAppend (cc(5))

        Pnl.AddView(Grid, 6dip, 185dip, 310dip, 130dip)
'        HorizontalScrollView1.Panel.AddView(Grid, 6dip, 185dip, 310dip, 130dip)
'        HorizontalScrollView1.FullScroll(True)

'        Grid_fill
'        bt_paint_click
        Grid.Multiselect = False
        Grid.GridWidth = 1dip
        Grid.GridColor = Colors.Black
        Grid.PixelFix = False
        Grid.Color = Colors.LightGray

        Dim t0 As Long
        Dim t1 As Long
        Dim dd As Long

        t0 = DateTime.Now
        Grid.GridCreate2(False)
        t1 = DateTime.Now

        dd = t1 - t0
        Log (dd)
    End If
    
    ACEdtCode.TextSize = 14
    ACEdtItem.TextSize = 14
    EdtQty.TextSize = 14
    EdtRate.TextSize = 14

    EdtTableNo.InputType = EdtTableNo.INPUT_TYPE_NUMBERS
    EdtTableNo.TextColor = Colors.Black
    EdtKOT.InputType = EdtKOT.INPUT_TYPE_NUMBERS
    EdtKOT.TextColor = Colors.Black
    EdtPax.InputType = EdtPax.INPUT_TYPE_NUMBERS
    EdtPax.TextColor = Colors.Black
    EdtSession.TextColor = Colors.Black
    EdtQty.InputType = EdtQty.INPUT_TYPE_NUMBERS
    ACEdtCode.InputType = ACEdtCode.INPUT_TYPE_TEXT
'    ACEdtCode.TextColor = Colors.Black
    ACEdtItem.InputType = ACEdtItem.INPUT_TYPE_TEXT
'    ACEdtItem.textColor = Colors.Black
    EdtQty.Textcolor = Colors.Black
    EdtRate.TextColor = Colors.Black
    EdtTableNo.Text= Table.tablno1
    Label1.TextColor = Colors.Black  'Label1.Gravity = Gravity.CENTER_HORIZONTAL
    Label2.TextColor = Colors.Black  'Label2.Gravity = Gravity.CENTER_HORIZONTAL
    Label3.TextColor = Colors.Black  'Label3.Gravity = Gravity.CENTER_HORIZONTAL
    Label4.TextColor = Colors.Black  'Label4.Gravity = Gravity.CENTER_HORIZONTAL
    Label5.TextColor = Colors.Black  'Label5.Gravity = Gravity.CENTER_HORIZONTAL
    Label6.TextColor = Colors.Black  'Label6.Gravity = Gravity.CENTER_HORIZONTAL
    Label7.TextColor = Colors.Black  'Label7.Gravity = Gravity.CENTER_HORIZONTAL
    Label8.TextColor = Colors.Black  'Label8.Gravity = Gravity.CENTER_HORIZONTAL
End Sub

'Sub Print1
'    print.PrintHtml("job", $"<b>Hello world!!!</b><br/>
'<h1>second line</h1>
'<img src="${WebViewAssetFile("smiley.png")}"/>"$)
'End Sub
'
'Sub WebViewAssetFile (FileName As String) As String
'    Dim jo As JavaObject
'    jo.InitializeStatic("anywheresoftware.b4a.objects.streams.File")
'    If jo.GetField("virtualAssetsFolder") = Null Then
'        Return "file:///android_asset/" & FileName.ToLowerCase
'    Else
'        Return "file://" & File.Combine(jo.GetField("virtualAssetsFolder"), _
'  jo.RunMethod("getUnpackedVirtualAssetFile", Array As Object(FileName)))
'    End If
'End Sub

Sub Activity_Resume
    
End Sub

Sub Activity_Pause (UserClosed As Boolean)

End Sub

Sub BtnDisBill_Click
    StartActivity(DiscountBill)
    Activity.Finish
End Sub

Sub BtnBill_Click
'    pdf.StartPage(595, 842) 'A4 size
'    pdf.Canvas.DrawLine(2, 2, 593 , 840, Colors.Blue, 4)
'    pdf.Canvas.DrawText("Hello", 100, 100, Typeface.DEFAULT_BOLD, 30 / GetDeviceLayoutValues.Scale , Colors.Yellow, "CENTER")
'    pdf.FinishPage
'    Dim out As OutputStream = File.OpenOutput(File.DirRootExternal, "1.pdf", False)
'    pdf.WriteToStream(out)
'    out.Close
'    pdf.Close
End Sub

Sub BtnKot_Click
    Dim job_5 As HttpJob
    job_5.Initialize("job", Me)
'    job_5.PostString("http://192.168.1.4/posapi/insertKot/addkot",JSON)
        
    Dim k1 As Double
    Dim ic As String
    ic = m.Get("itemcat")
    For i = 0 To Grid.RowCount - 1
        job_5.Download2("http://192.168.1.4/posapi/insertKot/addkot", _
        Array As String("KoTNo",EdtKOT.Text,"itemcode",Grid.GetValue(i,0),"itemcategory",ic,"unqno",2,"reason","vcsdawre","rate",Grid.GetValue(i,3),"amount",Grid.GetValue(i,4),"quantity",Grid.GetValue(i,2),"taxrate",0,"preferencepckey",123,"kotdate",DateTime.Now,"status","A","itempckey",1234))
    Next
    k = EdtKOT.Text
    k1 = k + 1
    
    Wait For (job_5) JobDone(job_5 As HttpJob)
    If job_5.Success Then
        Log(job_5.GetString)
        ' = job_5.GetString
    Else
        ' = job_5.ErrorMessage
        ToastMessageShow(job_5.ErrorMessage,True)
        Return
    End If
    job_5.Release
    
    Dim http3 As HttpJob
    http3.Initialize("Billing",Me)
'    http3.PostString("http://192.168.1.4/posapi/kotno/updatedata","KotNo=k & posarea=Table.code")
    http3.Download2("http://192.168.1.4/posapi/kotno/updatedata", _
    Array As String("KotNo=" & k1, "posarea=" & Table.code))
    
    EdtTableNo.Text = ""
    EdtKOT.Text = ""
    EdtPax.Text = ""
    
    EdtKOT.Text = k1
'    Log(k1)
'    Log(EdtKOT.Text)
End Sub

Sub ACEdtItem_ItemClick (Value As String)
    For i = 0 To rows.Size - 1
'        Dim m As Map
        m = rows.Get(i)
        If ACEdtItem.text = m.Get("ItemName") And Table.code = m.Get("poscode") Then
            ACEdtCode.Text = m.Get("Itemcode")
            EdtRate.Text = m.Get("Rate")
            EdtQty.RequestFocus
        End If
    Next
    Log(m.Get("Rate"))
End Sub

Sub ACEdtCode_ItemClick (Value As String)
    For i = 0 To rows.Size - 1
        m = rows.Get(i)
        If ACEdtCode.text = m.Get("Itemcode") And Table.code = m.Get("poscode") Then
            ACEdtItem.Text = m.Get("ItemName")
            EdtRate.Text = m.Get("Rate")
            EdtQty.RequestFocus
        End If
    Next
    Log(m.Get("Rate"))
End Sub

Sub job1()
    '   Log("Entering job3")
    Dim http1 As HttpJob
    Dim d As Double
    d = Table.code
    http1.Initialize("Billing",Me)
    http1.PostString("http://192.168.1.4/posapi/kot/selectdata","posarea=" & d)
'    http1.Download2("http://192.168.1.4/posapi/kot/selectdata", _
'    Array As String("posarea=", val))
    wait for (http1) JobDone(http1 As HttpJob)
    If http1.Success Then
        Select http1.JobName
            Case "Billing"
'                Log(job1.GetString)
                Dim parser1 As JSONParser
                Dim response1 As String = http1.GetString
                parser1.Initialize(response1)
'                Log(response2)
                Dim rows1 As List
                rows1.Initialize
                rows1 = parser1.NextArray
                For i = 0 To rows1.Size - 1
                    Dim m1 As Map
                    m1 = rows1.Get(i)
                    If Table.code = m1.Get("posarea") Then
                        EdtKOT.Text = m1.Get("KotNo")
                    Log(m1)
                    End If
                Next
        End Select
        Else
        Log("Error:" & http1.ErrorMessage)
    End If
'    Log("Exiting job3")
End Sub

Sub job2()
'   Log("Entering job2")
   Dim http2 As HttpJob
    http2.Initialize("Billing",Me)
   http2.PostString("http://192.168.1.4/posapi/session/getsessiondata","closed=false")
   wait for (http2) JobDone(http2 As HttpJob)
    If http2.Success Then
        Select http2.JobName
            Case "Billing"
'                Log(job1.GetString)
                Dim parser1 As JSONParser
                Dim response1 As String = http2.GetString
                parser1.Initialize(response1)
'                Log(response1)
                Dim rows2 As List
                rows2.Initialize
                rows2 = parser1.NextArray
                For i = 0 To rows2.Size - 1
                    Dim m3 As Map
                    m3 = rows2.Get(i)
                    If Table.close = m3.Get("closed") Then
                        EdtSession.Text = m3.Get("SessionName")
'                        Log(m.Get("SessionName"))
                    End If
                Next
        End Select
   End If
'    Log("Exiting job2")
End Sub

'Sub job3()
'    '   Log("Entering job3")
'    Dim http3 As HttpJob
'    http3.Initialize("Billing",Me)
''    http3.PostString("http://192.168.1.4/posapi/kotno/updatedata","KotNo=k, posarea=Table.code")
'    http3.Download2("http://192.168.1.4/posapi/kotno/updatedata", _
'    Array As String("KotNo=", k, "posarea=", Table.code))
'    wait for (http3) JobDone(http3 As HttpJob)
'    If http3.Success Then
'        Select http3.JobName
'            Case "Billing"
''                Log(job1.GetString)
'                Dim parser2 As JSONParser
'                Dim response2 As String = http3.GetString
'                parser2.Initialize(response2)
''                Log(response2)
'                Dim rows3 As List
'                rows3.Initialize
'                rows3 = parser2.NextArray
'                For i = 0 To rows3.Size - 1
'                    Dim m3 As Map
'                    m3 = rows3.Get(i)
'                    EdtKOT.Text = m3.Get("KotNo")
''                    k = EdtKOT.Text
'                    k = k + 1
''                    Log(m3.Get("KotNo"))
'                Next
'        End Select
'    End If
''    Log("Exiting job3")
'End Sub

Sub job4()                                                                             ' this is the sub which gets the retrieved data
'Log("Entering job1")
   Dim http4 As HttpJob
    http4.Initialize("Billing",Me)
    http4.Download("http://192.168.1.4/posapi/result/getresultdata")
   wait for (http4) JobDone(http4 As HttpJob)
    If http4.Success Then
        Select http4.JobName
            Case "Billing"
'                Log(job1.GetString)
                Dim parser As JSONParser
                Dim response As String = http4.GetString
                parser.Initialize(response)
'                Log(response)
                valuesdata.Initialize
                valuesdata2.Initialize
'                Dim rows1 As List
                Dim str As String
                Dim val As Int
                rows.Initialize
                rows = parser.NextArray
                str = rows.Size
                val = str
                For i = 0 To val - 1                                         ' this part loaded a retrieved value which is comming from Database
'                    Dim m1 As Map
                    m = rows.Get(i)
                    If Table.code = m.Get("poscode") Then
                        valuesdata.Add(m.Get("ItemName"))
                        valuesdata2.Add(m.Get("Itemcode"))
                        ACEdtCode.SetItems(valuesdata2)
                        ACEdtItem.SetItems(valuesdata)
                    End If
                Next
        End Select
   End If
'   Log("Exiting job1")
End Sub

Sub JobDone (job_6 As HttpJob)
    job1
    job2
'    job3
    job4
    job_6.Release
End Sub

Sub BtnDown_Click
    If ACEdtCode.Text = "" Or ACEdtItem.Text = "" Or EdtQty.Text = "" Or EdtRate.Text = "" Then
        Msgbox("You have to enter all fields","Missed data field")
    Else
        Grid.RowAppend(Array As String(ACEdtCode.Text, ACEdtItem.Text, EdtQty.Text, EdtRate.Text, (EdtRate.Text) * (EdtQty.Text), m.Get("itemcat")))

        ACEdtCode.Text = ""
        ACEdtItem.Text = ""
        EdtQty.Text = ""
        ACEdtCode.RequestFocus
        EdtRate.Text = ""
    End If

End Sub

Sub BtnTrnsfr_Click
    StartActivity(TableTransfer)
'    Activity.Finish
End Sub

Sub BtnDlt_Click
    Msgbox2Async("Do you want to Delete this row?", "Warning", "Yes", "", "No", Null, False)
    
   Wait For MsgBox_Result (Result As Int)
    If Result = DialogResponse.POSITIVE Then
        Grid.RowDelete(Grid.RowSelected)
    ''    Return False
    Else
    ''    Return True
    End If
End Sub
 
Upvote 0

DonManfred

Expert
Licensed User
Longtime User
i attach api code to retrieve date from database given below
It is NOT Relevant what you are using! You can use whatever you want....

Relevant is WHAT AMOUNT OF DATA YOU GET BACK (1kb, 1mb, 1GB)? How many dataset you fetch?

I ask this now the THIRD time and the third time i do not get any Useful answer from you.

Good luck, i´m out here. I´m not interested to help anymore. I can use my time with more useful things than trying to help you.
 
Upvote 0

MarkusR

Well-Known Member
Licensed User
Longtime User
you should Initialize this global lists once and use the .clear command
B4X:
valuesdata.Initialize
valuesdata2.Initialize
 
Upvote 0

kisoft

Well-Known Member
Licensed User
Longtime User
Add this to the manifest:
B4X:
SetApplicationAttribute(android:largeHeap,"true")
this will allocate more memory on the pile of your application.
 
Upvote 0
Cookies are required to use this site. You must accept them to continue using the site. Learn more…