1

在我的Android應用填補了整個內存我有一個ViewPager這樣的:FragmentStatePagerAdapter只是通過滑動

<android.support.v4.view.ViewPager 
        android:layout_width="match_parent" 
        android:layout_height="300dp" 
        android:visibility="visible" 
        android:id="@+id/single_product_details_view_pager"/> 

而且我設置這樣的FragmentStatePagerAdapter:

productHeroPagerAdapter = new ProductHeroPagerAdapter(
              fragmentManager, imagesArray, videosArray, 
              singleProductDetailsSlidingLayer.getId(), 
              productImagesViewPager.getId()); 

和我ProductHeroPagerAdapter是像這樣:

import android.support.v4.app.Fragment; 
import android.support.v4.app.FragmentManager; 
import android.support.v4.app.FragmentStatePagerAdapter; 

import org.json.JSONArray; 
import org.json.JSONException; 
import org.json.JSONObject; 

/** 
* Pager adapter for hero images and videos of the product 
* This pager adapter is responsible to do necessary calculations to handle separate images and videos array at one place 
* Created by ishan on 22/1/16. 
*/ 
public class ProductHeroPagerAdapter extends FragmentStatePagerAdapter { 
private static final String TAG = ProductHeroPagerAdapter.class.getSimpleName(); 
private JSONArray videosArray, imagesArray; 
private final int slidingLayerId; 
private final int viewPagerId; 

public ProductHeroPagerAdapter(FragmentManager fragmentManager, JSONArray imagesArray, JSONArray videosArray, int slidingLayerID, int viewPagerId) { 
    super(fragmentManager); 
    this.imagesArray = imagesArray; 
    this.videosArray = videosArray; 
    this.slidingLayerId = slidingLayerID; 
    this.viewPagerId = viewPagerId; 
} 

@Override 
public int getCount() { 
    return imagesArray.length() + videosArray.length(); 
} 

@Override 
public Fragment getItem(int position) { 
    // First Fragments will be images 
    if (position < imagesArray.length()) { 
     // Sending Fragment containing image 
     String imageUrl; 
     try { 
      JSONObject imageObject = (JSONObject) imagesArray.get(position); 
      imageUrl = imageObject.getString("img_600"); 
     } catch (JSONException e) { 
      e.printStackTrace(); 
      // imageUrl is blank making the error cases to have default error case images 
      imageUrl = ""; 
     } 
     return ProductHeroImageFragment.newInstance(imageUrl, position, imagesArray, videosArray); 
    } else { 
     // Sending Fragment containing video 
     String videoUrl; 
     try { 
      JSONObject videoObject = (JSONObject) videosArray.get(position - imagesArray.length()); 
      videoUrl = videoObject.getString("link"); 
     } catch (JSONException e) { 
      e.printStackTrace(); 
      // videoUrl is blank making the error cases to have default error case screen 
      videoUrl = ""; 
     } 
     return ProductHeroVideoFragment.newInstance(videoUrl, position, imagesArray, videosArray, slidingLayerId, viewPagerId); 
    } 
} 

}

我遇到的問題是,當我在ViewPager上來回切換片段時,應用程序的內存使用量不斷增加,大約128 MB(在我測試的電話上)完全停止加工。

memory uses imcreasing

memory uses reasching 127.93 MB

我沒有做不是迅速刷卡片段的任何其他。 片段並不複雜,它們只包含圖像或視頻。目前,我正在測試空白videoJsonArray,以便只顯示圖像。我似乎無法弄清楚RAM爲何如此使用,爲什麼它們不斷增加?

UPDATE:

我似乎無法弄清楚什麼是保持我的記憶擠兌。這裏是我的ProductHeroImageFragment:

import android.content.Intent; 
import android.os.Bundle; 
import android.support.v4.app.Fragment; 
import android.view.LayoutInflater; 
import android.view.View; 
import android.view.ViewGroup; 
import android.widget.ImageView; 

import com.bumptech.glide.Glide; 
import com.bumptech.glide.load.resource.drawable.GlideDrawable; 
import com.bumptech.glide.request.animation.GlideAnimation; 
import com.bumptech.glide.request.target.SimpleTarget; 
import com.ishan1608.utility.ColoredProgressBar; 
import com.weddingonclick.customer.R; 

import org.json.JSONArray; 

/** 
* Product hero image display fragment 
* Created by ishan on 22/1/16. 
*/ 
public class ProductHeroImageFragment extends Fragment { 

    private static final String IMAGE_URL = "IMAGE_URL"; 
    public static final String POSITION = "POSITION"; 
    public static final String IMAGES_ARRAY_STRING = "IMAGES_ARRAY_STRING"; 
    public static final String VIDEOS_ARRAY_STRING = "VIDEOS_ARRAY_STRING"; 
    private String imageUrl; 
    private int position; 
    private String imagesArrayString, videosArrayString; 

