B4J Question keyvaluestore._getobjectinternal Java Heap Space Error

TorontoJim

Member
Licensed User
Longtime User
I'm still having an issue with a program closing when when it tries to open a data file, in release mode.

Using Erel's code, this is the output of the error, but it doesn't happen when I run it in debug mode.

The error looks like there is an issue grabbing an object through KVS, but that should be the same whether it's in release or debug mode.

I haven't tried to increase the memory available, yet, because I'm tryiung to understand WHY this is happening. The data file it's reading is only 22Kb in size.

What condition(s) would cause a problem with keyvaluestore._getobjectinternal ?



B4X:
keyvaluestore._getobjectinternal (java line: 202)
java.lang.OutOfMemoryError: Java heap space
    at anywheresoftware.b4a.randomaccessfile.RandomAccessFile.readHelper(RandomAccessFile.java:382)
    at anywheresoftware.b4a.randomaccessfile.RandomAccessFile.ReadObject(RandomAccessFile.java:369)
    at b4j.example.keyvaluestore._getobjectinternal(keyvaluestore.java:202)
    at b4j.example.keyvaluestore._getobject(keyvaluestore.java:168)
    at b4j.example.main._loadkvsdata(main.java:625)
    at b4j.example.main._combofilefield_selectedindexchanged(main.java:396)
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source)
    at java.lang.reflect.Method.invoke(Unknown Source)
    at anywheresoftware.b4a.BA.raiseEvent2(BA.java:93)
    at anywheresoftware.b4a.BA$2.run(BA.java:165)
    at com.sun.javafx.application.PlatformImpl.lambda$null$173(Unknown Source)
    at com.sun.javafx.application.PlatformImpl$$Lambda$50/24760006.run(Unknown Source)
    at java.security.AccessController.doPrivileged(Native Method)
    at com.sun.javafx.application.PlatformImpl.lambda$runLater$174(Unknown Source)
    at com.sun.javafx.application.PlatformImpl$$Lambda$49/9599617.run(Unknown Source)
    at com.sun.glass.ui.InvokeLaterDispatcher$Future.run(Unknown Source)
    at com.sun.glass.ui.win.WinApplication._runLoop(Native Method)
    at com.sun.glass.ui.win.WinApplication.lambda$null$148(Unknown Source)
    at com.sun.glass.ui.win.WinApplication$$Lambda$38/20085625.run(Unknown Source)
    at java.lang.Thread.run(Unknown Source)

This is the subroutine that is executing when the error occurs:

B4X:
Sub LoadKVSData
    tableDataView.Items.Clear
    Dim listKeys As List
    listKeys.Initialize
    listKeys = kvs.ListKeys
    For i = 0 To listKeys.Size - 1
        Dim Row(2) As Object
        Row(0) = listKeys.Get(i)
        Dim strKeyName As String = listKeys.Get(i)
        strKeyName = strKeyName.ToLowerCase
        If txtPassword.Text <> "" Then
            Try
                Row(1) = kvs.GetEncryptedObject(listKeys.Get(i), txtPassword.Text)
            Catch
                If strKeyName.Contains(".jpg") = True Or strKeyName.Contains(".jpeg") = True Or strKeyName.Contains(".gif") = True Or strKeyName.Contains(".png") = True Or strKeyName.Contains(".bmp") = True Then
                    Dim im As ImageView
                    im.Initialize("im")
                    im.SetImage(kvs.GetBitmap(listKeys.Get(i)))
                    Row(1) = im
               Else
                    Try
                        Row(1) = kvs.GetObject(listKeys.Get(i))
                    Catch
                        Row(1) = kvs.GetSimple(listKeys.Get(i))
                    End Try
                End If
            End Try
        Else
            If strKeyName.Contains(".jpg") = True Or strKeyName.Contains(".jpeg") = True Or strKeyName.Contains(".gif") = True Or strKeyName.Contains(".png") = True Or strKeyName.Contains(".bmp") = True Then
                Dim im As ImageView
                im.Initialize("im")
                im.SetImage(kvs.GetBitmap(listKeys.Get(i)))
                Row(1) = im
           Else
                Try
                    Row(1) = kvs.GetObject(listKeys.Get(i))
                Catch
                    Row(1) = kvs.GetSimple(listKeys.Get(i))
                End Try
            End If
        End If
        tableDataView.Items.Add(Row)
    Next
    refreshTableView
End Sub

Sub refreshTableView
    Dim tempData As List
    tempData.Initialize
    tempData.AddAll(tableDataView.Items)
    tableDataView.Items.Clear
    For i = 0 To tempData.Size - 1
        Dim r() As Object = tempData.Get(i)
        tableDataView.Items.Add(Array As Object(r(0), r(1)))
    Next
    tableDataView.SelectedRow = 0
End Sub

Just to clarify, when I say in "relase mode", I mean trying to run the jar directly after having run the IDE in release mode to create the jar. Running it from the IDE, the error doesn't occur.
 

TorontoJim

Member
Licensed User
Longtime User
In the KVS module GEtObjectInternal, it is failing at:

B4X:
res = raf.ReadObject(raf.CurrentPosition)

That's based on the Log statements. I've figured out that it's attempting the get object on a string value. So, okay, I can see that raising an exception. It's not actually a problem with that statement, it's a problem with how I'm trying to bend it to my will, and not suceeding.

Obviously the Try/Catch doesn't work in this situation. So I added the following to my code:

B4X:
                Dim rs As ResultSet
                rs = kvs.sql1.ExecQuery2("SELECT typeof(value) FROM main WHERE key = ?", Array As String(listKeys.Get(i)))
                If rs.GetString(rs.GetColumnName(0)) = "text" Then
                    Row(1) = kvs.GetSimple(listKeys.Get(i))
                Else
                    Row(1) = kvs.GetObject(listKeys.Get(i))
                End If

The program no longer exits, and the table loads lightening fast.

Now I'm going back to square one, and do the typeof() testing where I need to so that I can correct this programmer error whereever I need to.
 
Upvote 0
Top