2016-04-15 7 views
4

Ich habe eine App, die Inhalte aus dem Internet herunterladen. Musik, Videos, PDFs .... wie ein Download-Manager.Schwerwiegende Ausnahme: java.lang.OutOfMemoryError mit okhttp3 okio für Download-Dateien

Aber jetzt seine stürzt jedes Mal heruntergeladen wird es Inhalt:

E/LVN/advanced_memory_manager.c: ---------------------------------- AMM report ------------------------------ 
-> Memory Currently Allocated: 0 bytes <=> 0 components 
-> Max Memory Need: 512000 bytes 
-> Overall Memory Allocation: 515652 bytes (l:423) 
E/art: Throwing OutOfMemoryError "Failed to allocate a 2060 byte allocation with 16777232 free bytes and 308MB until OOM; failed due to fragmentation (required continguous free 4096 bytes where largest contiguous free 0 bytes)" 
E/AndroidRuntime: FATAL EXCEPTION: OkHttp Dispatcher 
    Process: com.rokki.life2, PID: 32171 
    java.lang.OutOfMemoryError: Failed to allocate a 2060 byte allocation with 16777232 free bytes and 308MB until OOM; failed due to fragmentation (required continguous free 4096 bytes where largest contiguous free 0 bytes) 
     at okio.Segment.<init>(Segment.java:58) 
     at okio.SegmentPool.take(SegmentPool.java:46) 
     at okio.Buffer.writableSegment(Buffer.java:1114) 
     at okio.Okio$2.read(Okio.java:137) 
     at okio.AsyncTimeout$2.read(AsyncTimeout.java:211) 
     at okio.RealBufferedSource.read(RealBufferedSource.java:50) 
     at okhttp3.internal.http.Http1xStream$FixedLengthSource.read(Http1xStream.java:381) 
     at okio.RealBufferedSource.request(RealBufferedSource.java:71) 
     at okhttp3.logging.HttpLoggingInterceptor.intercept(HttpLoggingInterceptor.java:225) 
     at okhttp3.RealCall$ApplicationInterceptorChain.proceed(RealCall.java:187) 
     at okhttp3.RealCall.getResponseWithInterceptorChain(RealCall.java:160) 
     at okhttp3.RealCall.access$100(RealCall.java:30) 
     at okhttp3.RealCall$AsyncCall.execute(RealCall.java:127) 
     at okhttp3.internal.NamedRunnable.run(NamedRunnable.java:32) 
     at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1112) 
     at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:587) 
     at java.lang.Thread.run(Thread.java:818) 

und

Fatal Exception: java.lang.OutOfMemoryError: Failed to allocate a 106405148 byte allocation with 16777120 free bytes and 82MB until OOM 
     at java.lang.String.<init>(String.java:332) 
     at java.lang.String.<init>(String.java:371) 
     at okio.Buffer.readString(Buffer.java:579) 
     at okio.Buffer.readString(Buffer.java:562) 
     at okhttp3.logging.HttpLoggingInterceptor.intercept(HttpLoggingInterceptor.java:244) 
     at okhttp3.RealCall$ApplicationInterceptorChain.proceed(RealCall.java:187) 
     at okhttp3.RealCall.getResponseWithInterceptorChain(RealCall.java:160) 
     at okhttp3.RealCall.access$100(RealCall.java:30) 
     at okhttp3.RealCall$AsyncCall.execute(RealCall.java:127) 
     at okhttp3.internal.NamedRunnable.run(NamedRunnable.java:32) 
     at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1112) 
     at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:587) 
     at java.lang.Thread.run(Thread.java:818) 

Ich habe bereits das meinem Manifest:

<application 
    android:name=".app.MyApp" 
    android:allowBackup="true" 
    android:hardwareAccelerated="true" 
    android:icon="@mipmap/ic_launcher" 
    tools:replace="android:icon" 
    android:label="@string/app_name" 
    android:largeHeap="true" 
    android:supportsRtl="true" 
    android:theme="@style/AppTheme.NoActionBar"> 

