Java的虚拟机和CLR [英] Java's Virtual Machine and CLR

查看:265
本文介绍了Java的虚拟机和CLR的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

作为一种跟进呼吁的问题<一href="http://stackoverflow.com/questions/95163/differences-between-msil-and-java-byte$c$c">Differences之间MSIL和Java字节code?,什么是怎样的Java虚拟机的工作原理相对于(专业)的差异或相似性怎么 .NET框架通用语言运行时(CLR)工作?

As a sort of follow up to the question called Differences between MSIL and Java bytecode?, what is the (major) differences or similarity in how the Java Virtual Machine works versus how the .NET Framework Common Language Runtime (CLR) works?

另外,是 .NET框架 CLR的虚拟机或者它没有一个虚拟机的属性?

Also, is the .NET framework CLR a "virtual machine" or does it not have the attributes of a virtual machine?

推荐答案

有很多既实现之间的相似性(在我看来:是的,他们都是虚拟机)的。

There are a lot of similarities between both implementations (and in my opinion: yes, they're both "virtual machines").

首先,它们都是基于堆栈的虚拟机,拥有像我们看惯了现代的CPU如x86或PowerPC的寄存器的概念。所有前pressions的评价((1 + 1)/ 2)通过按下操作数上的堆,然后突然出现的那些操作数关每当指令(加,除法等),需要消耗那些堆栈执行操作数。每个指令将其结果返回到堆栈中。

For one thing, they're both stack-based VM's, with no notion of "registers" like we're used to seeing in a modern CPU like the x86 or PowerPC. The evaluation of all expressions ((1 + 1) / 2) is performed by pushing operands onto the "stack" and then popping those operands off the stack whenever an instruction (add, divide, etc) needs to consume those operands. Each instruction pushes its results back onto the stack.

这是实现虚拟机的便捷方式,因为pretty的多,在世界上每一个CPU有一个堆栈,但寄存器的数目往往是不同的(和一些寄存器是特殊用途,而每一种指令期望其在不同的寄存器,等等)。操作数

It's a convenient way to implement a virtual machine, because pretty much every CPU in the world has a stack, but the number of registers is often different (and some registers are special-purpose, and each instruction expects its operands in different registers, etc).

所以,如果你要模拟一个抽象的机器,纯粹基于堆栈的模型是pretty的路很好走。

So, if you're going to model an abstract machine, a purely stack-based model is a pretty good way to go.

当然,真正的机器不运转的方式。因此,JIT编译器负责执行字节code操作enregistration,基本上调度实际CPU寄存器包含操作数和结果只要有可能。

Of course, real machines don't operate that way. So the JIT compiler is responsible for performing "enregistration" of bytecode operations, essentially scheduling the actual CPU registers to contain operands and results whenever possible.

所以,我认为这是CLR和JVM之间最大的共同点之一。

So, I think that's one of the biggest commonalities between the CLR and the JVM.

至于差异...

两个实现之间的一个有趣的不同之处在于在CLR包括用于创建一般类型的指令,然后施加参数专业的那些类型。因此,在运行时,CLR认为一个List&LT; INT&GT;是一种完全不同的类型从列表&LT;字符串&GT;

One interesting difference between the two implementations is that the CLR includes instructions for creating generic types, and then for applying parametric specializations to those types. So, at runtime, the CLR considers a List<int> to be a completely different type from a List<String>.

在内部,它使用相同的MSIL所有引用类型专业(因此列表&LT;字符串&GT;使用相同的实现作为一个List&LT;对象&gt ;,不同类型强制转换在API边界),但每个值型采用了自己独特的实现(名单&LT; INT&GT;从名单,其中会产生完全不同的code;双&GT;)。

Under the covers, it uses the same MSIL for all reference-type specializations (so a List<String> uses the same implementation as a List<Object>, with different type-casts at the API boundaries), but each value-type uses its own unique implementation (List<int> generates completely different code from List<double>).

在Java中,泛型类型是一个纯粹的编译器把戏。 JVM有没有概念,其中类具有类型的参数,它是无法在运行时执行的参数特例。

In Java, generic types are a purely a compiler trick. The JVM has no notion of which classes have type-arguments, and it's unable to perform parametric specializations at runtime.

从实用的角度来看,这意味着你不能重载Java方法的泛型类型。你不能有两种不同的方法,使用相同的名称,只在是否接受一个名单,其中不同;字符串&GT;或List&LT;日期取代。当然,由于CLR知道有关参数类型,却没有这个问题处理方法重载的泛型类型专业。

From a practical perspective, that means you can't overload Java methods on generic types. You can't have two different methods, with the same name, differing only on whether they accept a List<String> or a List<Date>. Of course, since the CLR knows about parametric types, it has no problem handling methods overloaded on generic type specializations.

在某一天到一天的基础上,那就是我发现大部分的CLR之间的差异 JVM。

On a day-to-day basis, that's the difference that I notice most between the CLR and the JVM.

其他重要的不同之处包括:

Other important differences include:

  • 在CLR具有封闭(实现为C#代表)。在JVM不支持封锁只能从Java 8。

  • The CLR has closures (implemented as C# delegates). The JVM does support closures only since Java 8.

在CLR具有协同程序(用C#'产量'关键字来实现)。在JVM没有。

The CLR has coroutines (implemented with the C# 'yield' keyword). The JVM does not.

在CLR允许用户code来定义新的值类型(结构),而JVM提供了固定的收藏价值类型(字节,short,int和长,浮动,双,焦炭,布尔)的而只允许用户定义新的引用类型(类)。

The CLR allows user code to define new value types (structs), whereas the JVM provides a fixed collection of value types (byte, short, int, long, float, double, char, boolean) and only allows users to define new reference-types (classes).

CLR提供了声明和处理指针的支持。这是特别有趣,因为这两个JVM和CLR使用严格代压实垃圾收集器的实现作为自己的存储管理策略。在一般情况下,严格压缩GC有一个很艰难的时期与指针,因为当你从一个内存位置移动值到另一个,所有的指针(和指针的指针)的失效。但是,CLR提供了钉住机制,使开发人员可以申报code在其中CLR不允许移动某些指针块。这是非常方便的。

The CLR provides support for declaring and manipulating pointers. This is especially interesting because both the JVM and the CLR employ strict generational compacting garbage collector implementations as their memory-management strategy. Under ordinary circumstances, a strict compacting GC has a really hard time with pointers, because when you move a value from one memory location to another, all of the pointers (and pointers to pointers) become invalid. But the CLR provides a "pinning" mechanism so that developers can declare a block of code within which the CLR is not allowed to move certain pointers. It's very convenient.

的code JVM中的最大的单位或者是证明,可见此能够specifiy一个罐子中的'保护'的关键字,或可以说是一个JAR(即Java归档)一个一揽子类路径,并将它当作了code文件夹。在CLR,类被聚集成'组件',和CLR提供逻辑推理和操纵组件(其被加载到应用程序域,提供的子应用程序级沙箱存储器分配和code执行)。

The largest unit of code in the JVM is either a 'package' as evidenced by the 'protected' keyword or arguably a JAR (i.e. Java ARchive) as evidenced by being able to specifiy a jar in the classpath and have it treated like a folder of code. In the CLR, classes are aggregated into 'assemblies', and the CLR provides logic for reasoning about and manipulating assemblies (which are loaded into "AppDomains", providing sub-application-level sandboxes for memory allocation and code execution).

在CLR字节code画幅(MSIL的指令和元数据组成)具有比JVM少指令类型。在JVM中,每一个独特的操作(添加两个int值,添加两个float值等),有自己独特的指令。在CLR中,所有的MSIL指令都是多态(添加两个值)和JIT编译器是负责确定操作数的类型和创建适当的机器code。我不知道这是preferably策略,虽然。两者都有取舍。热点JIT编译器,为JVM,可以使用一个简单的code-生成机制(它并不需要确定操作的类型,因为它们已经连接在指令codeD),但是这意味着它需要一个更复杂的字节code幅面,拥有更多的指令类型。

The CLR bytecode format (composed of MSIL instructions and metadata) has fewer instruction types than the JVM. In the JVM, every unique operation (add two int values, add two float values, etc) has its own unique instruction. In the CLR, all of the MSIL instructions are polymorphic (add two values) and the JIT compiler is responsible for determining the types of the operands and creating appropriate machine code. I don't know which is the preferably strategy, though. Both have trade-offs. The HotSpot JIT compiler, for the JVM, can use a simpler code-generation mechanism (it doesn't need to determine operand types, because they're already encoded in the instruction), but that means it needs a more complex bytecode format, with more instruction types.

我一直在使用的Java(和欣赏JVM)大约十年了。

I've been using Java (and admiring the JVM) for about ten years now.

不过,在我看来,在CLR现在是优越的实施,几乎在每一个方式。

But, in my opinion, the CLR is now the superior implementation, in almost every way.

这篇关于Java的虚拟机和CLR的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