Created
December 16, 2015 07:24
-
-
Save fyhack/e571ae1d965015127408 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public static class GCUtils { | |
private static final String GC_TAG = GCUtils.class.getSimpleName(); | |
public static final int GC_TRY_COUNT = 2; | |
// GC_TRY_LOOP_MAX is used for the hard limit of GC wait, | |
// GC_TRY_LOOP_MAX should be greater than GC_TRY_COUNT. | |
public static final int GC_TRY_LOOP_MAX = 5; | |
private static final long GC_INTERVAL = DateUtils.SECOND_IN_MILLIS; | |
private static GCUtils sInstance = new GCUtils(); | |
private int mGCTryCount = 0; | |
public static GCUtils getInstance() { | |
return sInstance; | |
} | |
public void reset() { | |
mGCTryCount = 0; | |
} | |
public boolean tryGCOrWait(String metaData, Throwable t) { | |
if (mGCTryCount == 0) { | |
System.gc(); | |
} | |
if (++mGCTryCount > GC_TRY_COUNT) { | |
LatinImeLogger.logOnException(metaData, t); | |
return false; | |
} else { | |
try { | |
Thread.sleep(GC_INTERVAL); | |
return true; | |
} catch (InterruptedException e) { | |
Log.e(GC_TAG, "Sleep was interrupted."); | |
LatinImeLogger.logOnException(metaData, t); | |
return false; | |
} | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment