java 在 Sun VM 中可以在 Dalvik VM(Android 的 VM)上做什么?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/230193/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
What can you not do on the Dalvik VM (Android's VM) that you can in Sun VM?
提问by Ichorus
I know that you can run almost all Java in Dalvik's VMthat you can in Java's VMbut the limitations are not very clear. Has anyone run into any major stumbling blocks? Any major libraries having trouble? Any languages that compile to Java byte code (Scala, Jythonetc...) not work as expected?
我知道你可以在Dalvik 的 VM中运行几乎所有的 Java ,你可以在Java 的 VM 中运行,但限制不是很清楚。有没有人遇到任何主要的绊脚石?有没有遇到问题的主要图书馆?任何编译为 Java 字节码(Scala、Jython等)的语言都不能按预期工作?
采纳答案by Marcin
There is a number of things that Dalvik will not handle or will not handle quite the same way as standard Java bytecode, though most of them are quite advanced.
有很多东西 Dalvik 不会或不会像标准 Java 字节码那样处理,尽管它们中的大多数都非常先进。
The most severe example is runtime bytecode generationand custom class loading. Let's say you would like to create some bytecode and then use classloader to load it for you, if that trick works on your normal machine, it is guaranteed to not work on Dalvik, unless you change your bytecode generation.
在最严重的例子是运行时字节码生成和自定义类加载。假设您想创建一些字节码,然后使用类加载器为您加载它,如果该技巧在您的普通机器上有效,则保证在 Dalvik 上不起作用,除非您更改字节码生成。
That prevents you from using certain dependency injection frameworks, most known example being Google Guice (though I am sure some people work on that). On the other hand AspectJ should work as it uses bytecode instrumentation as a compilation step (though I dunno if anyone tried).
这会阻止您使用某些依赖项注入框架,最著名的例子是 Google Guice(尽管我相信有些人会这样做)。另一方面,AspectJ 应该可以工作,因为它使用字节码检测作为编译步骤(尽管我不知道是否有人尝试过)。
As to other jvm languages -- anything that in the end compiles to standard bytecode and does not use bytecode instrumentation at runtime can be converted to Dalvik and should work. I know people did run Jython on Android and it worked ok.
至于其他 jvm 语言——任何最终编译为标准字节码并且在运行时不使用字节码检测的东西都可以转换为 Dalvik 并且应该可以工作。我知道人们确实在 Android 上运行了 Jython,并且运行良好。
Other thing to be aware of is that there is no just in time compilation. This is not strictly Dalviks problem (you can always compile any bytecode on the fly if you wish) but that Android does not support that and is unlikely to do so. In the effect while microbenchmarking for standard Java was useless -- components had different runtime characterstics in tests than as parts of larger systems -- microbenchmarks for Android phones totally make sense.
需要注意的另一件事是没有及时编译。这不是严格的 Dalvik 问题(如果您愿意,您可以随时编译任何字节码),但 Android 不支持并且不太可能这样做。虽然标准 Java 的微基准测试毫无用处——组件在测试中的运行时特征与作为大型系统的一部分不同——但 Android 手机的微基准测试完全有意义。
回答by Wonil
If you see "Dalvik Virtual Machine internals" Google IO session, you can find Dalvik does not support generational GC.
如果你看到“ Dalvik Virtual Machine internals”Google IO session,你会发现Dalvik不支持分代GC。
So, it could degrade performance of frequent object creation and deletion. Java VM supports generational GC so, it would show better GC performance for the same situation.
因此,它可能会降低频繁创建和删除对象的性能。Java VM 支持分代 GC,因此在相同情况下它会显示出更好的 GC 性能。
And also, Dalvik uses trace-granuality JITinstead of method granuality JIT.
而且,Dalvik 使用跟踪粒度 JIT而不是方法粒度 JIT。
回答by Wilfred Springer
Another thing that I guess could be added here is that Dalvik apparently does not preserve field order when listing the fields of a class using the reflection API. Now, the reflection API does not make any guarantees on it anyway (so ideally you shouldn't depend on it anyway), but most of the other VMs out there dopreserve the order.
我想可以在这里添加的另一件事是,在使用反射 API 列出类的字段时,Dalvik 显然没有保留字段顺序。现在,反射 API 无论如何都不会对其做出任何保证(因此理想情况下您无论如何都不应该依赖它),但是那里的大多数其他 VM确实保留了顺序。
回答by Sean Walsh
Just to add to the conversation, not intended to revive an old thread. I just ran across this in my search, and want to add that Jython does not work out of the box with Dalvik either. Simply trying to do a hello world example will yield the following:
只是为了添加到对话中,而不是为了恢复旧线程。我刚刚在搜索中遇到了这个问题,并想补充一点,Jython 也不能与 Dalvik 一起使用。简单地尝试做一个 hello world 示例将产生以下结果:

