Sathish Kumar J Sathish Kumar J - 4 months ago 17
Java Question

Throwing OutOfMemoryError Android

My Scenario is, I have some data's in Database

SQLite
. I can get data's Correctly by,

Cursor c0 = OCTDBHelper.myDataBase.rawQuery("SELECT DISTINCT ALIAS FROM ICT32 where ALIAS NOT NULL", null);
Log.i("IC-Count", "IC-Count : " + c0.getCount());
String arr[] =new String[c0.getCount()];

c0.moveToFirst();
for(int i = 0; i < c0.getCount(); i++)
{
arr[i] = c0.getString(0);
c0.moveToNext();

}


here i got all data's correctly in
arr
like,


arr[0] :abcb,hjhj,jkshdf,hdjf,ghsg,aghgh,

arr[1] : klkl,sdjk,jkji,iui,jiji,jij,

.
.
.

arr[n] : qw,rwer,re,tr,yty,uyu,rtr,ter,etr,yrt,ytr


ie
, each location has 10-30 data with
comma(,)
.
total arr.length is 1322


after getting that data i need to do some operation with those data as follow as,


  • I need to
    split(",")
    based on
    comma(,)
    and add into Single
    List
    until array reach
    n'th
    position.

  • Finally my output should be,


    list[0] : abcb

    list[1] : hjhj

    list[2] : jkshdf

    ...

    list[n-1] : yrt

    list[n] : ytr



I think already did correctly. but it gives error like
Throwing OutOfMemoryError


My code is,

Cursor c0 = OCTDBHelper.myDataBase.rawQuery("SELECT DISTINCT ALIAS FROM ICT32 where ALIAS NOT NULL", null);

Log.i("IC-Count", "IC-Count : " + c0.getCount());
ArrayList<String> tmp = new ArrayList<>();
c0.moveToFirst();
for(int i = 0; i < c0.getCount(); i++)
{
String arr[] = c0.getString(0).split(",");
for(int j= 0; j< arr.length;i++)
{
if(!arr[j].equals(","))
{
tmp.add(arr[j]);
}
}
c0.moveToNext();

}

Log.i("Alias" , "count : " +tmp.size());


this code gives
Throwing OutOfMemoryError



Logcat


FATAL EXCEPTION: main
Process: estec.android.mvi32, PID: 11005
java.lang.OutOfMemoryError: Failed to allocate a 103059952 byte allocation with 16777120 free bytes and 58MB until OOM
at java.util.ArrayList.add(ArrayList.java:118)
at estec.android.mvi32.OCTActivity.onOptionsItemSelected(OCTActivity.java:294)
at android.app.Activity.onMenuItemSelected(Activity.java:2882)
at android.support.v4.app.FragmentActivity.onMenuItemSelected(FragmentActivity.java:353)
at android.support.v7.app.AppCompatActivity.onMenuItemSelected(AppCompatActivity.java:144)
at android.support.v7.internal.view.WindowCallbackWrapper.onMenuItemSelected(WindowCallbackWrapper.java:99)
at android.support.v7.app.AppCompatDelegateImplV7.onMenuItemSelected(AppCompatDelegateImplV7.java:541)
at android.support.v7.internal.view.menu.MenuBuilder.dispatchMenuItemSelected(MenuBuilder.java:811)
at android.support.v7.internal.view.menu.MenuItemImpl.invoke(MenuItemImpl.java:153)
at android.support.v7.internal.view.menu.MenuBuilder.performItemAction(MenuBuilder.java:958)
at android.support.v7.internal.view.menu.MenuBuilder.performItemAction(MenuBuilder.java:948)
at android.support.v7.internal.view.menu.MenuPopupHelper.onItemClick(MenuPopupHelper.java:187)
at android.widget.AdapterView.performItemClick(AdapterView.java:300)
at android.widget.AbsListView.performItemClick(AbsListView.java:1143)
at android.widget.AbsListView$PerformClick.run(AbsListView.java:3044)
at android.widget.AbsListView.onTouchUp(AbsListView.java:3872)
at android.widget.AbsListView.onTouchEvent(AbsListView.java:3627)
at android.view.View.dispatchTouchEvent(View.java:8388)
at android.view.ViewGroup.dispatchTransformedTouchEvent(ViewGroup.java:2424)
at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:2158)
at android.view.ViewGroup.dispatchTransformedTouchEvent(ViewGroup.java:2430)
at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:2172)
at android.widget.PopupWindow$PopupViewContainer.dispatchTouchEvent(PopupWindow.java:1682)
at android.view.View.dispatchPointerEvent(View.java:8578)
at android.view.ViewRootImpl$ViewPostImeInputStage.processPointerEvent(ViewRootImpl.java:4021)
at android.view.ViewRootImpl$ViewPostImeInputStage.onProcess(ViewRootImpl.java:3887)
at android.view.ViewRootImpl$InputStage.deliver(ViewRootImpl.java:3449)
at android.view.ViewRootImpl$InputStage.onDeliverToNext(ViewRootImpl.java:3502)
at android.view.ViewRootImpl$InputStage.forward(ViewRootImpl.java:3468)
at android.view.ViewRootImpl$AsyncInputStage.forward(ViewRootImpl.java:3578)
at android.view.ViewRootImpl$InputStage.apply(ViewRootImpl.java:3476)
at android.view.ViewRootImpl$AsyncInputStage.apply(ViewRootImpl.java:3635)
at android.view.ViewRootImpl$InputStage.deliver(ViewRootImpl.java:3449)
at android.view.ViewRootImpl$InputStage.onDeliverToNext(ViewRootImpl.java:3502)
at android.view.ViewRootImpl$InputStage.forward(ViewRootImpl.java:3468)
at android.view.ViewRootImpl$InputStage.apply(ViewRootImpl.java:3476)
at android.view.ViewRootImpl$InputStage.deliver(ViewRootImpl.java:3449)
at android.view.ViewRootImpl.deliverInputEvent(ViewRootImpl.java:5701)
at android.view.ViewRootImpl.doProcessInputEvents(ViewRootImpl.java:5675)
at android.view.ViewRootImpl.enqueueInputEvent(ViewRootImpl.java:5646)
at android.view.ViewRootImpl$WindowInputEventReceiver.onInputEvent(ViewRootImpl.java:5791)
at android.view.InputEventReceiver.dispatchInputEvent(InputEventReceiver.java:185)
at android.os.MessageQueue.nativePollOnce(Native Method)
at android.os.MessageQueue.next(MessageQueue.java:143)
at android.os.Looper.loop(Looper.java:122)
at android.app.ActivityThread.main(ActivityThread.java:5253)
at java.lang.reflect.Method.invoke(Native Method)
at java.lang.reflect.Method.invoke(Method.java:372)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:899)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:694)


How to resolve this problem??.

Answer

This for loop never terminates, look close at the last part:

for(int j= 0; j< arr.length;i++)

Change the i++ to j++ to fix this bug.