TweetFollow Us on Twitter

Mar 99 Factory Floor

Volume Number: 15 (1999)
Issue Number: 3
Column Tag: From The Factory Floor

Mac OS Runtime for Java 2.1

by Kelly Jacklin and Dave Mark, ©1999 by Metrowerks, Inc., all rights reserved.

This month, the Factory Floor visits with Kelly Jacklin, a Senior Software Engineer at Apple working on Java runtime and JIT technology. The newest version of MRJ (version 2.1) is about to ship, and Kelly was kind enough to take time out of his schedule to share with us some of the technical details about Apple's virtual machine for Java. Kelly Jacklin <jacklin@apple.com> has been a software engineer at Apple for over seven years working on a variety of operating system projects and related technologies. He began work at Apple on A/UX 3.0, Apple's UNIX operating system, and was one of the original team members of the Star Trek project to port Mac OS to Intel, as well as a core microkernel and OS services engineer on the Copland project. He has spent the last couple of years working on virtual machine and language runtime technology, as has been involved with all currently shipping versions of RJ. In his ever decreasing spare time, he works on pointless engineering projects like writing scheme interpreters and ray tracers in Java. Aside from his engineering and familial joys of raising his son Kai with his wife Yun, he enjoys playing a plethora of musical instruments (mostly stringed), much to the chagrin of those around him...

Dave: What exactly is MRJ? Is it simply a VM, or is there more to it than that?

Kelly: MRJ, or Mac OS Runtime for Java, is Apple's language runtime environment for the Java programming language. It emulates a virtual machine for interpreting Java bytecode, supplies the runtime services Java programs expect, and provides versions of the core Java class libraries that map to Mac OS APIs such as graphics, networking, file I/O and other OS services.

In addition to these basic services, MRJ also provides developers with the ability to call the Macintosh toolbox and OS APIs, so they can write Mac applications in Java. The MRJ SDK includes basic tools for developing Java programs, as well as a powerful binding tool called JBindery which gives developers an easy way to package their Java executables as double-clickable Macintosh applications; this capability is also rather seemlessly integrated into the most recent version of CodeWarrior Pro.

Dave: What's new in MRJ 2.1?

Kelly: MRJ 2.1 reflects a single-minded goal of radically increasing the performance and stability of the Java platform on the Mac. Previous versions of MRJ, as well as third-party VMs for the Mac, have been either slow or buggy, or sometimes both. With 2.1, we strove to correct this trend, and have succeeded in producing a VM that performs very well, and is very stable. All of the features added in 2.1 reflect this single-minded approach. Specific areas of improvement include:

  • JDK 1.1.6: MRJ 2.1 implements the 1.1.6 version of the Java environment and APIs. Since we use the shared JDK classes supplied by Sun, we have picked up many bug fixes from them which help provide stable and predictable behaviour of the Java APIs.
  • AWT: The MRJ implementation of Java's abstract windowing toolkit, has been completely rewritten, mostly in Java. AWT performance has been radically enhanced, and this reflects directly in better user-perceived, as well as actual, performance of most applications. This new AWT implementation makes extensive use of the Appearance Manager, with the result that Java applications look very Mac-like when running under MRJ. This has also been an area in which we have greatly improved our stability and correctness, where correctness is reflected both in adherence to the Java specifications for the APIs, as well as in developer expectations where the specifications are ill-defined.
  • Cross-platform Consistency: Many Java developers have been ignoring the Mac, simply because the Java implementations on the Mac have lacked the maturity or predictability of behaviour that they have ccome to expect from Java implementations on Windows. We have geatly enhanced the consistency of our platform, which allows developers to bring their Java applications over to the Mac easily and successfully with very little pain. Where specifications of API behaviour have been ill-defined, we have assessed the behaviour of the API on other platforms, and gone with the de-facto standard expected by developers.
  • Networking/Security: Through the use of asynchronous networking using OpenTransport, MRJ 2.1 exhibits greatly enhanced networking performance, maintaining asynchronous execution of other Java threads while threads performing network operations remain highly responsive to completion of outstanding requests. MRJ 2.1 also reflects improved support for networking and security services, such as RMIC, JDBC, encryption/decryption and code signing. It is important to note that while there were bugs in some security features in MRJ 2.0, those bugs manifested in lack of functionality, rather than security holes.
  • SDK: We have improved our support for Java development, by providing better basic Java development tools, as well as working with third parties to improve the support for MRJ in their tools. CodeWarrior now has built-in support for generating executables that use MRJ, and their debugger works very well with MRJ. In addition, MRJ 2.1 allows Mac developers to write Java applications that call the Macintosh toolbox and OS APIs, through the use of wrapper classes automatically generated from the universal Mac OS headers.
  • Threading and Synchronization: The low-level VM support in MRJ for threads and synchronization services has been completely rewritten in 2.1. This rewrite represents much better performance in areas of cotext-switching and scheduling, and provides enhanced scheduling support in areas like priority inheritance for monitors and condition variables. This has had a direct impact on both benchmark scores and real-world server applications or computationally intensive code.
  • No 68k Support: Radical improvements to a product often come at a price. In the case of MRJ 2.1, this price was paid in lack of support for 68k machines. The complexity of the project demanded that we sacrifice support for this processor, support for which required too much duplicate engineering and would have caused us to deliver much needed improvements to our developers much later. Feedback from most of our development community has shown overwhelming support for, or at least sympathy with, this decision.
  • Just-in-time Compiler: One of the more radical enhancements to MRJ has come with the introduction of a new JITC. For MRJ 2.1, Apple has an agressively optimizing JITC that far exceeds the performance of our previous JITC. We have also enhanced this JITC to provide better interaction between Java code and certain natively-dispatched calls, such as the Macintosh toolbox APIs. This has dramatically improved performance of Java code running on our VM in general; which improvement is even more pronounced for computationally-intensive code or long-running server applications. In addition, the deployment of this JITC has provided us with the flexibility to implement much of MRJ itself in Java, providing increased robustness and feature richness, while still providing competitive performance.

