1490086 字节的 Android 错误 : I/dalvikvm-heap: Grow heap (frag case) to 16. 159MB

标签 android heap-memory

我正在用 for 循环中的对象编写一个 JSON 文件
当传输到 JSON 文件的数据量增加时,我在 Logcat 中看到以下错误:

I/dalvikvm-heap: Grow heap (frag case) to 16.159MB for 1490086-byte allocation

这里有什么问题? 我已经验证了关于 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 台华为)上测试了这个案例,问题只出现在华为

最佳答案

堆可能有很多可用内存,但 fragment 化为小块。如果 VM 找不到足够大的 block 来进行您尝试进行的分配,则会发生 OOME。

关于1490086 字节的 Android 错误 : I/dalvikvm-heap: Grow heap (frag case) to 16. 159MB,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54111134/

相关文章:

java - 关闭应用程序后服务未运行

android - 从 Jetpack Compose 生成的屏幕外布局创建位图

java - JVM 内存中特定对象的子树的大小

c - 堆内存澄清

android - Android 上的蓝牙 : StartDiscovery not working. 无法扫描设备

android - Windows 上的 Hello-Jni,Android NDK - build-local.mk : No such file

android - 在 Windows 操作系统的 Cygwin 下为 Android 构建 FFMPEG

java - OutOfMemoryError : Java Heap Space. 如何修复递归方法中发生的此错误?

c - 影响其他程序的堆溢出

c - 对于指向数组的指针,realloc 失败(在 C 中)