我在使用GSON和Jackson解析Android中大约11MB的大型JSON时遇到了问题。问题是发生内存不足错误异常,并且堆大小也不足以完成此过程。 这是我的纸模型类
public class Paper {
public int primaryKey;
public String title;
public int entry;
public Boolean favourite;
public String comment;
public int opt;
public int score;
}
这是我的响应模型类
public class Response {
public List<Paper> papers;
}
这是我的JSON字符串
{"Paper":[[{"abstract":"Not Available","title":"A Fully Intraocular 0.0169mm<sup>2<\/sup>/pixel 512-Channel Self-Calibrating Epiretinal Prosthesis in 65nm CMOS","primaryKey":3,"entry":9,"score":-1,"comment":null,"favourite":false,"opt":1},{"abstract":"Not Available","title":"A Scalable 2.9mW 1Mb/s eTextiles Body Area Network Transceiver with Remotely Powered Sensors and Bi-Directional Data Communication","primaryKey":14,"entry":9,"score":-1,"comment":null,"favourite":false,"opt":1},{"abstract":"Not Available","title":"A 0.18µm CMOS SoC for a 100m-Range 10fps 200×96-Pixel Time-of-Flight Depth Sensor","primaryKey":20,"entry":9,"score":-1,"comment":null,"favourite":false,"opt":1},{"abstract":"Not Available","title":"A 12b 1.6GS/s 40mW DAC in 40nm CMOS with >70dB SFDR over Entire Nyquist Bandwidth","primaryKey":26,"entry":9,"score":-1,"comment":null,"favourite":false,"opt":1},{"abstract":"Not Available","title":"All-Digital Hybrid Temperature Sensor Network for Dense Thermal Monitoring","primaryKey":49,"entry":9,"score":-1,"comment":null,"favourite":false,"opt":1},{"abstract":"Not Available","title":"32Gb/s Data-Interpolator Receiver with 2-Tap DFE in 28nm CMOS","primaryKey":51,"entry":9,"score":-1,"comment":null,"favourite":false,"opt":1},{"abstract":"Not Available","title":"A 93% Efficiency Reconfigurable Switched-Capacitor DC-DC Converter Using On-Chip Ferroelectric Capacitors","primaryKey":60,"entry":9,"score":-1,"comment":null,"favourite":false,"opt":1},{"abstract":"Not Available","title":"A 45nm CMOS Near-Field Communication Radio with 0.15A/m RX Sensitivity and 4mA Current Consumption in Card Emulation Mode","primaryKey":61,"entry":9,"score":-1,"comment":null,"favourite":false,"opt":1}]]}
我不知道我在哪里犯了错误。我因为文件的缺失而无效。
答案 0 :(得分:4)
使用流解析器并在读取完整输入之前尝试在读取内容时处理内容。这样就可以避免在内存中保存完整的结构。
例如,如果您的输入JSON是一个巨大的数组,您可以按元素处理输入元素。
答案 1 :(得分:0)
您也可以尝试使用Genson,检查数据绑定api(new Genson().deserialize(json, ToClass.class)
)或直接使用流式传输API。
这取决于你想做什么。 如果你需要内存中的所有数据,那么没有太多选择,你需要增加它。 如果您可以在阅读时处理事物并且不需要它总是在内存中,那么它将与流式api一起正常工作(非常高性能且使用极少的内存)。
修改强> 溶液
您希望使用数据绑定,因此您可以减少工作量。首先看一下这个项目https://github.com/joelittlejohn/jsonschema2pojo,它允许你基于json示例生成java bean类。当你有生成的类时,如果你正在进行多个ser / deser,你只需要new Genson().deserialize(json, MyGeneratedClass.class);
,你应该重用Genson实例以获得更好的性能。
如果您不想使用1),如果结构在运行时更改(这会阻止生成类),或者您仍然遇到内存问题,请使用Gensons streaming api。 它具有内存效率,高性能且易于使用。