简体   繁体   English

图片占用太多堆空间

[英]Images take up too much heap space

I've come across a strange problem - I'm making an android game and noticed a lag every few seconds and checked the logs when I noticed the garbage collector was being called every few seconds for 50-150ms a go.我遇到了一个奇怪的问题 - 我正在制作一个 android 游戏,并注意到每隔几秒就会出现延迟,并在我注意到垃圾收集器每隔几秒被调用一次 50-150 毫秒时检查日志。

I've whittled this problem down to the images, when I launch my title screen activity, using one background image (150kb) - the messages i get are:当我使用一张背景图片(150kb)启动我的标题屏幕活动时,我已经将这个问题归结为图像——我得到的消息是:

4% free (8009k/8259k) GROW HEAP to 9.012MB GC_CONCURRENT freed 1kb (9178k/9479k) 4% 免费 (8009k/8259k) 将堆增长到 9.012MB GC_CONCURRENT 释放 1kb (9178k/9479k)

Which is a lot of heap space for an image and three buttons.这是一个图像和三个按钮的大量堆空间。 When I remove the image, I get no warnings or messages (not sure how to force an output on heap-space, but I hope it's at normal levels)当我删除图像时,我没有收到任何警告或消息(不确定如何在堆空间上强制输出,但我希望它处于正常水平)

The code for the activity image isn't that exciting as it's just a title screen, but:活动图像的代码并不是那么令人兴奋,因为它只是一个标题屏幕,但是:

    <ImageView
    android:id="@+id/backgroundImage1"
    android:contentDescription="Image"
    android:src="@drawable/openingscreen" 
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:layout_gravity="top"
    />

When I launch the actual game the heap size increases again (only by a few mb's) -- the weird thing is when I didn't have the title screen, the heap space for the main game was still 12 or so mb's - so I'm thinking maybe the image is holding a reference to everything else.当我启动实际游戏时,堆大小再次增加(仅增加了几 mb)——奇怪的是,当我没有标题屏幕时,主游戏的堆空间仍然是 12 mb 左右——所以我我在想,也许这张图片包含了对其他所有东西的引用。 Although I tried using a background image of 1 pixel in size and this caused no problems.虽然我尝试使用大小为 1 像素的背景图像,但这没有造成任何问题。

Secondly, the code for images inside the gameThread:其次,gameThread内部图片的代码:

mPlayer = Bitmap.createScaledBitmap(mPlayer, mCanvasWidth / 15, mCanvasHeight / 8, true);

canvas.drawBitmap(mPlayer, xPosition, yPosition, null);

I did hear somewhere that scaling bitmaps can cause memory problems, but that doesn't really add up with everything else.我确实在某处听说缩放位图会导致内存问题,但这并没有真正与其他所有事情相加。

Yes,a lot of developers while developing has faced this situation.是的,很多开发者在开发过程中都遇到过这种情况。

What i would ask you to do is a thorough study check of what exactly this heap is,how the garbage collector handles it,how can you efficiently use or even scale bitmaps我会要求你做的是彻底研究检查这个堆到底是什么,垃圾收集器如何处理它,你如何有效地使用甚至缩放位图

You could first start with going through this video Google Android Memory Management您可以先从观看此视频Google Android 内存管理开始

Next in the android developers site, go through in detail Displaying bitmaps efficiently接下来在android开发者网站,详细介绍高效显示位图

Last and final thing start a habit of using MAT( Memory analyzer Tool ),there are tutorials,lessons a lot of blogs on how to use it and finding out where and why the app is being deprived of memory最后也是最后一件事是养成使用 MAT(内存分析器工具)的习惯,有很多教程、课程,关于如何使用它以及找出应用程序被剥夺内存的位置和原因的博客

Following formular tells you how much heap space an image take: width x height x 8 bytes.以下公式告诉您图像占用多少堆空间:宽 x 高 x 8 字节。 So an image thats all white but large in size will still take a lot of heap space.所以一个全白但尺寸很大的图像仍然会占用大量的堆空间。

Just updating, I found out the main problem in my code and managed to get it running smoothly, I doubt anybody else is stupid enough to make the same mistake but I'll post my solution just in case!刚刚更新,我发现了我的代码中的主要问题并设法让它顺利运行,我怀疑其他人是否愚蠢到犯同样的错误,但我会发布我的解决方案以防万一!

I was re-sizing my images every iteration of the game, I somehow didn't realize I put the code in there, changed it so it re-sizes them at the very start and that's it.我在游戏的每次迭代中都重新调整我的图像大小,我不知何故没有意识到我把代码放在那里,改变了它,所以它在一开始就重新调整了它们的大小,就是这样。 But there it is, I doubt this solution will help anybody, but perhaps one day it will!但就是这样,我怀疑这个解决方案会帮助任何人,但也许有一天它会!

Kevin.凯文。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM