Java如何通过内存对象堆分配在Android中工作
我是 Android 的新手,但是具有 Java 的经验. 在 Java 中,当我们这样做时:
I am new to Android but experienced in Java. In Java when we do this:
MyClass myObj = new MyClass();
它显然会执行以下操作:
It clearly does the following:
- 将内存指针
myObj
插入堆栈 - 为堆中类型为
MyClass
的对象分配新空间 - 指定此新空间对
myObj
的引用
- Inserts the memory pointer
myObj
upto the stack - Allocates a new space for object of type
MyClass
in the heap - Appoints this new space's reference to
myObj
但是我对以下问题有些困惑:
But I am a little bit confused about the following questions:
- 但是在 Android 中,这是否以相同的方式工作?
- Android 是否具有完整的堆栈和堆内存?
- Android 是否可以在我的 Java 应用( Android 应用)上使用Java虚拟机(JVM)?
- But in Android, does this work in the same way?
- Does Android have full stack and heap memories?
- Does Android have Java Virtual Machine (JVM) for my Java app (Android app) to work on?
非常感谢!
Android使用自己的Dalvik虚拟机重新实现了Java虚拟机.与基于堆栈的 JVM 不同, Dalvik 是基于寄存器的.您可以在此处看到两者之间的比较:
Android re-implemented the Java Virtual Machine with their own Dalvik Virtual Machine. Unlike the JVM which is stack-based, Dalvik is register based. You can see a comparison between the two here:
http://en.wikipedia.org/wiki/Dalvik_(软件) #性能
是的,Dalvik VM具有与JVM一样的堆-而不是堆栈.它与任何Java 1.6源代码都100%兼容(对1.7的支持正在慢慢出现,并且在Android Studio中提供了初步的支持).因此,在您的3个步骤中:
Yes Dalvik VM has a heap just like the JVM - just not a stack. It is 100% compatible with any Java 1.6 source code (1.7 support is slowly coming, and preliminary support is available in Android Studio). So in your 3 steps:
- 将对myObj的引用插入寄存器中
- 在堆中为MyClass类型的对象分配一个新空间
- 指定此新空间对myObj的引用