react-native内存优化--图⽚内存
react-native在android中使⽤fresco来加载图⽚。说到fresco很多⼈都会为之欢呼,总⽐各种轮⼦内存调优的好。不过在react-native 中在它的使⽤fresco还是稍显随意,如果⼀个页⾯是典型的图列表,运⾏⼀段时间⽤dumpsys meminfo命令观察您的程序内存会出现令您惊讶的内存暴增。
reactnative开发为了使得图⽚内存好看点,我们来修改以下react-native的FrescoModule模块。
public class MyFrescoModule extends ReactContextBaseJavaModule implements
ModuleDataCleaner.Cleanable {
private @Nullable
ImagePipelineConfig mConfig;
public MyFrescoModule(ReactApplicationContext reactContext) {
this(reactContext, getDefaultConfig(reactContext, null, null));
}
public MyFrescoModule(ReactApplicationContext reactContext, RequestListener listener) {
this(reactContext, getDefaultConfig(reactContext, listener, null));
}
public MyFrescoModule(
ReactApplicationContext reactContext,
RequestListener listener,
DiskCacheConfig diskCacheConfig) {
this(reactContext, getDefaultConfig(reactContext, listener, diskCacheConfig));
}
public MyFrescoModule(ReactApplicationContext reactContext, ImagePipelineConfig config) {
super(reactContext);
mConfig = config;
}
@Override
public void initialize() {
super.initialize();
// Make sure the SoLoaderShim is configured to use our loader for native libraries.
// This code can be removed if using Fresco from Maven rather than from source
SoLoaderShim.setHandler(new MyFrescoModule.FrescoHandler());
Context context = getReactApplicationContext().getApplicationContext();
Fresco.initialize(context, mConfig);
mConfig = null;
}
@Override
public String getName() {
return "FrescoModule";
}
@Override
public void clearSensitiveData() {
// Clear image cache.
ImagePipelineFactory imagePipelineFactory = ImagePipelineFactory();
}
private static ImagePipelineConfig getDefaultConfig(
Context context,
@Nullable RequestListener listener,
@Nullable DiskCacheConfig diskCacheConfig) {
HashSet<RequestListener> requestListeners = new HashSet<>();
HashSet<RequestListener> requestListeners = new HashSet<>();
requestListeners.add(new SystraceRequestListener());
if (listener != null) {
requestListeners.add(listener);
}
OkHttpClient okHttpClient = OkHttpClient();
ImagePipelineConfig.Builder builder =
builder
.setDownsampleEnabled(false)
.setRequestListeners(requestListeners);
if (diskCacheConfig != null) {
builder.setMainDiskCacheConfig(diskCacheConfig);
}
final int maxCacheSize= getMaxCacheSize(context);
builder.setBitmapMemoryCacheParamsSupplier(new Supplier<MemoryCacheParams>() {
@Override
public MemoryCacheParams get() {
return new MemoryCacheParams(maxCacheSize,100,0,Integer.MAX_VALUE, Integer.MAX_VALUE);
}
});
return builder.build();
}
private static int getMaxCacheSize(Context context) {
final ActivityManager activityManager=(SystemService(Context.ACTIVITY_SERVICE);
final int maxMemory =  Math.MemoryClass() * ByteConstants.MB, Integer.M
AX_VALUE);
if (maxMemory < 32 * ByteConstants.MB) {
return 4 * ByteConstants.MB;
} else if (maxMemory < 64 * ByteConstants.MB) {
return 6 * ByteConstants.MB;
} else {
// We don't want to use more ashmem on Gingerbread for now, since it doesn't respond well to
// native memory pressure (doesn't throw exceptions, crashes app, crashes phone)
if (Build.VERSION.SDK_INT < Build.VERSION_CODES.HONEYCOMB) {
return 8 * ByteConstants.MB;
} else {
return maxMemory / 4;
}
}
}
private static class FrescoHandler implements SoLoaderShim.Handler {
@Override
public void loadLibrary(String libraryName) {
SoLoader.loadLibrary(libraryName);
}
}
}
这⾥我们重新实现了MyFrescoModule,之所以不继承FrescoModule是因为FrescoModule⾥的getDefaultConfig函数是static的。
下⼀步继承MainReactPackage 替换掉MainReactPackage ,override createNativeModules⽅法将FrescoModule替换成MyFrescoModule。
最后run起来再看看您的应⽤内存是怎样的。祝您好运!
转载于:my.oschina/droidwolf/blog/759541

版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系QQ:729038198,我们将在24小时内删除。