package com.xxx.xxx;
import com.google.gson.Gson;
import java.lang.ref.WeakReference;
/**
* Created by easyboot.
* on Date: 2021/8/16 Time: 9:46
*/
public class GSON {
private static WeakReference<Gson> instance;
private GSON(){
}
public static Gson get() {
if (instance == null || instance.get() == null) {
Gson gson = new Gson();
instance = new WeakReference<>(gson);
}
return instance.get();
}
}
GSON类
最新推荐文章于 2025-06-11 10:11:41 发布
这个博客展示了如何利用WeakReference创建Gson实例的单例,避免内存泄漏问题。在`GSON`类中,通过检查静态 WeakReference 是否为空来确保在需要时才初始化 Gson 对象,从而优化内存管理。
878

被折叠的 条评论
为什么被折叠?



