Android,在AVD上运行时抛出内存不足错误

时间:2015-02-17 16:00:50

标签: java android sqlite

这是我的MainActivity班级

public class MainActivity extends Activity {

    TextView resultView;
    ContentValues values;
    DbHelper dbHelper;
    SQLiteDatabase db;

    public static final String TAG="database";

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        StrictMode.enableDefaults();
        resultView = (TextView) findViewById(R.id.result);
         dbHelper = new DbHelper(this);
         db = dbHelper.getWritableDatabase(); 

         values = new ContentValues();

            Log.d(TAG,"oncreate");

        getData();
    }

    public void getData(){

        String result = "";
        InputStream isr = null;
        try{
            HttpClient httpclient = new DefaultHttpClient();
            HttpPost httppost = new HttpPost("http://10.0.2.2/idao/json/flore.json");
            HttpResponse response = httpclient.execute(httppost);
            HttpEntity entity = response.getEntity();
            isr = entity.getContent();
        }
        catch (Exception e){
            Log.e("log_tag", "error in connection " + e.toString());
            resultView.setText("couldnot connect to database");
        }
        try{
            BufferedReader reader = new BufferedReader(new InputStreamReader(isr, "iso-8859-1"),8);
            StringBuilder sb = new StringBuilder();
            String line =null;
            while ((line = reader.readLine()) != null){
                sb.append(line+ "\n");
            }
            isr.close();
            result = sb.toString();
        }
        catch(Exception e){
            Log.e("log_tag", "error converting result "+e.toString());
        }
        try{
            String s ="";
            JSONArray jArray = new JSONArray(result);

            for(int i=0; i<jArray.length(); i++){
                JSONObject json = jArray.getJSONObject(i);

                s = s+ "Name  : "+json.getString("DbHelper.F_AC");

                values.put(DbHelper.F_AA, json.getString(DbHelper.F_AA));
                values.put(DbHelper.F_AB, json.getString(DbHelper.F_AB));
                values.put(DbHelper.F_AC, json.getString(DbHelper.F_AC));
                values.put(DbHelper.F_AD, json.getString(DbHelper.F_AD));
                values.put(DbHelper.F_AE, json.getString(DbHelper.F_AE));
                values.put(DbHelper.F_AF, json.getString(DbHelper.F_AF));
                values.put(DbHelper.F_AG, json.getString(DbHelper.F_AG));
                values.put(DbHelper.F_AG, json.getString(DbHelper.F_AH));
                values.put(DbHelper.F_AI, json.getString(DbHelper.F_AI));
                values.put(DbHelper.F_AJ, json.getString(DbHelper.F_AJ));
                values.put(DbHelper.F_AK, json.getString(DbHelper.F_AK));
                values.put(DbHelper.F_AL, json.getString(DbHelper.F_AL));
                values.put(DbHelper.F_AM, json.getString(DbHelper.F_AM));
                values.put(DbHelper.F_AN, json.getString(DbHelper.F_AN));
                values.put(DbHelper.F_AO, json.getString(DbHelper.F_AO));
                values.put(DbHelper.F_AP, json.getString(DbHelper.F_AP));
                values.put(DbHelper.F_AQ, json.getString(DbHelper.F_AQ));
                values.put(DbHelper.F_AR, json.getString(DbHelper.F_AR));
                values.put(DbHelper.F_AS, json.getString(DbHelper.F_AS));
                values.put(DbHelper.F_AT, json.getString(DbHelper.F_AT));
                values.put(DbHelper.F_AU, json.getString(DbHelper.F_AU));
                values.put(DbHelper.F_AV, json.getString(DbHelper.F_AV));
                values.put(DbHelper.F_AW, json.getString(DbHelper.F_AW));
                values.put(DbHelper.F_AX, json.getString(DbHelper.F_AX));
                values.put(DbHelper.F_AY, json.getString(DbHelper.F_AY));
                values.put(DbHelper.F_AZ, json.getString(DbHelper.F_AZ));               

                 db.insertWithOnConflict(DbHelper.TABLE, null, values, SQLiteDatabase.CONFLICT_IGNORE);

            }
            resultView.setText(s);
        }
        catch(Exception e){
            Log.e("log_tag", "error parsing data "+e.toString());
        }
    }
}

这是我的DbHelper班级

public class DbHelper extends SQLiteOpenHelper{

    static final String TAG = "DbHelper";
    public static final String DB_NAME = "wikwio_idao.db";
    public static final int DB_VERSION = 11;

        public static final String TABLE = "flore";
        public static final String F_AA = "Objet";
        public static final String F_AB = "Espece";
        public static final String F_AC = "portal_url";
        public static final String F_AD = "Famille";
        public static final String F_AE = "Legende";
        public static final String F_AF = "Gramineen";
        public static final String F_AG = "Dresse";
        public static final String F_AH = "Liane";
        public static final String F_AI = "Rosette";
        public static final String F_AJ = "Prostre";
        public static final String F_AK = "Tracant";
        public static final String F_AL = "Flottant";
        public static final String F_AM = "Liane_vrilles";
        public static final String F_AN = "Liane_sans";
        public static final String F_AO = "Phyllo_Alterne";
        public static final String F_AP = "Phyllo_Opposee";
        public static final String F_AQ = "Phyllo_vert";
        public static final String F_AR = "Simple";
        public static final String F_AS = "Composee";
        public static final String F_AT = "Monocotyledone";
        public static final String F_AU = "Sans_feuilles";
        public static final String F_AV = "L_sed";
        public static final String F_AW = "L_gra";
        public static final String F_AX = "L_bi";
        public static final String F_AY = "L_3flB";
        public static final String F_AZ = "L_3fl";

    public DbHelper(Context context) {
        super(context, DB_NAME, null, DB_VERSION);
        // TODO Auto-generated constructor stub
    }


    @Override
    public void onCreate(SQLiteDatabase db) {

         String sql = "create table " + TABLE + " (" + F_AA + " text, " + F_AB + " text, " + F_AC + " text, " 
                 + F_AD + " text, " + F_AE + " text, " + F_AF + " int, " + F_AG + " int, " + F_AH + " int, " 
                 + F_AI + " int, " + F_AJ + " int, " + F_AK + " int, " + F_AL + " int, " + F_AM + " int, " 
                 + F_AN + " int, " + F_AO + " int, " + F_AP + " int, " + F_AQ + " int, " + F_AR + " int, " 
                 + F_AS + " int, " + F_AT + " int, " + F_AU + " int, " + F_AV + " int, " + F_AW + " int, " 
                 + F_AX + " int, " + F_AY + " int, " + F_AZ + " int)"; 

        db.execSQL(sql);    
        Log.d(TAG,"oncreate-database");

    }
    @Override
    public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {

        db.execSQL("drop table if exists "+ TABLE); 
        onCreate(db);
    }

}

我调用flore.json的文件大小为1.8 MB ..我搜索了堆栈溢出,我发现使用setChunkedStreamingMode(lengthoffile); ...但是我的文件大小是1.8mb ..请帮我说说从大文件中获取数据

我的日志错误

02-17 20:08:04.834: E/art(30265): Throwing OutOfMemoryError "Failed to allocate a 164 byte allocation with 100 free bytes and 100B until OOM" (recursive case)

02-17 20:08:04.877: E/art(30265): "main" prio=5 tid=1 Runnable

02-17 20:08:04.877: E/art(30265):   | group="main" sCount=0 dsCount=0 obj=0x732e4970 self=0xb5107800

02-17 20:08:04.877: E/art(30265):   | sysTid=30265 nice=0 cgrp=apps sched=0/0 handle=0xb6ff4ec8

02-17 20:08:04.877: E/art(30265):   | state=R schedstat=( 23280000000 17280000000 1932 ) utm=2190 stm=138 core=0 HZ=100

02-17 20:08:04.877: E/art(30265):   | stack=0xbe0db000-0xbe0dd000 stackSize=8MB

