View this PageEdit this PageAttachments to this PageHistory of this PageHomeRecent ChangesSearch the SwikiHelp Guide
Hotspots: Admin Pages | Turn-in Site |
Current Links: Cases Final Project Summer 2007

Summer 2003 Final Exam Review: Virtual Machines

Virtual Machines



a. Give three reasons that one might use a virtual machine (and just-in-time compiling to native code) instead of a compiler to compile directly to machine code.

1) It is better for object-oriented because objects are very complex and VM handles initialization. It supports message sending and not function calling. 2) VM's allow for portability, binary compatibility between platforms. 3) VM's allow memory handling so that programmers don't have to explicitly allocate and deallocate memory. VM also provides garbage collection. ~Sabina Karkin
Good answers Sabina, anymore anyone? Barbara Ericson

Using a virtual machine and compiling to native code are two different things. You're using a compiler to compile to machine code in either case. (3:) There is nothing saying that VM's have to do garbage collection. (1:) This answer isn't really on-target either... timmy

When using a virtual machine you can either do just-in-time compiliation or do an interpreter. If the platform that you want to run on has a just-in-time compilier you can use it since they are usually faster but that doesn't mean you have to use it on all platforms. One reason for using a VM is that it can do garbage collection and in Squeak and Java's VMs do provide garbage collection. Barbara Ericson

There are more reasons for using a VM. Anybody else want to add some? Barbara Ericson

Security and faster startup are also reasons for using a VM. ~Sabina Karkin

faster startup? timmy

as a side-effect of them being more memory efficient (and of course this isn't necessarily so). A more important advantage of this is that it makes VM's suitable for low-power, low-memory consumer devices. Michael Mitchell

Good answer Michael Barbara Ericson


b. Give a good reason why you might add a primitive to a VM.

You might add a primitive (code inside the VM interpreter) in VM for handling arithmetic manipulations, storage management, for control, and for input-output. ~Sabina Karkin

Good answers Sabina, anymore anyone? Barbara Ericson

Another reason to add primitives might be for increased speed. Jason Williams

Yes, Jason, speed is a major reason for adding a primitive. Barbara Ericson

c. How are the Java and Squeak VM's similar. How are they different?

Similarities: They are both stack-based. They both execute bytecodes. They both have garbage collection. Differences: Java VM bytecodes are typed and have more operands. Java VM makes threads more significant (stacks are associated with threads, not with method contexts). With Java, threads/processes are all in the VM, whereas with Squeak, they are done with Squeak and primitives. Java handles exceptions within VM (faster but more complex), whereas Squeak handles exceptions within Squeak (flexible-able to continue, easier to port, but slower). It's harder to port to different platforms with Java VM. Java VM guarantees a level of security. ~Sabina Karkin

Nice answers Sabina Barbara Ericson

d. What is one algorithm that is used for garbage collection?

One algorithm is reference-counting where you keep a count of all references to an object. When it's zero, you remove the object. Downside is problems with cycles and it can defer with things on stack because every store to object memory requires incrementing/decrementing. ~Sabina Karkin

Good answers Sabina. There are more algorithms. Anyone else care to try it? Barbara Ericson

mark-and-sweep, generational, and uhh maybe copy-and-(sweep?). Maybe you'll ask us to explain them on the test? timmy

Yes, explaining would be good. Barbara Ericson

mark-and-sweep is an algorithm that is usually invoked at specific times, either manually, or when the garbage collectory decides it needs to clean up. This means you usually stop what the program is doing and then do the garbage collection. It works first by marking every object that is visible to the program. This can be recursively done and it works with cyclic data structures unlike ref counting. After the visible objects are marked, we sweep by going through all the allocated data and freeing stuff that isn't marked. Then we go back to the execution of the program.

generational garbage collection doesn't have such a set algorithm, but it relies on the fact that most new objects created won't last as long as the older ones. So it focuses on new memory and can deallocate that when it isn't visible anymore. The thing to watch out for here is when old objects somehow manage to point to newly create objects. But that doesn't happen all the time. When an object survives GC, I would guess that everything alive would be 'pushed back a generation'.

The copy or copy-and-sweep thing I said above is basically like mark-and-sweep except that it copys all the data that would be marked to a new location and then frees the old location. This can save space because it's sort of like defragmenting the data as it is reallocated in the new region. You would have to update the pointers so that they don't point to the old region. I guess the downside here is that copying is a pretty slow/intensive operation and that is only going to be multiplied by the fact that you have to stop execution to do this.

timmy




e. Is it a good idea to use a VM when doing 3D graphics? When doing applications that you want to be secure?

It is not a good idea to use a VM when doing 3D graphics because it is not able to handle 3D. It is a good idea to use a VM when doing applications that you want to be secure because VM generates a good level of security. ~Sabina Karkin
Why isn't the VM able to handle 3D graphics? Barbara Ericson
VM's in theory can handle 3D graphics; there are speed issues, however, that make it more viable to do 3D graphics in a lower-level language like C or C++. Jason Williams

VMs generally don't optimize for a particular hardware feature. Same goes for many libraries. timmy

Right, timmy, virtual machines don't take advantage of hardware features since they are supposed to be easy to port to other machines. Barbara Ericson


f. What takes up the majority of code (and probably execution time) in a primitive written for a virtual machine?

Defining the Plugin code takes up most of the code. (I'm not really sure about this question.) ~Sabina Karkin

No, I am looking for what takes the most code and execution time in the code for the primitive. Barbara Ericson

depends on the primitive...handling types and transferring data is probably one of the bigger annoyances timmy

What is it that we have to do with types in going from OO to primitives? Barbara Ericson

convert them. The way a language or implementation holds non-primitive data is implementation dependant so there is no standard way. Sometimes the conversion is referred to boxing and unboxing. timmy
Right, timmy, in fast primitives much of the code and execution time goes into boxing and unboxing (converting from the object type to the primtive type. For example, Strings in Java are not arrays of characters as they are in C so a conversion has to be made. Barbara Ericson

Links to this Page