基于函数调用栈优化Android App代码的方法?

发布于 2024-09-04 02:58:28 字数 251 浏览 3 评论 0原文

我听说 Android 操作系统将所有函数调用存储在堆栈中。即使程序功能正常,这也可能会导致许多问题,并导致运行时出现“打嗝”,对吗?

那么问题来了,我们怎样才能防止这种情况发生呢?明显的解决方案是减少功能化,以及其他明智的行为,例如避免过度/不必要地创建对象、对不访问字段的函数执行静态调用等……

还有其他方法吗?或者这只能通过程序员仔细编写代码来完成? JVM/JIT 是否会在编译时自动优化字节码来解决这个问题?

非常感谢您的回复!!

I've been told that Android OS stores all function calls in a stack. This can lead to many problems and cause the 'hiccups' during runtime, even if a program is functionalized properly, correct?

So the question is, how can we prevent this from happening? The obvious solution is to functionalize less, along with other sensible acts such as refraining from excessively/needlessly creating objects, performing static calls to functions that don't access fields, etc...

Is there another way though? Or can this only be done through careful code writing on the programmers' part? Does the JVM/JIT automatically optimize the bytecode during compile time to account for this??

Thanks a lot for your responses!!

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

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

发布评论

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

评论(1

淡忘如思 2024-09-11 02:58:28

我听说 Android 操作系统商店
堆栈中的所有函数调用

这几乎是所有编程语言的工作方式,并且这种情况已经持续了 30 或 40 年。

这可能会导致许多问题
导致运行时“打嗝”,
即使程序已功能化
正确,正确吗?

不会,但如果堆栈空间不足,可能会导致异常。

所以问题是,我们如何才能预防
这会发生吗?

如何防止事情发生?

堆栈空间耗尽的第一大罪魁祸首是视图层次结构太深。使用 hierarchyviewer 检查您的活动,并计算视图层次结构中从 PhoneWindow$DecorView 到最远叶节点的层数。如果达到 15 左右,则可能会耗尽堆栈空间。在这种情况下,请简化您的 UI,例如用 RelativeLayout 替换嵌套的 LinearLayouts

I've been told that Android OS stores
all function calls in a stack

This is the way pretty much all programming languages work, and that has been the case for 30 or 40 years.

This can lead to many problems and
cause the 'hiccups' during runtime,
even if a program is functionalized
properly, correct?

No, but it can lead to exceptions if you run out of stack space.

So the question is, how can we prevent
this from happening?

How can you prevent what from happening?

The #1 culprit for running out of stack space is having too deep of a view hierarchy. Use hierarchyviewer to examine your activity, and count how many levels there in your view hierarchy, from PhoneWindow$DecorView to the farthest leaf node. If you get to around 15, you are likely to run out of stack space. In that case, simplify your UI, such as replacing nested LinearLayouts with a RelativeLayout.

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