English 中文(简体)
Json () 的 gson. from Json () gson (gson) 中的内存例外
原标题:Out of memory exception in gson.fromJson()

将 Json 字符串( strWebserviceResult) 转换为我的对象时, 我使用以下代码 :

EntMyClass entMyClass = gson.fromJson(strWebserviceResult,EntMyClass.class);

当 < 坚固> strWebserviceResult 很大( 约2.5 MB) 时, 我得到这条线上的“ 坚固” 排除记忆

我怎样才能解决呢?

有人有什么建议吗?

05-26 15:52:49.607: E/dalvikvm-heap(2078): Out of memory on a 9200-byte allocation.
05-26 15:52:49.618: E/dalvikvm(2078): Out of memory: Heap Size=31879KB, Allocated=27693KB, Bitmap Size=936KB, Limit=32768KB
05-26 15:52:49.618: E/dalvikvm(2078): Extra info: Footprint=31879KB, Allowed Footprint=31879KB, Trimmed=7400KB
05-26 15:52:49.618: E/AndroidRuntime(2078): FATAL EXCEPTION: Thread-19
05-26 15:52:49.618: E/AndroidRuntime(2078): java.lang.OutOfMemoryError: (Heap Size=31879KB, Allocated=27693KB, Bitmap Size=936KB)
05-26 15:52:49.618: E/AndroidRuntime(2078):     at java.util.ArrayList.add(ArrayList.java:123)
05-26 15:52:49.618: E/AndroidRuntime(2078):     at com.google.gson.DefaultTypeAdapters$CollectionTypeAdapter.deserialize(DefaultTypeAdapters.java:664)
05-26 15:52:49.618: E/AndroidRuntime(2078):     at com.google.gson.DefaultTypeAdapters$CollectionTypeAdapter.deserialize(DefaultTypeAdapters.java:624)
05-26 15:52:49.618: E/AndroidRuntime(2078):     at com.google.gson.JsonDeserializerExceptionWrapper.deserialize(JsonDeserializerExceptionWrapper.java:51)
05-26 15:52:49.618: E/AndroidRuntime(2078):     at com.google.gson.JsonDeserializationVisitor.invokeCustomDeserializer(JsonDeserializationVisitor.java:92)
05-26 15:52:49.618: E/AndroidRuntime(2078):     at com.google.gson.JsonObjectDeserializationVisitor.visitFieldUsingCustomHandler(JsonObjectDeserializationVisitor.java:117)
05-26 15:52:49.618: E/AndroidRuntime(2078):     at com.google.gson.ReflectingFieldNavigator.visitFieldsReflectively(ReflectingFieldNavigator.java:63)
05-26 15:52:49.618: E/AndroidRuntime(2078):     at com.google.gson.ObjectNavigator.accept(ObjectNavigator.java:120)
05-26 15:52:49.618: E/AndroidRuntime(2078):     at com.google.gson.JsonDeserializationContextDefault.fromJsonObject(JsonDeserializationContextDefault.java:76)
05-26 15:52:49.618: E/AndroidRuntime(2078):     at com.google.gson.JsonDeserializationContextDefault.deserialize(JsonDeserializationContextDefault.java:54)
05-26 15:52:49.618: E/AndroidRuntime(2078):     at com.google.gson.DefaultTypeAdapters$CollectionTypeAdapter.deserialize(DefaultTypeAdapters.java:663)
05-26 15:52:49.618: E/AndroidRuntime(2078):     at com.google.gson.DefaultTypeAdapters$CollectionTypeAdapter.deserialize(DefaultTypeAdapters.java:624)
05-26 15:52:49.618: E/AndroidRuntime(2078):     at com.google.gson.JsonDeserializerExceptionWrapper.deserialize(JsonDeserializerExceptionWrapper.java:51)
05-26 15:52:49.618: E/AndroidRuntime(2078):     at com.google.gson.JsonDeserializationVisitor.invokeCustomDeserializer(JsonDeserializationVisitor.java:92)
05-26 15:52:49.618: E/AndroidRuntime(2078):     at com.google.gson.JsonDeserializationVisitor.visitUsingCustomHandler(JsonDeserializationVisitor.java:80)
05-26 15:52:49.618: E/AndroidRuntime(2078):     at com.google.gson.ObjectNavigator.accept(ObjectNavigator.java:101)
05-26 15:52:49.618: E/AndroidRuntime(2078):     at com.google.gson.JsonDeserializationContextDefault.fromJsonArray(JsonDeserializationContextDefault.java:67)
05-26 15:52:49.618: E/AndroidRuntime(2078):     at com.google.gson.JsonDeserializationContextDefault.deserialize(JsonDeserializationContextDefault.java:52)
05-26 15:52:49.618: E/AndroidRuntime(2078):     at com.google.gson.Gson.fromJson(Gson.java:551)
05-26 15:52:49.618: E/AndroidRuntime(2078):     at com.google.gson.Gson.fromJson(Gson.java:498)
05-26 15:52:49.618: E/AndroidRuntime(2078):     at com.google.gson.Gson.fromJson(Gson.java:467)
05-26 15:52:49.618: E/AndroidRuntime(2078):     at com.google.gson.Gson.fromJson(Gson.java:417)
05-26 15:52:49.618: E/AndroidRuntime(2078):     at com.google.gson.Gson.fromJson(Gson.java:389)
05-26 15:52:49.618: E/AndroidRuntime(2078):     at org.mabna.order.businessLayer.BoWebService.getDataForUpdate(BoWebService.java:188)
05-26 15:52:49.618: E/AndroidRuntime(2078):     at org.mabna.order.ui.ActToolDataExchange.threadGetDataForFullUpdate(ActToolDataExchange.java:371)
05-26 15:52:49.618: E/AndroidRuntime(2078):     at org.mabna.order.ui.ActToolDataExchange.access$9(ActToolDataExchange.java:362)
05-26 15:52:49.618: E/AndroidRuntime(2078):     at org.mabna.order.ui.ActToolDataExchange$33.run(ActToolDataExchange.java:603)
05-26 15:52:49.618: E/AndroidRuntime(2078):     at org.mabna.order.utils.Utilities$5.run(Utilities.java:778)
问题回答

尝试使用"http://google-gson.googlecode.com/svn/trunk/gson/docs/javadocs/com/google/gson/Gson.html#来自Json%28com.google.gson.strong.JsonReader,%20java.lang.refect.Type%29" rel=“norefererr” >来自Json 的方法,该方法取JsonReader作为输入。这应该允许您一次不需要整个输入字符串在记忆中。 这里有一些样本代码 :

        final HttpURLConnection c = (HttpURLConnection) url.openConnection();
        final InputStreamReader isr = new InputStreamReader(c.getInputStream());
        final JsonReader reader = new JsonReader(isr);
        final EntMyClass entMyClass = GSON.fromJson(reader, EntMyClass.class);
        reader.close();
        c.disconnect();

与其它文章一样, 我将询问是否有办法可以避免您的应用程序使用大型内存。 如果您可以这样做, 这将是最理想的解决方案。 如果您的应用程序真的需要这么多内存, 您可以尝试设置 < code> and mid: largeHeap=“ true” 用于您在列表中的应用程序 。 以下是引用 :

< a href="http:// developmenter.android.com/ reference/android/R.stingable.html#android Manifest Application_ largeHeap" rel="noreferr" >http:// developinger.android.com/ reference/android/R. styleable.html#android Manifest Application_ largeheap

详情请见此影片:

http://www.youtube.com/watch?vCruQY55Hok" rel=“noreferrer>>http://www.youtube.com/watch?vCruQY55Hok

First of all. Do you really need to load 2.5MB data into memory?
At first there is created a big string, then it s parsed (another objects in memory), and then there is created a huge instance of EntMyClass. This approach is really inefficient.

我猜您在此对象中有列表。 只有您已经提供的信息, 我建议将数据输入数据库。 然后您可以创建适应器, 在 ListView 中显示数据 。

有许多方法可以做到这一点。我使用杰克逊图书馆,因为图书馆是最快的,GSON应该有类似的功能。

假设您在下面的此对象中有列表,请举例说明如何在数据库中存储 json 阵列:

    //create parser
    final JsonParser jsonParser = objectMapper.getJsonFactory().createJsonParser(inputStream);

    JsonToken jsonToken = jsonParser.nextToken();
    while ((jsonToken = jsonParser.nextToken()) != null && jsonToken != JsonToken.END_ARRAY) {
        //map json object to ItemClass
        final ItemClass item = jsonParser.readValueAs(ItemClass.class);
        //store in database
        itemDAO.insert(item);
    }

围绕着数据库交易, 不仅数据在出错时会回落, 而且效率也会提高。

请读读Gson-的作者的回答“https://stackoverflow.com/a/9460267/394678>这里

还有我的5美分:

        JsonReader r = null;
        try {
            Reader reader = new BufferedReader(new InputStreamReader(is));
            r = new JsonReader(reader);
            result = gson.fromJson(r, clazz);
        } finally {
            if (null != r) {
                r.close();
            }
        }




相关问题
Android - ListView fling gesture triggers context menu

I m relatively new to Android development. I m developing an app with a ListView. I ve followed the info in #1338475 and have my app recognizing the fling gesture, but after the gesture is complete, ...

AsyncTask and error handling on Android

I m converting my code from using Handler to AsyncTask. The latter is great at what it does - asynchronous updates and handling of results in the main UI thread. What s unclear to me is how to handle ...

Android intent filter for a particular file extension?

I want to be able to download a file with a particular extension from the net, and have it passed to my application to deal with it, but I haven t been able to figure out the intent filter. The ...

Android & Web: What is the equivalent style for the web?

I am quite impressed by the workflow I follow when developing Android applications: Define a layout in an xml file and then write all the code in a code-behind style. Is there an equivalent style for ...

TiledLayer equivalent in Android [duplicate]

To draw landscapes, backgrounds with patterns etc, we used TiledLayer in J2ME. Is there an android counterpart for that. Does android provide an option to set such tiled patterns in the layout XML?

Using Repo with Msysgit

When following the Android Open Source Project instructions on installing repo for use with Git, after running the repo init command, I run into this error: /c/Users/Andrew Rabon/bin/repo: line ...

Android "single top" launch mode and onNewIntent method

I read in the Android documentation that by setting my Activity s launchMode property to singleTop OR by adding the FLAG_ACTIVITY_SINGLE_TOP flag to my Intent, that calling startActivity(intent) would ...

From Web Development to Android Development

I have pretty good skills in PHP , Mysql and Javascript for a junior developer. If I wanted to try my hand as Android Development do you think I might find it tough ? Also what new languages would I ...

热门标签