    public ProductHeroImageFragment() { 
     // Required Empty Constructor 
    } 

    /** 
    * Use this factory method to create a new instance of 
    * this fragment using the provided parameters. 
    * 
    * @param imageUrl Url of the image to be displayed 
    * @param position position in the image and video array 
    * @param imagesArray JSONArray for the images 
    * @param videosArray JSONArray for the videos 
    * @return A new instance of fragment ProductHeroImageFragment. 
    */ 
    public static ProductHeroImageFragment newInstance(String imageUrl, int position, JSONArray imagesArray, JSONArray videosArray) { 
     ProductHeroImageFragment fragment = new  ProductHeroImageFragment(); 
    Bundle args = new Bundle(); 
    args.putString(IMAGE_URL, imageUrl); 
    args.putInt(POSITION, position); 
    args.putString(IMAGES_ARRAY_STRING, imagesArray.toString()); 
    args.putString(VIDEOS_ARRAY_STRING, videosArray.toString()); 
    fragment.setArguments(args); 
    return fragment; 
} 

@Override 
public void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    if (getArguments() != null) { 
     this.imageUrl = getArguments().getString(IMAGE_URL); 
     this.position = getArguments().getInt(POSITION); 
     this.imagesArrayString = getArguments().getString(IMAGES_ARRAY_STRING); 
     this.videosArrayString = getArguments().getString(VIDEOS_ARRAY_STRING); 
    } 
} 

@Override 
public View onCreateView(LayoutInflater inflater, ViewGroup container, 
         Bundle savedInstanceState) { 
    final View rootView = inflater.inflate(R.layout.fragment_product_hero_image, container, false); 
    final ColoredProgressBar loadingProgressBar = (ColoredProgressBar) rootView.findViewById(R.id.loading_progress_bar); 
    loadingProgressBar.setVisibility(View.VISIBLE); 
    final ImageView productHeroImageView = (ImageView) rootView.findViewById(R.id.product_hero_image_view); 
    Glide.with(getContext()) 
      .load(this.imageUrl) 
      .fitCenter() 
      .into(new SimpleTarget<GlideDrawable>() { 
       @Override 
       public void onResourceReady(GlideDrawable resource, GlideAnimation<? super GlideDrawable> glideAnimation) { 
        loadingProgressBar.setVisibility(View.GONE); 
        productHeroImageView.setImageDrawable(resource); 
       } 
      }); 

    rootView.setOnClickListener(new View.OnClickListener() { 
     @Override 
     public void onClick(View v) { 
      if (!getActivity().getClass().equals(ProductHeroImageVideoActivity.class)) { 
       final Intent productHeroImageVideoIntent = new Intent(getActivity(), ProductHeroImageVideoActivity.class); 
       productHeroImageVideoIntent.putExtra(POSITION, position); 
       productHeroImageVideoIntent.putExtra(IMAGES_ARRAY_STRING, imagesArrayString); 
       productHeroImageVideoIntent.putExtra(VIDEOS_ARRAY_STRING, videosArrayString); 
       startActivity(productHeroImageVideoIntent); 
      } 
     } 
    }); 

    return rootView; 
} 

@Override 
public void onDestroy() { 
    super.onDestroy(); 
    Glide.get(getContext()).clearMemory(); 
} 
} 

這裏是fragment_product_hero_image.xml:

<?xml version="1.0" encoding="utf-8"?> 
<RelativeLayout 
xmlns:android="http://schemas.android.com/apk/res/android" 
android:orientation="vertical" 
android:layout_width="match_parent" 
android:layout_height="match_parent" 
android:background="@color/pure_black"> 
<ImageView 
    android:layout_width="match_parent" 
    android:layout_height="match_parent" 
    android:id="@+id/product_hero_image_view"/> 
<com.ishan1608.utility.ColoredProgressBar 
    android:layout_width="45dp" 
    android:layout_height="45dp" 
    android:layout_centerInParent="true" 
    android:id="@+id/loading_progress_bar"/> 
</RelativeLayout> 

回答

0

按照該Doc,你應該保持頁數限制儘可能低。

參考this post,我相信系統gc不會釋放所有東西。而對於複雜的佈局,您必須設置onDestroy用於內存釋放。 This post可能會幫助你。

+0

我已經在問題中添加了我的Fragment代碼。請告訴我內存問題在哪裏。 – Ishan

+0

嗨,我現在讀了關於滑翔。所以,好像有一個API:'target.clear()'。我們有一個'clearDrawables'選項,可以在調用'onDestroy'時使用。請注意,Glide在關閉時不會釋放圖像。 – cprakashagr

+0

嘿,請看看這篇文章:https://github.com/bumptech/glide/issues/1062 – cprakashagr