HashMap的反序列化 [英] HashMap De-Serialization

查看:225
本文介绍了HashMap的反序列化的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一台服务器/客户端应用程序,我检索数据经由黑森州/ hessdroid服务器。数据与包含存储在字节数组其他HashMaps这样和图像HashMaps这样非常复杂的。我可以完美显示数据。

I have a server/client app where I retrieve data from the server via Hessian/hessdroid. The data is very complex with HashMaps containing other HashMaps and images stored in byte arrays. I can display the data perfectly.

要未使用的数据结构作为高速缓存总是查询服务器,I'm。这个数据对象我保存到SD卡关闭应用程序时使用ObjectOutputStream。当我重新启动它,我读回内存与ObjectInputStream的。

To not always query the server, I´m using a data structure as a cache. This data object I save to SD card using ObjectOutputStream when closing the app. When I restart it, I read it back to memory with an ObjectInputStream.

后,才从SD卡读取数据有问题的应用程序I'm。 LogCat中给了我下面的输出(100次):

I´m having problems with the app only after reading the data from SD card. LogCat gives me the following output (100 times):

DEBUG/dalvikvm(4150): GetFieldID: unable to find field Ljava/util/HashMap;.loadFactor:F

这在其他消息的:

and this in between the other messages:

INFO/dalvikvm-heap(4150): Grow heap (frag case) to 10.775MB for 281173-byte allocation

在堆中长大后〜17 MB的应用程序崩溃。

When the heap grows upon ~17 MB the app crashes.

我读到的HashMap序列化多个线程,而且似乎架构之间序列化时,是一个错误,但对我来说,通过黑森州数据传送完美的作品,并具有所描述的问题I'm只能从磁盘读取HashMaps这样的时候。

I read several threads about HashMap Serialization and that there seems to be a bug when serializing between architectures, but for me the data transfer via Hessian works perfectly and I´m having the described problems only when reading the HashMaps from disk.

任何想法?

推荐答案

问题是没有直接关系的HashMap的反序列化,作为网络僧评价道。有确实是在Android的某种缺陷或it's HashMap的实现,但我不认为it's为什么应用程序崩溃。

The problem is not directly related to the HashMap Deserialization, as cyber-monk commented. There is indeed some kind of bug in Android or it´s HashMap implementation, but I don´t think it´s why the app crashes.

现在我解决了这个问题,在应用程序中使用较少的图像。我有一个画廊例如,你可以从一个图像刷卡到下一个鳍状肢,并加载所有的图像一次。在一定量的图像,没有足够的空间堆

By now I solved the problem, using less images in the app. I had a gallery for example in which you could swipe from one image to the next in a flipper and loaded all the images at once. At a certain amount of images, there is not enough heap space.

我的解决方法是,不要让所​​有的德codeD映像一次。

My solution to this is, to not keep all the decoded images at once.

It's做过这样的:

It´s done like this:

1)保留在内存中的二进制图像数据(而不是一个问题,只要图像没有那么大)

1) Hold the binary image data in memory (not a problem as long as the images are not that big)

2)鸵鸟政策装载二进制图象数据到ImageViews创建挡板的意见时

2) Don´t load the binary image data into the ImageViews when creating the views of the flipper.

3)设定的二进制图象数据到所显示的ImageView的

3) Set the binary image data into the ImageView that is displayed.

4)保持的下一个和最后一个ImageView的更好的用户体验)

4) Keep the binary image data of the next and the last ImageView for better user experience)

5)卸载不是由它的资源设置为透明颜色显示的ImageViews

5) "Unload" the ImageViews that are not displayed by setting its resource to the transparent color.

Here's约code:

Here´s some code:

// initialize the viewFlipper by creating blank views
for (ComponentImageDto listElement : images) {
    LinearLayout view = renderView();
    flipper.addView(view);
}
showImage(flipper.getCurrentView());

的RenderView()刚刚返回包含ImageView的一个LinearLayout中

renderView() just returns a LinearLayout containing an ImageView

然后我写了一些方法,以表示我设置的二进制数据ImageView的下一个/ previous图像:

Then I wrote some methods to show the next/previous image in which I set the binary data to the ImageView:

private void showNextElement() {
    // show next flipper view
    flipper.showNext();

    // get current view
    int displayedChild = flipper.getDisplayedChild();
    View currentView = flipper.getCurrentView();

    // load the binary data
    showImage(currentView);

    // get the next to last view index (if keeping max. 3 images at a time in memory)
    int otherChild = (displayedChild - 2);
    if (otherChild < 0) {
        otherChild = otherChild + flipper.getChildCount();
    }

    // .. and remove it
    removeImage(flipper.getChildAt(otherChild));
}

private void showPreviousElement() {
    flipper.showPrevious();
    int displayedChild = flipper.getDisplayedChild();
    View currentView = flipper.getCurrentView();
    showImage(currentView);
    setTitle((CharSequence) currentView.getTag());
    int otherChild = (displayedChild + 2) % flipper.getChildCount();
    removeImage(flipper.getChildAt(otherChild));
}

private void removeImage(View view) {
    ImageView imageView = (ImageView) view.findViewById(R.id.gallery_image);
    if (imageView != null) {
        imageView.setImageResource(R.color.transparent);
        System.gc();
    }
}

private void showImage(View view) {
    ImageView imageView = (ImageView) view.findViewById(R.id.gallery_image);
    if (imageView != null) {
        bm = BitmapHelper.decodeByteArray(images.get(flipper.getDisplayedChild()).getImage().getBinaryObject());
        imageView.setImageBitmap(bm);
    }
}

要furtherly改进的内存处理I'm使用一些code在BitmapHelper类,我发现计算器,有助于减少它们的大小,以节省内存的图像。

To furtherly improve memory handling I´m using some code in BitmapHelper class that I found on stackoverflow, that helps to save memory for images by reducing their size.

这篇关于HashMap的反序列化的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