Java tutorial
//package com.java2s; import android.view.LayoutInflater; import android.view.View; import android.view.ViewGroup; public class Main { private static final int OUT_OF_MEMORY_RETRY_COUNT = 5; public static <T extends View> T inflateWithOutOfMemoryRetrial(Class<T> clazz, LayoutInflater inflater, int resourceId, ViewGroup root, boolean attachToRoot) { for (int i = 0; i < OUT_OF_MEMORY_RETRY_COUNT; ++i) { try { return clazz.cast(inflater.inflate(resourceId, root, attachToRoot)); } catch (OutOfMemoryError e) { // Retry with GC. System.gc(); } } return clazz.cast(inflater.inflate(resourceId, root, attachToRoot)); } }