02-17 20:08:04.877: E/art(30265):   | held mutexes= "mutator lock"(shared held
)
02-17 20:08:04.877: E/art(30265):   at java.util.Arrays.copyOfRange(Arrays.java:2701)

02-17 20:08:04.877: E/art(30265):   at java.lang.String.<init>(String.java:421)

02-17 20:08:04.878: E/art(30265):   at org.json.JSONTokener.nextString(JSONTokener.java:210)

02-17 20:08:04.878: E/art(30265):   at org.json.JSONTokener.nextValue(JSONTokener.java:107)
02-17 20:08:04.878: E/art(30265):   at org.json.JSONTokener.readObject(JSONTokener.java:362)
02-17 20:08:04.878: E/art(30265):   at org.json.JSONTokener.nextValue(JSONTokener.java:100)
02-17 20:08:04.878: E/art(30265):   at org.json.JSONTokener.readArray(JSONTokener.java:430)
02-17 20:08:04.878: E/art(30265):   at org.json.JSONTokener.nextValue(JSONTokener.java:103)

02-17 20:08:04.878: E/art(30265):   at org.json.JSONArray.<init>(JSONArray.java:92)

02-17 20:08:04.878: E/art(30265):   at org.json.JSONArray.<init>(JSONArray.java:108)

02-17 20:08:04.878: E/art(30265):   at com.learnsocial.testingdatabase.MainActivity.getData(MainActivity.java:81)

02-17 20:08:04.878: E/art(30265):   at com.learnsocial.testingdatabase.MainActivity.onCreate(MainActivity.java:48)
02-17 20:08:04.878: E/art(30265):   at android.app.Activity.performCreate(Activity.java:5933)
02-17 20:08:04.878: E/art(30265):   at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1105)

02-17 20:08:04.878: E/art(30265):   at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2251)

02-17 20:08:04.878: E/art(30265):   at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2360)

02-17 20:08:04.878: E/art(30265):   at android.app.ActivityThread.access$800(ActivityThread.java:144)
02-17 20:08:04.879: E/art(30265):   at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1278)
02-17 20:08:04.879: E/art(30265):   at android.os.Handler.dispatchMessage(Handler.java:102)
02-17 20:08:04.879: E/art(30265):   at android.os.Looper.loop(Looper.java:135)
02-17 20:08:04.879: E/art(30265):   at android.app.ActivityThread.main(ActivityThread.java:5221)
02-17 20:08:04.879: E/art(30265):   at java.lang.reflect.Method.invoke!(Native method)
02-17 20:08:04.879: E/art(30265):   at java.lang.reflect.Method.invoke(Method.java:372)

02-17 20:08:04.879: E/art(30265):   at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:899)

02-17 20:08:04.879: E/art(30265):   at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:694)
02-17 20:08:04.927: D/AndroidRuntime(30265): Shutting down VM

02-17 20:08:05.147: I/art(30265): Clamp target GC heap from 17MB to 16MB

02-17 20:08:05.147: I/art(30265): Alloc partial concurrent mark sweep GC freed 3(96B) AllocSpace objects, 0(0B) LOS objects, 0% free, 15MB/16MB, paused 956us total 318.845ms

02-17 20:08:05.151: I/art(30265): WaitForGcToComplete blocked for 225.093ms for cause Background

02-17 20:08:05.153: I/art(30265): WaitForGcToComplete blocked for 224.800ms for cause Alloc

02-17 20:08:05.169: I/art(30265): Alloc sticky concurrent mark sweep GC freed 0(0B) AllocSpace objects, 0(0B) LOS objects, 0% free, 15MB/16MB, paused 5.961ms total 15.315ms

02-17 20:08:05.546: I/art(30265): Alloc concurrent mark sweep GC freed 269120(8MB) AllocSpace objects, 2(6MB) LOS objects, 46% free, 583KB/1095KB, paused 1.005ms total 373.878ms

