Android:每个意图/活动的堆大小是多少?

时间:2013-03-26 14:17:07

标签: android android-listview heap out-of-memory android-gridview

众所周知,Android应用的VM堆大小有限。 (主要是16,24,32,48或64 MB的RAM,具体取决于硬件)

我们可以用

获得实际的堆大小
ActivityManager am = (ActivityManager)this.getSystemService(ACTIVITY_SERVICE);
Toast.makeText(this, "HEAP SIZE: " + am.getMemoryClass() + "MB", 1).show();

但是,如果我打开一个新的Intent是什么内存策略???:

Intent intent = new Intent(MainActivity.this, NewActivity.class);
MainActivity.this.startActivity(intent);

此新活动是否获得完整的堆大小,旧活动是否在后台保留,其内存是否已缓存?

或意图是否获得了一个完整的新VM?

我有一个非常记忆密集的应用程序,有很多filles Grid-和ListViews。 从Android 3.0开始,Bitmaps在Heap中分配,这会导致OutOfMemory错误带来很多麻烦......我想知道我是否可以在他们自己的Intent中超出内存饥饿的视图。 < - 这有什么意义吗?

2 个答案:

答案 0 :(得分:0)

每个进程都有一个堆,无论设备指定的大小(如果在设备上安装了Google Play,最小为16 MB)。

您的应用程序是一个进程,所有应用程序的组件都必须在此堆中运行。所以你的所有活动,服务,BroadcastReceivers共享同一个堆。

当您启动新活动时,您之前的活动会被推送到后台。因此,调用它onPause(),如果需要,你应该使用该方法释放内存(删除加载的位图等)。

此外,outsorce the memory hungry Views in their own Intents没有意义,因为Intent用于启动活动和服务等应用程序组件。您不能使用具有意图的视图。相反,您应该扩展位图并仅加载所需的大小,并使用延迟加载等技术来确保内存中的位图数量不超过所需位图。

答案 1 :(得分:0)

我同意Raghav Sood。我在listview或gridview中添加了更多如何显示图像的内容。

我使用通用图片加载程序在listview中显示大量图片。

您应该在不使用时回收位图。

http://www.youtube.com/watch?v=_CruQY55HOk。谈论是关于memoy管理和内存泄漏以及如何避免它。如果遇到内存泄漏,可以使用MAT Analyzer查找内存泄漏。该视频还讨论了使用MAT Analyzer并演示如何摆脱内存泄漏。

在listview中显示图像时,需要回收视图。可见的视图不会被回收。

要在gridview或listview中显示图像,您可以使用单面图像加载器。延迟加载的改进版本。图像被缓存。您可以在本地或从服务器显示图像。

https://github.com/nostra13/Android-Universal-Image-Loader

 File cacheDir = StorageUtils.getOwnCacheDirectory(context, "your folder");

 // Get singletone instance of ImageLoader
 imageLoader = ImageLoader.getInstance();
 // Create configuration for ImageLoader (all options are optional)
 ImageLoaderConfiguration config = new ImageLoaderConfiguration.Builder(a)
      // You can pass your own memory cache implementation
     .discCache(new UnlimitedDiscCache(cacheDir)) // You can pass your own disc cache implementation
     .discCacheFileNameGenerator(new HashCodeFileNameGenerator())
     .enableLogging()
     .build();
 // Initialize ImageLoader with created configuration. Do it once.
 imageLoader.init(config);
 options = new DisplayImageOptions.Builder()
 .showStubImage(R.drawable.stub_id)//display stub image
 .cacheInMemory()
 .cacheOnDisc()
 .displayer(new RoundedBitmapDisplayer(20))
 .build();

在你的getView()

 ImageView image=(ImageView)vi.findViewById(R.id.imageview); 
 imageLoader.displayImage(imageurl, image,options);//provide imageurl, imageview and options

您可以配置其他选项以满足您的需求。

与Universal Image Loader一起,您可以查看持有者以获得流畅的滚动和性能。 http://developer.android.com/training/improving-layouts/smooth-scrolling.html

http://www.youtube.com/watch?v=wDBM6wVEO70。谈话是关于观众和表演的。