Android 应用程序在某些手机上使用的 RAM 多于最大堆大小

发布于 2024-12-10 13:46:29 字数 1458 浏览 1 评论 0原文

我有一个应用程序可以在大多数手机(例如 Nexus **)上运行正常:内存使用量永远不会超过 24MB。 但在我的索尼爱立信Xperia Arc上,运行几天后,它使用了70MB以上的RAM(根据设置->应用程序->运行服务)。

Runtime.getRuntime().maxMemory() 表示我的手机的最大堆大小仅为 32MB。

有什么方法可以了解我的手机上的应用程序的情况吗?

这是此应用程序的 adb meminfo:

C:\>adb -d shell dumpsys meminfo 4606
Applications Memory Usage (kB):
Uptime: 409334045 Realtime: 585217200

** MEMINFO in pid 4606 [com.*****************] **
                    native   dalvik    other    total
            size:    60796    10695      N/A    71491
       allocated:    54057     6091      N/A    60148
            free:     6706     4604      N/A    11310
           (Pss):    56549     1129     8385    66063
  (shared dirty):     2128     1304     4964     8396
    (priv dirty):    56496      892     8156    65544

 Objects
           Views:        0        ViewRoots:        0
     AppContexts:        0       Activities:        0
          Assets:        3    AssetManagers:        3
   Local Binders:        5    Proxy Binders:       14
Death Recipients:      222
 OpenSSL Sockets:        2

 SQL
               heap:      425         MEMORY_USED:      425
 PAGECACHE_OVERFLOW:       81         MALLOC_SIZE:       50

 DATABASES
      pgsz     dbsz   Lookaside(b)  Dbname
         1       54            223  *.db
         1       27            198  **.db
         1        5             24  ***.db
         1       11             56  ****.db

C:\>

I have an app that runs OK on most phones(such as Nexus **): the memory usage never goes above 24MB.
But on my Sony Ericsson Xperia Arc, after running for a few days, it uses more than 70MB RAM (according to Settings->Application->Running services).

Runtime.getRuntime().maxMemory() says my phone has a max heap size of only 32MB.

Is there any way to find out what's going on with my app on this phone?

Here's the adb meminfo for this app:

C:\>adb -d shell dumpsys meminfo 4606
Applications Memory Usage (kB):
Uptime: 409334045 Realtime: 585217200

** MEMINFO in pid 4606 [com.*****************] **
                    native   dalvik    other    total
            size:    60796    10695      N/A    71491
       allocated:    54057     6091      N/A    60148
            free:     6706     4604      N/A    11310
           (Pss):    56549     1129     8385    66063
  (shared dirty):     2128     1304     4964     8396
    (priv dirty):    56496      892     8156    65544

 Objects
           Views:        0        ViewRoots:        0
     AppContexts:        0       Activities:        0
          Assets:        3    AssetManagers:        3
   Local Binders:        5    Proxy Binders:       14
Death Recipients:      222
 OpenSSL Sockets:        2

 SQL
               heap:      425         MEMORY_USED:      425
 PAGECACHE_OVERFLOW:       81         MALLOC_SIZE:       50

 DATABASES
      pgsz     dbsz   Lookaside(b)  Dbname
         1       54            223  *.db
         1       27            198  **.db
         1        5             24  ***.db
         1       11             56  ****.db

C:\>

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

梦里的微风 2024-12-17 13:46:29

本机代码的内存使用不受 Java 堆大小的限制,因此应用程序很容易超出该限制。虽然这确实意味着 Android 更有可能在后台杀死您的应用程序,但只要您不超过设备上的可用 RAM,这就不成问题。

如果您在一部手机上看到不同的内存使用情况,但在另一部手机上却没有(并且两个应用程序都以相同的方式进行交互),那么我猜测制造商在该特定手机上定制的 Android 版本存在内存泄漏。正在触发。如果是这种情况,那么您无能为力。不过,检查一下您自己的代码以确保内存泄漏不是您的错也没什么坏处。

话虽如此,在现代设备上 70MB 并不需要担心。您在大多数手机上应该是安全的,因为您的大小低于 150MB。

The memory usage of native code is not restricted by the Java heap size, so applications can easily exceed that limit. While this does mean Android is more likely to kill your application when it is in the background, it shouldn't be a problem as long as you don't exceed the available RAM on the device.

If you are seeing different memory use over time on one phone but not the other (and both apps are being interacted with in the same manner) then I would guess that the manufacturer's custom build of Android on that particular phone has a memory leak that you are triggering. If that is the case, then there isn't much you can do about it. It wouldn't hurt to look through your own code just to make sure the memory leak isn't your fault, however.

That being said, 70MB isn't anything to worry about on modern devices. You should be safe on most phones as as you stick under ~150MB.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文