Codebeispiel:

private OkHttpClient client; 


onViewCreated...{ 

    //Initiate OkHttp with interceptor 
    HttpLoggingInterceptor logging = new HttpLoggingInterceptor(); 
    logging.setLevel(HttpLoggingInterceptor.Level.BODY); 

    client = new OkHttpClient.Builder() 
      .addInterceptor(logging) 
      .build(); 

... 
} 



private void downloadPdf() { 
AsyncTask.execute(new Runnable() { 
    @Override 
    public void run() { 


    client 
     .newCall(getRequest(Config._API_PDF)) 
     .enqueue(new Callback() { 
      @Override 
      public void onFailure(Call call, IOException e) { 
       ... 
      } 


       @Override 
       public void onResponse(Call call, Response response) throws IOException { 


       try { 

            InputStream ins = response.body().byteStream(); 
            BufferedReader in = new BufferedReader(new InputStreamReader(ins)); 

            String lineTotal = ""; 
            while (true) { 
             String line = in.readLine(); 
             if (line == null) 
              break; 
             else 
              lineTotal += line; 
            } 

            ...json parsing ... 



... 

Mein BuildGradle:

apply plugin: 'com.android.application' 

android { 
    compileSdkVersion 23 
    buildToolsVersion "23.0.2" 

    defaultConfig { 
     applicationId "com.my.app" 
     minSdkVersion 17 
     targetSdkVersion 22 
     versionCode 1 
     versionName "1.0" 

     ndk { abiFilters "armeabi", "x86", "mips" } 
    } 
    buildTypes { 
     release { 
      minifyEnabled false 
      proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro' 
     } 
    } 
} 


dependencies { 
    compile fileTree(dir: 'libs', include: ['*.jar']) 

    final SUPPORT_LIBRARY_VERSION = '23.2.0' 
    final PLAY_SERVICES_VERSION = '8.3.0' 
    final RETROFIT_VERSION = '2.0.0' 
    final OKHTTP3_VERSION = '3.2.0' 


    compile "com.android.support:design:$SUPPORT_LIBRARY_VERSION" 
    compile "com.android.support:support-v4:$SUPPORT_LIBRARY_VERSION" 
    compile "com.android.support:appcompat-v7:$SUPPORT_LIBRARY_VERSION" 
    compile "com.android.support:cardview-v7:$SUPPORT_LIBRARY_VERSION" 
    compile "com.android.support:recyclerview-v7:$SUPPORT_LIBRARY_VERSION" 


    compile "com.squareup.retrofit2:retrofit:$RETROFIT_VERSION" 
    compile "com.squareup.retrofit2:converter-gson:$RETROFIT_VERSION" 
    compile "com.squareup.retrofit2:adapter-rxjava:$RETROFIT_VERSION" 
    compile "com.squareup.okhttp3:logging-interceptor:$OKHTTP3_VERSION" 
    compile 'com.google.code.gson:gson:2.6.2' 
    compile 'com.jakewharton:butterknife:7.0.1' 

} 

dann habe ich ein anderes ähnliches Verfahren zum Herunterladen Video ...

Hat jemand über diese vor kam? Dank

+0

'java.lang.OutOfMemoryError: Fehler zuzuteilen eine 2060 Byte Zuweisung mit 16777232 freien Bytes und 308 MB bis OOM' –

+0

postet den Code, der zum Herunterladen verwendet wird. möglicherweise gibt es ein Ressourcenleck –

+0

nur einen Beispielcode hinzugefügt. Danke – Joolah

Antwort

0

hinzufügen unten Linie in DefaultConfig in gradle

multiDexEnabled true 

dann in Android Funktion

dexOptions { 
     incremental = true; 
     preDexLibraries = false 
     javaMaxHeapSize "4g" // 2g should be also OK 
    } 

Fügen Sie diese dependecies

compile 'com.android.support:multidex:1.0.1'