Dave: We hear lot about Just-In-Time compilers. How does a JITC work? When are methods compiled by a JITC? Are all JITCs strictly dynamic?

Kelly: Basically, a Java JITC is a dynamic recompilation engine for translating bytecode into native instructions. The JITC is dynamically loaded during startup of the Java virtual machine, and plugs into the VM through a set of hooks. When classes are loaded by the VM, the JITC is notified, and it can decorate the class meta-data with information it will later need to compile the methods of that class. Implementations of JITCs vary from simple strict translators to optimizing compilers, and run the gamut in between. The JITC included in MRJ versions 1.5 and 2.0 performed basic translation of the bytecode stream into an equivalent native instruction stream, with a decent register allocator to improve the performance. The advantage of this JITC was its extremely low cost in compilation, at the expense of runtime performance of the translated code that paled somewhat in comparison with JITCs in competitive virtual machines. The new JITC introduced with MRJ 2.1 offers much more compelling performance of compiled code. However, this performance advantage comes at the cost of increased compilation time for complex methods. We have mitigated this cost using some of the mechanisms commonly used in JIT technology.

Compilation is typically performed on first invocation of a method. However, with any JIT technology other than the most primitive, compilation can be computationally expensive for complex methods. For this reason, it is often better to defer compilation of a method until it is known that the speed increase of the compiled code would make up for the cost of compilation. This can be done by applying heuristics to allow a method to be interpreted until it is determined to be frequently called, at which time it is compiled. A JITC can also make use of the Java VM's threading services to defer compilation to a lower-priority background thread. In these ways, the cost of optimizing compilation can be amortized or deferred so that it is not noticable to time-critical or user-interface code.

It is also possible for a JITC to cache the results of compilation for use by a subsequent execution. However, due to the dynamic nature of the Java language, this typically involves bloating the code, or having complex fixup data stored with the code, so that dynamic conditions can be updated when the code is restored. Such conditions include, for example, whether dependent classes have been statically initialized or not, as well as things like the constants values for field offset and method table offsets that can vary as classes upon which the target class is dependent are changed. A JITC that caches compiled code is, by its nature, not strictly dynamic.

Dave: What kinds of optimizations can a JITC perform? And are there specific coding techniques I can use to make my code more JITC friendly?

Kelly: A JITC can perform any optimization that maintains the correct behaviour of the Java code, as defined by the specifications. This includes traditional optimizations, such as various loop transformations, common sub-expression elimination, global register allocation, dead code elimination, copy propogation, etc. In addition, the Java language lends itself well to several OO-centric optimizations, such as monomorphic dispatch optimization and method inlining. Since, in Java, the majority of instance methods require virtual dispatch, optimizing virtual dispatch to methods that can be proven to have only one current implementation can be a big win. Most JITCs do this unconditionally for dispatches to any methods marked with the "final" keyword, or any methods belonging to classes marked with that keyword, so appropriate use of this keyword can improve performance. One must be careful, however, not to unnecessarily restrict code with aggressive use of the "final" keyword, as this keyword makes sub-classing difficult or impossible, thus sacrificing flexibility in the usage model of the class.

In general though, developers should not be too cognizant of the presense of a JITC, other than the fact that their code performs well. The JITC in MRJ 2.1 performs optimizations in places that would surprise the average developer, and which cannot be explicitly coded for in advance. Developers should trust the JITC to do its job, and not try to second-guess it in their code. In some cases, attempts that a developer makes to anticipate optimization will backfire. Some tools that "optimize" the Java bytecode in a class, for example, would defeat later optimization efforts by the JITC that shipped with MRJ 2.0.

Dave: Once the JITC has generated PPC code, how does that code interface with the VM?

Kelly: When the JITC starts up, the VM establishes a set of callbacks into the VM for the JITC to use, both during compilation and during runtime. Information is stored by the JITC in the meta-data for a method which allows the VM to invoke the generated native code for that method. Similarily, there is information in the meta-data for methods that have not been compiled on how to invoke that method using the VM. The transitions are handled through data-driven dispatching bottlenecks. These bottlenecks facilitate bi-directional "mode switches" between Java and compiled native code, similar in concept to the co-existence of PPC and 68k emulated code under MacOS.

In addition, both the JITC and VM provide a number of "helper" functions that can be called by compiled code. This includes code to perform such operations as object allocation, math routines, Java exception processing, and transitions out of Java into statically compiled C/C++ code. These routines are made available to the compiled code through linkages established at compile time.

Dave: What facilities exist to allow programmers to mix C/C++ and Java within a single program?

Kelly: Java supports calling C/C++ code from Java, as well as invoking Java from C/C++. The facilities in C/C++ that allow this are collectively termed the Java Native Interface (JNI) APIs. Calling C/C++ code from Java traditionally assumes that the C/C++ code is aware that it is being called from Java, which has required developers to write bridging code to map Java interfaces onto their existing or legacy code. This has been a problem for many developers. To help ease this integration problem, as well as ease our own internal development of MRJ itself, MRJ also supports a proprietary native code invocation model called JDirect, as well as the traditional Java-supported mechanisms. JDirect (not to be confused with J/Direct, which is a Microsoft technology) allows Java code to invoke native shared-library-based routines directly, without the need for wrapper code written in C/C++. It takes care of looking up the routine by symbol from the appropriate shared library, and marshalling parameters from Java into the native calling conventions and calling the target routine. This technology forms the basis for access to the Mac OS APIs provided by MRJ 2.1.

A closely related topic to the invocation integration between native and Java code is the issue of embedding Java content within the windows of a Mac application. MRJ 2.1 provides an embedding API, called JManager2, which allows applications to host Java content inside of their application windows, or in separate windows devoted to Java content. It provides APIs to manage window real estate, as well as to delegate events and give time to Java code running within the application. For example, JManager is used to embed Java content inside web browser windows in Microsoft Internet Explorer. Using these APIs and services, developers can integrate native code with Java code all in the same application. This gives developers the flexibility to write portions of their application in Java, or to use Java as a plug-in technology to allow their application functionality to be extended by third parties in a cross-platform and compatible fashion.
 

Community Search:
MacTech Search:

Software Updates via MacUpdate

Latest Forum Discussions

See All

Make the passage of time your plaything...
While some of us are still waiting for a chance to get our hands on Ash Prime - yes, don’t remind me I could currently buy him this month I’m barely hanging on - Digital Extremes has announced its next anticipated Prime Form for Warframe. Starting... | Read more »
If you can find it and fit through the d...
The holy trinity of amazing company names have come together, to release their equally amazing and adorable mobile game, Hamster Inn. Published by HyperBeard Games, and co-developed by Mum Not Proud and Little Sasquatch Studios, it's time to... | Read more »
Amikin Survival opens for pre-orders on...
Join me on the wonderful trip down the inspiration rabbit hole; much as Palworld seemingly “borrowed” many aspects from the hit Pokemon franchise, it is time for the heavily armed animal survival to also spawn some illegitimate children as Helio... | Read more »
PUBG Mobile teams up with global phenome...
Since launching in 2019, SpyxFamily has exploded to damn near catastrophic popularity, so it was only a matter of time before a mobile game snapped up a collaboration. Enter PUBG Mobile. Until May 12th, players will be able to collect a host of... | Read more »
Embark into the frozen tundra of certain...
Chucklefish, developers of hit action-adventure sandbox game Starbound and owner of one of the cutest logos in gaming, has released their roguelike deck-builder Wildfrost. Created alongside developers Gaziter and Deadpan Games, Wildfrost will... | Read more »
MoreFun Studios has announced Season 4,...
Tension has escalated in the ever-volatile world of Arena Breakout, as your old pal Randall Fisher and bosses Fred and Perrero continue to lob insults and explosives at each other, bringing us to a new phase of warfare. Season 4, Into The Fog of... | Read more »
Top Mobile Game Discounts
Every day, we pick out a curated list of the best mobile discounts on the App Store and post them here. This list won't be comprehensive, but it every game on it is recommended. Feel free to check out the coverage we did on them in the links below... | Read more »
Marvel Future Fight celebrates nine year...
Announced alongside an advertising image I can only assume was aimed squarely at myself with the prominent Deadpool and Odin featured on it, Netmarble has revealed their celebrations for the 9th anniversary of Marvel Future Fight. The Countdown... | Read more »
HoYoFair 2024 prepares to showcase over...
To say Genshin Impact took the world by storm when it was released would be an understatement. However, I think the most surprising part of the launch was just how much further it went than gaming. There have been concerts, art shows, massive... | Read more »
Explore some of BBCs' most iconic s...
Despite your personal opinion on the BBC at a managerial level, it is undeniable that it has overseen some fantastic British shows in the past, and now thanks to a partnership with Roblox, players will be able to interact with some of these... | Read more »

