Maneesh has posted 1 posts at DZone. View Full User Profile

JVM Internals Series -Part 1

07.09.2009
| 22607 views |
  • submit to reddit

A lot of Java developers tend to be unaware about the basics of the internals of the JVM. This series aims to look at the internals of the JVM and explain in a simple way, what  the Java Virtual Machine is and how the JVM functions


What is the Java Virtual Machine ?

When you talk of the JVM there are three aspects which we speak of

  1. Specification
  2. Concrete Implementation
  3. Runtime

The specification is a concept, Concrete implementations which exist on different platforms and are available from different vendors like IBM, Sun etc are either all software or a mix of hardware and software. A Runtime instance hosts a single running java application.

A runtime instance has one function and that is to run one java application.  Whenever a java application runs, a runtime instance is born. Thus the number of runtime instances on a machine are equal to the number of applications which are running.  The JVM starts with the invocation of a main() method which needs to be public, static, void and takes a String array as an argument. The main method thus serves as the initial thread for the running application. This can in turn spawn other threads. Inside the VM, threads come in two flavors, daemon and non-daemon. A daemon thread is a thread which is normally used by the VM itself, like the thread which runs the garbage collection. However an application can mark any thread it creates as a daemon. A java application continues to execute until there are any non-daemon threads alive(parallely the VM runtime instance continues to exist). When all non-daemon threads of an application exit, the virtual machine instance exits.

JVM Architecture

jvm-arch

The VM needs memory to store a lot of information, like the bytecode, program arguments, objects which have been instantiated etc. Some of the information stored in the memory is shared across all application threads, while other information may be unique to individual threads.

Each instance of a JVM has one method area and a heap. These areas are shared by all threads running within the instance.
The information regarding the type is loaded from the class files when the JVM loads a class file. This information is stored in the method area. All the objects which are instantiated placed on the heap.
As each new thread is created, it is assigned its own pc register(program counter) and java stack. If the thread is executing a java method (not a native method), the counter in the pc register points to the next instruction to execute. The java stack comprises of frames. A frame consists of the state of one method invocation. When a thread invokes a method, a new frame is pushed on the thread’s stack. On completion of the method execution, the VM pops and discards the frame. No other thread can access another threads pc register or java stack.

This gives a brief overview of the java virtual machine’s architecture. In the next post,I will cover data types and the class loading subsystem.

From http://maneeshchaturvedi.wordpress.com

Published at DZone with permission of its author, Maneesh Chaturvedi.

(Note: Opinions expressed in this article and its replies are the opinions of their respective authors and not those of DZone, Inc.)

Tags:

Comments

Yousuf Haider replied on Thu, 2009/07/09 - 12:56pm

Atleast have the decency to cite that you took this image from the Artima Book on JVM internals.

http://www.artima.com/insidejvm/ed2/jvm2.html

Yousuf Haider replied on Thu, 2009/07/09 - 12:59pm

Infact most of what you've written seems to be a copy paste from that book with some minor edits. Atleast bring some originality in what you're writing.

Maneesh Chaturvedi replied on Thu, 2009/07/09 - 11:45pm in response to: Yousuf Haider

The contents discussed in the post are taken from a variety of sources including other articles, the JLS books and the java docs. Obviously when talking about specifications one cannot write anything which crops(sic creativity) up in the mind. It is my understanding of what the internals of the VM are. In case you have a better approach please feel free to post your own on your blog. Obviously not everyone has the time or resources available, hence the intent here is to simplify and place information here which makes it possible for develpers to get to know about the internals of the VM.

Subhashish Dutta replied on Fri, 2009/07/10 - 2:19am

Thanks for the article. I moved to Java a couple of years ago after years of C++. Due to a mixture of both work pressure and laziness, I haven't had a chance to understand how the jvm really works internally. This series will surely help people like me.

Raghu Semburakk... replied on Fri, 2009/07/10 - 4:06pm

I hope you started reading about JVM internals and trying to recollect/summerize here? There are lot of article/tutorial already out there.  I'm wondering what extra thing or discussion or content you indent to provide?

Yousuf Haider replied on Fri, 2009/07/10 - 7:21pm

You have blatantly copied an image from the link I mentioned in my previous comment.

When borrowing any kind of content usually people cite the original source of the material.

 Let me quote a sentence from the link: http://www.artima.com/insidejvm/ed2/jvm.html

"A runtime instance of the Java virtual machine has a clear mission in life: to run one Java application. When a Java application starts, a runtime instance is born. "

 Now let me quote me what you've written:

"A runtime instance has one function and that is to run one java application. Whenever a java application runs, a runtime instance is born."

 Similarly here is another quote from the same link (above)

"Inside the Java virtual machine, threads come in two flavors: daemon and non- daemon. A daemon thread is ordinarily a thread used by the virtual machine itself, such as a thread that performs garbage collection. The application, however, can mark any threads it creates as daemon threads ...... A Java application continues to execute (the virtual machine instance continues to live) as long as any non-daemon threads are still running. When all non-daemon threads of a Java application terminate, the virtual machine instance will exit."

 

 and here is a quote from your article:

"Inside the VM, threads come in two flavors, daemon and non-daemon. A daemon thread is a thread which is normally used by the VM itself, like the thread which runs the garbage collection. However an application can mark any thread it creates as a daemon. A java application continues to execute until there are any non-daemon threads alive(parallely the VM runtime instance continues to exist). When all non-daemon threads of an application exit, the virtual machine instance exits."

 

To me this looks like all you've done is actually copied content from that site and edited. There are numerous examples like this in your article. If all you're doing is transferring information (which is surely a noble endeavor) then I have no objections to you writing a blog on this topic based on information found from varrious sources. However not only do you not cite any sources but you have copied from your original source and done minor edits on it and presented it as your own. That is what I am objecting to.

Maneesh Chaturvedi replied on Tue, 2009/07/14 - 12:03am in response to: Yousuf Haider

References - Various blogs/articles on artima,javaposse,dzone, the java language specifications and other unnamed thousands of other references which i fail to recollect as of now.

Balu Reddy replied on Tue, 2009/07/14 - 12:11pm

hi, Maneesh why you remove JVM interneals series from your blog.It is so useful for people like me.

Plz continue your post's on JVM Internals.

                                          thank U.

Maneesh Chaturvedi replied on Thu, 2009/07/16 - 11:30pm in response to: Balu Reddy

Hi Thanks for the encouragement. I am planning and charting the complete series.It should be available within the next 4-5 days.

Susi Sorglos replied on Mon, 2009/07/20 - 3:19am

Don't you have any shame you thief?

Manoj Bardhan replied on Tue, 2012/06/19 - 8:43am

Really its useful. I Need more!

Comment viewing options

Select your preferred way to display the comments and click "Save settings" to activate your changes.