Virtual Madness

I was going to write an article about virtual machines and performance. In particular, I intended to say that the Java VM and the Microsoft .NET virtual machines represented an extra level of code between applications and the operating system. I want my machine to run as quickly as possible, and I feel that having all these virtual machines between applications and the operating system was taking up memory and slowing my machine down. I was hoping that in the future improvements could be built into the OS rather than added on top of it in the form of a virtual machine.

However, a funny thing happened on the way to writing that article. I could explain to myself why the Java virtual machine existed: It helped provide a common platform for applications running on diverse operating systems. But trying to explain the existence of the Microsoft CLR proved more challenging, in part because I know so little about the subject. Certainly .NET code does run on multiple platforms because of the MONO project, but I didn’t feel that cross platform support was the primary reason that Microsoft created .NET. Granted, Windows CE is a distinct platform from the main Windows platform, but I’m not sure it is so radically different or radically challenging as to mandate creating a new virtual machine for use on Windows XP or Windows Server 2003.

Lost in these ruminations, and unable to complete my article, I turned to my compatriots at Falafel for enlightenment. Unexpectedly, chaos ensued. It turned out that we could agree on almost nothing regarding the CLR, including whether or not it was a virtual machine.

As explained in the previous paragraphs, this is a subject about which I know very little. My main purpose here is simply to ask for your opinion. In particular, I want to ask you, the readers of CodeFez, “Why did Microsoft build the CLR, and is it a virtual machine?”

Is the CLR a Virtual Machine?

To help you get started, you might be interested in hearing some of the discussion we had at Falafel.

There were two main points of view:

  1. That the CLR was a virtual machine

  2. That he CLR was not a virtual machine

In the spirit of full disclosure, I’ll admit that I thought the CLR was a virtual machine. However, some of my compatriots argued vehemently that it was not a virtual machine. So one question I’d like to hear from you about is whether or not you think Microsoft .NET is run on top of a virtual machine.

I define a virtual machine as a set of code that provides services similar to those offered by an operating system, but which sits between a running application and an operating system. An application that runs on top of a virtual machine needs to first locate or load the virtual machine in memory, then it can begin to request services from the virtual machine in lieu of calling the OS directly.

I see virtual machines as providing a superset of the features found in virtual memory. Virtual memory is designed to isolate a program from other programs and from the hardware for security purposes. Virtual machines also have this ability to add memory protections, but they do more than that. We had virtual memory even back in the last days of DOS, but protected mode DOS was not the same thing as a Java virtual machine. Virtual machines provide more than just memory protection; they provide a complete set of services. At any rate, that’s my view of a virtual machine. What do you think? For help, and for a variation on my point of view, you might want to read the Wiki on this subject.

A distinction needs to be made between an API, such as the Delphi VCL, and a virtual machine, such as the JVM. The Delphi VCL provides printing services, and the JVM provides printing services, but the distinction between them is that the JVM represents a complete set of services loaded into memory between you and the operating system, while an API like the VCL can be linked directly into your application, and does not necessarily provide a complete set of services equivalent to an operating system.

In some cases, the CLR is hardwired to give you fairly direct access to the operating system. This is also true of the JVM. For instance, you can write Java code that directly accesses DirectX, thereby giving your program a big boost in graphics performance. If some parts of the JVM or CLR give you direct access to the hardware, does this mean they are not virtual machines? Is it possible there is no such thing as a virtual machine?

Why Did Microsoft Create the CLR?

Assuming that you think the CLR is a virtual machine, or something like a virtual machine, the second question is why Microsoft used that architecture rather than extending the OS itself. It is probably clear to all of us that COM could be improved upon, and that .NET represents such an improvement. Most of us would also agree that .NET has other nice features, such as good type checking, garbage collection, multi-language support, heap management, etc. These features all represent potential improvements over Win32 code.

The question, of course, is not whether the CLR has good features, but why those features had to be implemented in a virtual machine. After all, Microsoft has control of the operating system. If they wanted to add a new feature to Windows such as improved type checking, multi-language support, or garbage collection, then they could have built it right in to the OS itself. And engineers have implemented all of those features without building virtual machines. So there is not, at least from my point of view, any law that dictates that type checking is not available unless there is a virtual machine. But perhaps there is something about a virtual machine that makes it possible to build better type checking, better garbage collection, better heap management than can be built without a virtual machine? If so, what is it about virtual machines that makes it possible for them to offer better type checking than you can get without a virtual machine?

Let me add one last thought that occurred to me while exploring this subject. Trying to write my original article, failingly about in the darkness, looking for answers, asking others for help, I began to wonder if Microsoft needed a way to transition from the Win32 world to a new world in which .NET features could be built directly into the OS. For now, Microsoft has to continue to support Win32, there are too many applications that run on top of it. And they couldn’t just abandon overnight all that work and set about the daunting task of rewriting all those services. So perhaps they chose the CLR as transitional step. Maybe they implemented their new improved OS at first as a virtual machine, leveraging the existing Win32 code as a starting point, and figuring that later they might build it into the OS itself? Is it possible that .NET as we know it is just a half way house on the way to a real implementation of these services inside of Windows itself?

Not everyone agreed with the idea that the CLR was simply a step in a larger process. But I am still intrigued by this idea, and so I’ll ask you if you have an opinion on this matter. Is it possible that the CLR is just a transitional phase and that eventually .NET will be built into Windows and Windows CE?

Summary

Admitting that I know almost nothing about this subject, I would like to hear your opinion. Why did Microsoft create the CLR instead of building these services directly into the OS? Is the CLR a virtual machine? Is it an execution engine? A runtime? How do you define these terms? Why do you think the CLR is or is not a virtual machine, or a runtime, or an execution engine? Do you like virtual machines? Should we build more of them? Do we need to get rid of them? What about building them into the CPU itself?