02-17 20:08:05.550: I/art(30265): WaitForGcToComplete blocked for 366.410ms for cause Alloc
02-17 20:08:05.573: E/StrictMode(30265): A resource was acquired at attached stack trace but never released. See java.io.Closeable for information on avoiding resource leaks.
02-17 20:08:05.573: E/StrictMode(30265): java.lang.Throwable: Explicit termination method 'close' not called

02-17 20:08:05.573: E/StrictMode(30265):    at dalvik.system.CloseGuard.open(CloseGuard.java:184)

02-17 20:08:05.573: E/StrictMode(30265):    at android.database.sqlite.SQLiteDatabase.openInner(SQLiteDatabase.java:807)

02-17 20:08:05.573: E/StrictMode(30265):    at android.database.sqlite.SQLiteDatabase.open(SQLiteDatabase.java:791)
02-17 20:08:05.573: E/StrictMode(30265):    at android.database.sqlite.SQLiteDatabase.openDatabase(SQLiteDatabase.java:694)
02-17 20:08:05.573: E/StrictMode(30265):    at android.app.ContextImpl.openOrCreateDatabase(ContextImpl.java:1142)
02-17 20:08:05.573: E/StrictMode(30265):    at android.content.ContextWrapper.openOrCreateDatabase(ContextWrapper.java:267)
02-17 20:08:05.573: E/StrictMode(30265):    at android.database.sqlite.SQLiteOpenHelper.getDatabaseLocked(SQLiteOpenHelper.java:223)

02-17 20:08:05.573: E/StrictMode(30265):    at android.database.sqlite.SQLiteOpenHelper.getWritableDatabase(SQLiteOpenHelper.java:163)

02-17 20:08:05.573: E/StrictMode(30265):    at com.learnsocial.testingdatabase.MainActivity.onCreate(MainActivity.java:41)
02-17 20:08:05.573: E/StrictMode(30265):    at android.app.Activity.performCreate(Activity.java:5933)
02-17 20:08:05.573: E/StrictMode(30265):    at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1105)
02-17 20:08:05.573: E/StrictMode(30265):    at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2251)
02-17 20:08:05.573: E/StrictMode(30265):    at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2360)
02-17 20:08:05.573: E/StrictMode(30265):    at android.app.ActivityThread.access$800(ActivityThread.java:144)
02-17 20:08:05.573: E/StrictMode(30265):    at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1278)
02-17 20:08:05.573: E/StrictMode(30265):    at android.os.Handler.dispatchMessage(Handler.java:102)
02-17 20:08:05.573: E/StrictMode(30265):    at android.os.Looper.loop(Looper.java:135)
02-17 20:08:05.573: E/StrictMode(30265):    at android.app.ActivityThread.main(ActivityThread.java:5221)
02-17 20:08:05.573: E/StrictMode(30265):    at java.lang.reflect.Method.invoke(Native Method)
02-17 20:08:05.573: E/StrictMode(30265):    at java.lang.reflect.Method.invoke(Method.java:372)
02-17 20:08:05.573: E/StrictMode(30265):    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:899)
02-17 20:08:05.573: E/StrictMode(30265):    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:694)
02-17 20:08:05.660: I/art(30265): Alloc partial concurrent mark sweep GC freed 208(60KB) AllocSpace objects, 0(0B) LOS objects, 46% free, 594KB/1106KB, paused 1.046ms total 106.015ms
02-17 20:08:05.667: E/AndroidRuntime(30265): FATAL EXCEPTION: main
02-17 20:08:05.667: E/AndroidRuntime(30265): Process: com.learnsocial.testingdatabase, PID: 30265
02-17 20:08:05.667: E/AndroidRuntime(30265): java.lang.OutOfMemoryError: OutOfMemoryError thrown while trying to throw OutOfMemoryError; no stack available
02-17 20:08:17.621: I/Process(30265): Sending signal. PID: 30265 SIG: 9

1 个答案:

答案 0 :(得分:0)

你可以添加

  

机器人:largeHeap =真

到您的清单:

<application
        android:allowBackup="true"
        android:icon="@drawable/logoapp"
        android:label="@string/app_name"
        android:largeHeap="true" >