基于函数调用栈优化Android App代码的方法?
我听说 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
这几乎是所有编程语言的工作方式,并且这种情况已经持续了 30 或 40 年。
不会,但如果堆栈空间不足,可能会导致异常。
如何防止事情发生?
堆栈空间耗尽的第一大罪魁祸首是视图层次结构太深。使用 hierarchyviewer 检查您的活动,并计算视图层次结构中从
PhoneWindow$DecorView
到最远叶节点的层数。如果达到 15 左右,则可能会耗尽堆栈空间。在这种情况下,请简化您的 UI,例如用RelativeLayout
替换嵌套的LinearLayouts
。This is the way pretty much all programming languages work, and that has been the case for 30 or 40 years.
No, but it can lead to exceptions if you run out of stack space.
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, fromPhoneWindow$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 nestedLinearLayouts
with aRelativeLayout
.