【发布时间】:2019-01-09 13:21:24
【问题描述】:
我正在用 for 循环中的对象编写 JSON 文件
当传输到 JSON 文件的数据数量增加时,我在 Logcat 中看到以下错误:
I/dalvikvm-heap:将堆(碎片情况)增加到 16.159MB 以获得 1490086 字节 分配
这里有什么问题?
我已经验证了Grow heap (frag case) 上的其他一些帖子,但没有结果!
是什么增加了导致此类错误的堆大小?
循环代码:
JSON_Functions_PRD mJSONF_Test = new JSON_Functions_PRD();
JSONObject obj = new JSONObject();
String Temp;
Temp = obj.toString();
mJSONF_Test.setJSONobj_PRD(Temp);
mJSONF_Test.CreateMenu_SMS_Naked();
A_DataClass m_DataClass = new A_DataClass();
int count = 10000;
for (int i = 0; i < count; i++)
{
mDataClass.setA("A1");
mDataClass.setB("B1");
mJSONF_Test.setItem_M0(mDataClass);
}
JSON 函数:
public class JSON_Functions_PRD {
String mJSONString_Result;
JSONObject obj = new JSONObject();
public void setJSONobj_PRD(String mJSONString) {
try {
mJSONString_Result = mJSONString;
obj = new JSONObject(mJSONString_Result);
} catch (JSONException e) {
e.printStackTrace();
}
}
public String getJSON_PRD() {
mJSONString_Result = obj.toString();
return mJSONString_Result;
}
public void CreateMenu_SMS_Naked() {
JSONArray jA1 = new JSONArray();
try {
obj.put("M0", jA1);
} catch (JSONException e) {
e.printStackTrace();
}
}
public void setItem_M0(A_DataClass Item) {
JSONArray jA1 = new JSONArray();
JSONObject jO1 = new JSONObject();
JSONObject jO2 = new JSONObject();
try {
jA1 = obj.getJSONArray("M0");
jO1.put("a", Item.getA());
jO2.put("a", Item.getB());
jO1.put("b", jO2);
jA1.put(jO1);
} catch (JSONException e) {
e.printStackTrace();
}
}
数据类:
public class A_DataClass {
String A;
String B;
public A_DataClass()
{
}
public String getA() {
return A;
}
public void setA(String name) {
thisA = name;
}
public String getB() {
return B;
}
public void setB(String name) {
this.B = name;
}
}
编辑:
我应该补充一点,我在 3 台手机(2 台三星,1 台华为)上测试了这个案例,问题只是在华为中看到
【问题讨论】:
标签: android heap-memory