Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How does a virtual machine work?

I've been looking into how programming languages work, and some of them have a so-called virtual machines. I understand that this is some form of emulation of the programming language within another programming language, and that it works like how a compiled language would be executed, with a stack. Did I get that right?

With the proviso that I did, what bamboozles me is that many non-compiled languages allow variables with "liberal" type systems. In Python for example, I can write this:

x = "Hello world!"
x = 2**1000

Strings and big integers are completely unrelated and occupy different amounts of space in memory, so how can this code even be represented in a stack-based environment? What exactly happens here? Is x pointed to a new place on the stack and the old string data left unreferenced? Do these languages not use a stack? If not, how do they represent variables internally?

like image 371
Martin Avatar asked May 29 '09 21:05

Martin


People also ask

What is virtual machine and how it works?

A virtual machine is a computer file, typically called an image, which behaves like an actual computer. It can run in a window as a separate computing environment, often to run a different operating system—or even to function as the user's entire computer experience—as is common on many people's work computers.

What is the main function of virtual machine?

Virtual machines (VMs) allow a business to run an operating system that behaves like a completely separate computer in an app window on a desktop.

What is an example of a virtual machine?

Examples of process VMs include the Java Virtual Machine, the . NET Framework and the Parrot virtual machine. System VMs rely on hypervisors as a go-between that give software access to the hardware resources.

How do I setup a virtual machine?

Open up your VM app and click the button to create a new virtual machine. You'll be guided through the process by a wizard that first asks which OS you'll be installing. If you type the name of the OS in the “Name” box, the app will most likely automatically select the type and version for the OS.


2 Answers

Probably, your question should be titled as "How do dynamic languages work?."

That's simple, they store the variable type information along with it in memory. And this is not only done in interpreted or JIT compiled languages but also natively-compiled languages such as Objective-C.

like image 61
mmx Avatar answered Nov 16 '22 18:11

mmx


In most VM languages, variables can be conceptualized as pointers (or references) to memory in the heap, even if the variable itself is on the stack. For languages that have primitive types (int and bool in Java, for example) those may be stored on the stack as well, but they can not be assigned new types dynamically.

Ignoring primitive types, all variables that exist on the stack have their actual values stored in the heap. Thus, if you dynamically reassign a value to them, the original value is abandoned (and the memory cleaned up via some garbage collection algorithm), and the new value is allocated in a new bit of memory.

like image 32
Doug Avatar answered Nov 16 '22 17:11

Doug