Price Scanner via MacPrices.net

You can save $300-$480 on a 14-inch M3 Pro/Ma...
Apple has 14″ M3 Pro and M3 Max MacBook Pros in stock today and available, Certified Refurbished, starting at $1699 and ranging up to $480 off MSRP. Each model features a new outer case, shipping is... Read more
24-inch M1 iMacs available at Apple starting...
Apple has clearance M1 iMacs available in their Certified Refurbished store starting at $1049 and ranging up to $300 off original MSRP. Each iMac is in like-new condition and comes with Apple’s... Read more
Walmart continues to offer $699 13-inch M1 Ma...
Walmart continues to offer new Apple 13″ M1 MacBook Airs (8GB RAM, 256GB SSD) online for $699, $300 off original MSRP, in Space Gray, Silver, and Gold colors. These are new MacBook for sale by... Read more
B&H has 13-inch M2 MacBook Airs with 16GB...
B&H Photo has 13″ MacBook Airs with M2 CPUs, 16GB of memory, and 256GB of storage in stock and on sale for $1099, $100 off Apple’s MSRP for this configuration. Free 1-2 day delivery is available... Read more
14-inch M3 MacBook Pro with 16GB of RAM avail...
Apple has the 14″ M3 MacBook Pro with 16GB of RAM and 1TB of storage, Certified Refurbished, available for $300 off MSRP. Each MacBook Pro features a new outer case, shipping is free, and an Apple 1-... Read more
Apple M2 Mac minis on sale for up to $150 off...
Amazon has Apple’s M2-powered Mac minis in stock and on sale for $100-$150 off MSRP, each including free delivery: – Mac mini M2/256GB SSD: $499, save $100 – Mac mini M2/512GB SSD: $699, save $100 –... Read more
Amazon is offering a $200 discount on 14-inch...
Amazon has 14-inch M3 MacBook Pros in stock and on sale for $200 off MSRP. Shipping is free. Note that Amazon’s stock tends to come and go: – 14″ M3 MacBook Pro (8GB RAM/512GB SSD): $1399.99, $200... Read more
Sunday Sale: 13-inch M3 MacBook Air for $999,...
Several Apple retailers have the new 13″ MacBook Air with an M3 CPU in stock and on sale today for only $999 in Midnight. These are the lowest prices currently available for new 13″ M3 MacBook Airs... Read more
Multiple Apple retailers are offering 13-inch...
Several Apple retailers have 13″ MacBook Airs with M2 CPUs in stock and on sale this weekend starting at only $849 in Space Gray, Silver, Starlight, and Midnight colors. These are the lowest prices... Read more
Roundup of Verizon’s April Apple iPhone Promo...
Verizon is offering a number of iPhone deals for the month of April. Switch, and open a new of service, and you can qualify for a free iPhone 15 or heavy monthly discounts on other models: – 128GB... Read more

Jobs Board

Relationship Banker - *Apple* Valley Financ...
Relationship Banker - Apple Valley Financial Center APPLE VALLEY, Minnesota **Job Description:** At Bank of America, we are guided by a common purpose to help Read more
IN6728 Optometrist- *Apple* Valley, CA- Tar...
Date: Apr 9, 2024 Brand: Target Optical Location: Apple Valley, CA, US, 92308 **Requisition ID:** 824398 At Target Optical, we help people see and look great - and Read more
Medical Assistant - Orthopedics *Apple* Hil...
Medical Assistant - Orthopedics Apple Hill York Location: WellSpan Medical Group, York, PA Schedule: Full Time Sign-On Bonus Eligible Remote/Hybrid Regular Apply Now Read more
*Apple* Systems Administrator - JAMF - Activ...
…**Public Trust/Other Required:** None **Job Family:** Systems Administration **Skills:** Apple Platforms,Computer Servers,Jamf Pro **Experience:** 3 + years of Read more
Liquor Stock Clerk - S. *Apple* St. - Idaho...
Liquor Stock Clerk - S. Apple St. Boise Posting Begin Date: 2023/10/10 Posting End Date: 2024/10/14 Category: Retail Sub Category: Customer Service Work Type: Part Read more
All contents are Copyright 1984-2011 by Xplain Corporation. All rights reserved. Theme designed by Icreon.