2

启动我的应用程序时,堆大约为 15M。在跟注后setContentViewMainActivity它加注到大约 30M。这种布局没有太多内容。这就是为什么我想知道这是否正常。当开始另一个 Acticity 时,它再次上升到大约 40M 并且不会再次下降。

main.xml看起来是这样的:

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:id="@+id/LinearLayout1"
    android:background="@drawable/background"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:gravity="center"
    android:orientation="vertical" >

    <LinearLayout
        android:id="@+id/LinLAdMain"
        android:layout_width="match_parent"
        android:layout_height="88dp"
        android:layout_weight="0.70"
        android:orientation="vertical" >
    </LinearLayout>

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_weight="0.70"
        android:orientation="vertical" >

        <ImageView
            android:id="@+id/imageView1"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_gravity="top"
            android:contentDescription="@string/logo_description"
            android:src="@drawable/logo" />

    </LinearLayout>

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_gravity="center"
        android:layout_weight="0.14"
        android:orientation="vertical" >

        <Button
            android:id="@+id/button1"
            style="@style/StartButton"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_gravity="center"
            android:onClick="onClick"
            android:layout_margin="8dp" />

        <Button
            android:id="@+id/button2"
            style="@style/HighscoreButton"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_gravity="center"
            android:onClick="onClick"
            android:layout_margin="8dp" />

        <Button
            android:id="@+id/button3"
            style="@style/SettingsButton"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_gravity="center"
            android:onClick="onClick"
            android:layout_margin="8dp" />

    </LinearLayout>
</LinearLayout>

如您所见,我正在使用背景可绘制对象。但是文件大约有60K大..

这就是 MAT-default-report 紧随其后的样子setContentView()

截屏

这就是 GC 的路径(不包括弱引用):

截屏 截屏

这些“下一个”以头类 java.lang.ref.FinalizerReference 结尾,它什么也没告诉我。

正如我所说,每次活动都会变得更糟。例如,在 Main->Highscore->GlobalHighscore 之后,就不能再启动 Activity“游戏”了。希望你们能帮助我:)。

如果您需要更多信息/代码,请告诉我。

4

2 回答 2

2

PNG 和 JPG 等图像文件格式正在使用压缩算法来减少使用的磁盘空间。但是,当图像在 android 应用程序中加载时,这些文件会被解压缩,并且可能会比原始文件大得多。

解压后使用的空间类似于将 PNG 或 JPG 文件转换为 BMP 32 位颜色时使用的磁盘空间。

一个例子

一个非常简单的全白绘图,大小为 1024 x 1024 像素,另存为 PNG 将使用几个 Kb。

解压成 ARGB_8888 后,它变成一个 1024 x 1024 x 4 字节的数组(最后 4 个代表每个像素使用的 4 个字节,每个基本颜色 RGB 一个,一个用于 Alpha)

1024 x 1024 x 4 = 4,194,304 = 4 MB

所以这个简单图像使用的内存为 4 MB。

该怎么办

减少图像使用的内存:

  • 使用仍然满足您要支持的不同屏幕尺寸要求的最小尺寸(宽度和高度)
  • 为不同的屏幕使用不同的尺寸(mdpi、hdpi 等)。但是,这会增加您的应用程序大小。
  • 使用 Draw 9 Patch 创建小尺寸图像,这些图像将以可预测和可控的方式扩展到大屏幕。

找出导致内存分配增加的内存泄漏。

可能您正在使用对Context阻止 GC 收集内存的活动的引用。最可能的原因是引用了 Context/Activity 的对象:

  • 线程
  • 处理程序
  • 意见

在不再需要时或在活动中尝试将它们归零OnDestroy()

问候。

于 2012-12-07T23:31:54.767 回答
0

获取android:src="@drawable/logo"并手动调整大小以适合您的屏幕,需要。我的意思是可绘制文件夹中的 logo.png。在 320x480 分辨率下,它不需要 2560x1600 像素!

于 2012-12-07T23:00:17.407 回答