HashMap反序列化

Kon*_*rer 5 serialization android hashmap

我有一个服务器/客户端应用程序,我通过Hessian/hessdroid从服务器检索数据.HashMaps包含其他HashMaps和存储在字节数组中的图像,数据非常复杂.我可以完美地显示数据.

为了不总是查询服务器,我使用数据结构作为缓存.关闭应用程序时,我使用ObjectOutputStream将此数据对象保存到SD卡.当我重新启动它时,我用ObjectInputStream将它读回内存.

我只是在从SD卡读取数据后才遇到应用程序问题.LogCat给我以下输出(100次):

DEBUG/dalvikvm(4150): GetFieldID: unable to find field Ljava/util/HashMap;.loadFactor:F
Run Code Online (Sandbox Code Playgroud)

这在其他消息之间:

INFO/dalvikvm-heap(4150): Grow heap (frag case) to 10.775MB for 281173-byte allocation
Run Code Online (Sandbox Code Playgroud)

当堆增长~17 MB时,应用程序崩溃.

我阅读了几个关于HashMap序列化的线程,并且在架构之间进行序列化时似乎存在一个错误,但对我来说,通过Hessian进行的数据传输工作正常,并且只有在从磁盘读取HashMaps时我才会遇到所描述的问题.

有任何想法吗?

Kon*_*rer 2

正如网络僧侣评论的那样,该问题与 HashMap 反序列化没有直接关系。Android 或它的 HashMap 实现中确实存在某种错误,但我不认为它是应用程序崩溃的原因。

\n\n

现在我解决了这个问题,在应用程序中使用了更少的图像。例如,我有一个画廊,您可以在其中从一个图像滑动到下一个图像,并立即加载所有图像。当图像达到一定数量时,堆空间不足。

\n\n

我的解决方案是,不要一次保留所有解码的图像。

\n\n

它\xc2\xb4s这样做是这样的:

\n\n

1)将二进制图像数据保存在内存中(只要图像不是那么大就不是问题)

\n\n

2) 创建 Flipper 视图时,Don\xc2\xb4t 将二进制图像数据加载到 ImageViews 中。

\n\n

3)将二值图像数据设置到显示的ImageView中。

\n\n

4)保留下一个和上一个ImageView的二值图像数据,以获得更好的用户体验)

\n\n

5)通过将其资源设置为透明颜色来“卸载”未显示的ImageView。

\n\n

这里\xc2\xb4s一些代码:

\n\n
// initialize the viewFlipper by creating blank views\nfor (ComponentImageDto listElement : images) {\n    LinearLayout view = renderView();\n    flipper.addView(view);\n}\nshowImage(flipper.getCurrentView());\n
Run Code Online (Sandbox Code Playgroud)\n\n

renderView() 只是返回一个包含 ImageView 的 LinearLayout

\n\n

然后我编写了一些方法来显示下一个/上一个图像,其中我将二进制数据设置为 ImageView:

\n\n
private void showNextElement() {\n    // show next flipper view\n    flipper.showNext();\n\n    // get current view\n    int displayedChild = flipper.getDisplayedChild();\n    View currentView = flipper.getCurrentView();\n\n    // load the binary data\n    showImage(currentView);\n\n    // get the next to last view index (if keeping max. 3 images at a time in memory)\n    int otherChild = (displayedChild - 2);\n    if (otherChild < 0) {\n        otherChild = otherChild + flipper.getChildCount();\n    }\n\n    // .. and remove it\n    removeImage(flipper.getChildAt(otherChild));\n}\n\nprivate void showPreviousElement() {\n    flipper.showPrevious();\n    int displayedChild = flipper.getDisplayedChild();\n    View currentView = flipper.getCurrentView();\n    showImage(currentView);\n    setTitle((CharSequence) currentView.getTag());\n    int otherChild = (displayedChild + 2) % flipper.getChildCount();\n    removeImage(flipper.getChildAt(otherChild));\n}\n\nprivate void removeImage(View view) {\n    ImageView imageView = (ImageView) view.findViewById(R.id.gallery_image);\n    if (imageView != null) {\n        imageView.setImageResource(R.color.transparent);\n        System.gc();\n    }\n}\n\nprivate void showImage(View view) {\n    ImageView imageView = (ImageView) view.findViewById(R.id.gallery_image);\n    if (imageView != null) {\n        bm = BitmapHelper.decodeByteArray(images.get(flipper.getDisplayedChild()).getImage().getBinaryObject());\n        imageView.setImageBitmap(bm);\n    }\n}\n
Run Code Online (Sandbox Code Playgroud)\n\n

为了进一步改进内存处理,我使用了我在 stackoverflow 上找到的 BitmapHelper 类中的一些代码,这有助于通过减小图像大小来节省图像内存。

\n