Sun released update 14 of the Java 6 JDK and JRE. As well as the usual collection of bug fixes, this release includes some experimental new features designed to improve the performance of the JVM (see the release notes). One of these is Escape Analysis. What Is Escape Analysis? Escape analysis is a compiler optimization technique that analyzes an object's access path to determine whether or not it is escaped from its current scope. If an object is not escaped, then the compiler can allocate it on the stack rather than on the heap. This eliminates the need for memory allocation and garbage collection for the object, which can improve performance. Allocating on Heap Vs the Stack To understand the implications of allocating an object on the stack or heap, it is first necessary to understand how these two data structures work. The heap is a pool of memory that is managed by the JVM. When an object is created, it is allocated a space on the heap. The size of this space depends on the object's type and the fields it contains. The stack is a data structure that is used to store local variables and function call information. When a function is called, its parameters and local variables are stored on the stack. When the function returns, these variables are popped off the stack. Allocating an object on the heap requires two operations: The object is allocated a space on the heap. The object's reference (a pointer to its location on the heap) is stored on the stack. Allocating an object on the stack requires only one operation: The object is allocated a space on the stack. Because allocating an object on the heap requires an extra operation, it is generally slower than allocating on the stack. In addition, heap allocation requires the JVM to manage the memory used by the object. This includes finding a free block of memory of the appropriate size and dealing with fragmentation. Stack allocation does not require any memory management, as the stack is managed by the operating system. What Is a Compiler? A compiler is a program that converts code written in one language (usually called the source language) into another language (usually called the target language). The target language can be either machine code or another high-level programming language. In Java 6 Update 14, escape analysis was turned off by default but could be enabled by passing the -XX:+DoEscapeAnalysis flag to the java command. However, now Escape analysis is supported and enabled by default in Java SE 6u23 and later. What Is An Object? In Java, an object is a self-contained unit of functionality that can be created and used independently. A typical object consists of both data and behavior. Data is represented by fields (also called member variables or instance variables), and behavior is represented by methods. You can think of an object as a "thing" that can perform certain actions and contains certain information. For example, a car is an object that can be driven (behavior) and has a color (data). Why Is Escape Analysis Important Today? Escape analysis is important because it can improve performance by eliminating the need for memory allocation and garbage collection for certain objects. When we say "garbage collection", that refers to the process of reclaiming memory that is no longer being used by the program. In general, garbage collection can be a CPU-intensive operation, so eliminating it can improve performance. Any time you can avoid allocating memory and/or doing garbage collection, your program will run faster. What Are Some Examples of Objects That Can Benefit from Escape Analysis? Small objects that are created and used within a single method can often benefit from escape analysis. For example, consider the following code: public static void main(String[] args) { String s = "Hello, world!"; System.out.println(s); } In this code, the String object created on line 3 is not escaped. It is created within the main method and is used only within that method. As a result, the String object can be allocated on the stack rather than on the heap. This eliminates the need for memory allocation and garbage collection for that object. How Does Escape Analysis Work? To determine whether or not an object is escaped, the compiler uses a flow-sensitive, interprocedural analysis. This means that the compiler not only analyzes the code within a single method but also looks at how the object is used in other methods that are called from the current method. If an object is only used within the scope of a single method and is never passed as an argument to another method, then it is not escaped. For example, consider the following code: public class MyClass { public static void main(String[] args) { MyClass obj = new MyClass(); obj.method1(); } public void method1() { int x = 5; method2(x); } public void method2(int y) { int z = y * 2; } } In this code, the MyClass object is created in the main method and passed as an argument to the method1 method. The method1 method then creates a local variable x and passes it as an argument to the method2 method. The MyClass object is therefore escaped because it is used in another method (method2) that is called from the current method (method1). In contrast, the local variable x is not escaped, because it is only used within the scope of the method1 method. When Should I Use Escape Analysis? Escape analysis can be used to improve the performance of any Java application. It helps to reduce the memory footprint. It is most effective when objects are short-lived and are only used within a single method. To see what kind of impact escape analysis might have on my applications, I decided to try it on a couple of my more CPU-intensive Java programs. Escape analysis is turned off by default since it is still experimental. It is enabled using the following command-line option: -XX:+DoEscapeAnalysis Benchmark 1 The first program I tested is a statistical simulation. Basically, it generates millions of random numbers (using Uncommons Maths naturally) and does a few calculations. VM Switches: -server 95 seconds VM Switches: -server -XX:+DoEscapeAnalysis 73 seconds Performance improvement using Escape Analysis: 23% Benchmark 2 The second program I tested is an implementation of non-negative matrix factorisation. VM Switches: -server 22.6 seconds VM Switches: -server -XX:+DoEscapeAnalysis 20.8 seconds Performance improvement using Escape Analysis: 8% In both cases, escape analysis gave a noticeable performance improvement. I was quite surprised by how much of an improvement I saw in the first benchmark. I wasn't expecting to see anything like that. Escape analysis is an important tool in the JVM performance tuner's armory and it's good to see that it is being improved and refined in each new release of the JDK. Are There Other Ways To Reduce Memory Footprint in Java? Yes, there are other ways to reduce the memory footprint in Java. One way is to use object pooling. Object pooling is a technique for reusing objects that have already been created. Another way is to use a generational garbage collector. A generational garbage collector is designed to collect short-lived objects more efficiently than long-lived objects. Finally, you can use a compacting garbage collector. A compacting garbage collector moves objects around in memory so that there are fewer gaps between objects. This can help to reduce memory fragmentation. Conclusions These benchmarks are neither representative nor comprehensive. Nevertheless, for certain types of programs, the addition of escape analysis appears to be another significant step forward in JVM performance. In conclusion, although escape analysis is still experimental, it is definitely worth experimenting with in your own applications. If you are interested in learning more about escape analysis, I highly recommend reading about it further on Dzone.
When moving around from page to page in your trusty browser, you get the benefit of hovering over links and viewing the link's target URL in the status bar. When it comes to page printouts, however, this obviously isn't an option. Most website printouts simply show the link as text with an underline. What good is that? Links Should be Printable One of the key elements that many people forget about when they are creating a web page of any kind is the fact that they should be making the links that are inserted printable. You see, there are many people who will want to check out the information and research that you are providing them via the links inserted into your page. Unfortunately, they may not be able to look at those links as completely as they might want to if you are not making it easy and possible for them to print off the links. Making links printable is a matter of getting into the CSS code and looking to make sure that any URLs that you have inserted are truly printable for your audience. It is fully expected that you will take the time to do this for the people who have bothered to show up and read your materials. Truthfully, you owe them a sign of your appreciation for having come to see what you are creating in the first place. Internet Explorer Often Ignores the Code One thing to keep in mind when you begin to mess around with the CSS codes for your URLs is that it is entirely possible that Internet Explorer will ignore the codes that you put in there if you don't get them just right. Internet Explorer is set up in such a way that it will ignore certain lines of code and other materials that people insert into their websites if Internet Explorer feels that the code is not relevant. This is why you must be on top of everything to make sure Internet Explorer does force your code out when you know for a fact that the code that you are using is incredibly important to the usefulness of your website. To provide extra help to your content viewers, you may want to include a snippet of information that lets them know that Internet Explorer may cause some problems when they are trying to print off links in the article. A quick line or two that encourages them to use a different Internet portal such as Firefox or Google Chrome may prove beneficial to all of your readers. They need access to your information, but they may struggle to get it if they are only using Internet Explorer. Sadly, they may not even realize that this is a problem unless you point it out by making it clear that they need to switch browsers in order to print off the information that they require. Take some time to throw in a quick line about why users need to shift over to using a browser other than Internet Explorer to get the information that they require. If you do this, then they will be able to finally get the information you are providing without getting jammed up simply because of the browser they are using. One simple line and you can help fix so many of the problems that they are facing. Providing URLs for links in the print version of your page can be extremely helpful to the reader. Using a small snippet of CSS code, you can get printouts to display link URLs right next to the link text. The CSS Code a:link:after, a:visited:after { content:" [" attr(href) "] "; } The pitfall of this method of displaying links URLs for print is that Internet Explorer ignores this code. If showing link URLs is critical, I'd recommend using a javascript alternative. If not, add this snippet to your print stylesheet to make your page print-outs more informative. Click here for an example! JavaScript Alternatives Can Help There are situations in which JavaScript alternatives can help you get around the auto-blockers that Internet Explorer has put up on some of these codes. You may need to work around the traditional system in order to get the results that you need, but you should have a lot of success getting the code to look just the way you need it to if you use some alternative programs to achieve your results. Sometimes, you just have to think outside of the box like this to get around the barriers that sadly exist within the computing space. We can all hope for a day when there aren't so many layers of protectionism between ourselves and the things that we want to put up on the Internet. If we can just use some of the tools at our disposal as far as JavaScript alternatives and the like are concerned, then we may be able to make some progress in the right direction. It is a challenge to be sure, but there are a lot of ways that we might be able to build a better Internet and a better world in general. Does Printing Still Matter for Links? There are different schools of thought about how important the ability to print something off still is to people, but most land on the assumption that it is better to have printing abilities than to not have them. People sometimes want to print something off so they can view it on their own time and even hand the printed copy off to someone else that they know that they believe might benefit from it as well. Why would you want to deny them the ability to do this? The CSS code that is used on your links will be extremely important to the final ability of someone else to use it to print off what they see on the screen. This is why you should be extra careful to make sure the code is written just right. There are situations in which your viewers may delight in the information that you provide them, but they may not have Internet access everywhere that they go. If they need to pull up your information without access to the Internet, then it is necessary for them to print off the files that they need from you. Thus, you might want to spend some time getting your links into a form that allows them to be printable. Not everyone who visits your website will care about printing something off, but the people that do care about it will notice the steps you took to make this possible for them, and they will surely appreciate what you have done for them.
Putting Together the Best of Both Worlds One of the things that people like so much about JavaFx is the fact that it has a suite of powerful tools used by designers and creators to get exactly the kind of look and feel that they desire from their creations. When used properly, JavaFX will change the way that you take care of your biggest technology challenges. That is why it is such a powerful product to use, and that is also why people around the world recommend it for the value that it can add to your work. A few of the features that are included are: WebView Java APIs High-Performance Media Engine Multitouch Support Canvas API These are just a few of the things that JavaFx has going for it. Truly, it is a powerful engine that people use to help make sure their media creations are something that people will stand up for and take note of. The truth is, no matter who you are or what your intentions are for the use of Java in your life, the JavaFX combined with Java itself is the way to go. while experimenting with javafx, remember that java is never far away. in this case, let's call out to : here we go, here's all that's needed, via the scripting api, which is included in the javafx sdk: package calc; import java.io.inputstreamreader; import javax.script.scriptengine; import javax.script.scriptenginemanager; import javax.script.scriptexception; public class calculatorlauncher { public static void main(string[] args) { try { scriptenginemanager manager = new scriptenginemanager(); scriptengine engine = manager.getenginebyextension("fx"); inputstreamreader reader = new inputstreamreader(calculatorlauncher.class.getresourceasstream("calculator.fx")); engine.eval(reader); } catch (scriptexception ex) { } } } and so, here's my whole application, calling jim weaver's calculator demo: What is even better would be to be able to embed that JavaFX stage (or a panel-like part thereof?) into a JFrame. then one would have the best of both worlds: the graphic "oomph" of JavaFX, together with the daily low-level grunt work of java. that, I believe (and hope), is the promise of JavaFX for java developers (and groovy developers), as opposed to designers and similar graphic artists, who seem to be the primary target of JavaFX. Day-to-Day Grind Work One thing that people absolutely love about Java itself is that it allows them to get the day-to-day grunt work done that they need to be done. They are able to see real progress on a daily basis simply by using Java to help propel their best efforts into the world. Yes, there are not as many bells and whistles with Java as there are with JavaFx and other tools, but that just means that there is more to combine with Java to get it to do all that you want it to do. Using JavaFX and Java together can help get more done. This is an innovative approach because it means that they are able to use the bells and whistles of JavaFx incorporated into the practicality of Java itself. Bringing those two things together is a great way for everyday people to achieve results in their work that they would not normally have believed was possible. We do need to recognize the fact that there are many great resources that Java can be used for, but it is also the case that JavaFx simply gives it the extra push that it needs to be a great computing program all around. We would be remiss if we didn’t combine the two things together for optimal use. Does JavaFX Makes Sense For Business Applications? There is still a lot of debate about how much sense it makes to use JavaFX for business applications. On the one hand, there are a lot of people who prefer this as their technology of choice. However, there are some others that say that it is too buggy and can’t handle all of the data that is required for a business operation to run smoothly. What people really like about JavaFx is the fact that it can be used on a “zero installation” basis. This means that it doesn’t need to dig deep into your databases and systems in order to function the way that it is supposed to. That is a pretty big deal because you may find it quite irritating to try to deal with systems that constantly request permissions from you to get deeper and deeper into sensitive data. JavaFX doesn’t do that. However, there is a trade-off. The fact that JavaFX doesn’t need to be as deep into your company’s systems also means that it doesn’t necessarily have the computing power that you may require to fun every operation that you need it to. That means that there could be unnecessary crashes and delays when you attempt to use this system as a standalone program. Does Combining It With Java Help? Some of the issues that are seen with JavaFx may be relieved by the fact that you can also use plain old Java to help make things run a little more smoothly. That’s right, you can combine the Java platform that you are already familiar with to let it latch on to the JavaFx system and generate results that way. You may see fewer system errors and hang-ups when you use this older piece of software to aid the JavaFx system. Regular Java does require more permissions to your data, but that may be a trade-off that you are willing to make if you understand that the upsides are potentially quite enormous for you. Think about your options carefully and then make a decision. Unless you are guarding particularly sensitive data at your company, it probably makes sense to go forward with the plan to get JavaFx and Java combined into the rotation of programs that you use. Innovation Is At Your Fingertips There is plenty of excitement and buzz about the possibilities that JavaFx can bring to the world of business. People are looking at it and starting to realize that there are many ways that it may be used as an innovation machine. They are particularly excited that there are more options than ever available to them and that they can see the ways in which this system may be used to generate outsized business opportunities both for the company that they work for as well as for themselves. If you haven’t had the chance to try the JavaFx system for yourself yet, you need to give it a shot. There are a lot of people who are already benefiting from what it has to offer, and you may fall behind if you don’t try it out as well. Look at what it can do when you get the chance, and then get one for yourself and begin the process of innovation. You won’t regret taking this opportunity to expand your horizons.
This article introduces how to use the paho-mqtt client and implement connection, subscribe, messaging, and other functions between the MQTT client and MQTT broker in the Python project.
This article introduces the software algorithms used in self-navigating vehicle processing architectures and covers critical safety-related requirements.
In this video tutorial, take a closer look at stored procedures with input & output parameters in MySQL, and CallableStatement with Input&Output Params.
If you're thinking of using Vaadin for your UI components, here are some resources, including two videos, to see how you can incorporate it into both Spring and Java EE.
Sealed types, switch expressions, and record types. Here are just a few new features introduced in the latest Groovy 4.0 release. In this article, I want to show you ten things that make Groovy 4.0 amazing.
Send in the clones: that’s what we do when we replicate databases. In this article, you’ll learn how to perform the most basic form of replication with MariaDB.
Learn of challenges associated with processing physical receipts for digital expensing operations and discover an OCR API solution to alleviate the problem.
The Jersey project is very well documented so it makes it easy to learn REST with Java. In this article I’m going to build two projects. The first project will be a very simple HTML page that presents a form to the user and then submits it to a REST project residing on the same server. The second project will be the REST part. For this article I used the following tools: 1. Netbeans 7 2. Apache Tomcat 7 3. Jersey 4. Java I built this on OS X Lion. Go ahead and create a new Maven Web Application with Netbeans 7 called: MyForm Once the project has been generated take the resulting (default) index.jsp file and delete it. In its place add a file called: index.html and add the following content to it: Name: Message: Item 1: Item 2: Basically, I created a simple (ugly) form that takes a few parameters the user enters. They submit the form and the data is sent to the REST project we will soon be building. The idea here is we are using an HTTP POST to create a new message. That’s it for the first project! With Netbean’s Maven integration do a Clean and Build and then deploy the resulting WAR file to Apache Tomcat. Create another new Maven Web Application with Netbeans 7 called: RESTwithForms Add two new Java classes to the new project: 1. MyApplication 2. MessageResource The code for MyApplication.java is as follows: package com.giantflyingsaucer; import com.sun.jersey.api.core.PackagesResourceConfig; import javax.ws.rs.ApplicationPath; @ApplicationPath("/") public class MyApplication extends PackagesResourceConfig { public MyApplication() { super("com.giantflyingsaucer"); } } In a brief nutshell this code allows us to make use of some Servlet 3.0 goodies (we don’t need to create a web.xml file for this project as an example). For more details see the sections titled: Example 2.8. Reusing Jersey implementation in your custom application model and Example 2.9. Deployment of a JAX-RS application using @ApplicationPath with Servlet 3.0 at this link. The real guts of the REST project are in the MessageResource.java file as seen below: package com.giantflyingsaucer; import java.net.URI; import java.util.List; import java.util.UUID; import javax.ws.rs.FormParam; import javax.ws.rs.POST; import javax.ws.rs.Path; import javax.ws.rs.core.Response; import javax.ws.rs.Consumes; import javax.ws.rs.core.MediaType; @Path("/messages") public class MessageResource { @POST @Consumes(MediaType.APPLICATION_FORM_URLENCODED) public Response createMessage(@FormParam("name") String name, @FormParam("message") String message, @FormParam("thelist") List list) { if(name.trim().length() > 0 && message.trim().length() > 0 && !list.isEmpty()) { // Note 1: Normally you would persist the new message to a datastore // of some sort. I'm going to pretend I've done that and // use a unique id for it that obviously points to nothing in // this case. // Note 2: The way I'm returning the data should be more like the commented // out piece, I am being verbose for the sake of showing you how to // get the values and show that it was read. return Response.created(URI.create("/messages/" + String.valueOf(UUID.randomUUID()))).entity( name+ ": " + message + " --> the items: " + list.get(0) + " - " + list.get(1)).build(); // This is a more real world "return" //return Response.created(URI.create("/messages/" + String.valueOf(UUID.randomUUID()))).build(); } return Response.status(Response.Status.PRECONDITION_FAILED).build(); } } Note: Pay special attention to the comments. Please don’t email me stating I shouldn’t be returning text back with the values, also please don’t tell me I should be iterating the list, etc. this is just a demo. You will obviously do this differently in a production environment. The key here is simplicity and minimal code. At this point you need to add jersey-server as a dependency in your POM file. com.sun.jersey jersey-server-linking 1.9.1 With Netbean’s Maven integration do a Clean and Build and then deploy the resulting WAR file to Apache Tomcat. You are now ready to test it out. Load up the HTML file from the first project and enter some data and then submit it. If you have a tool like FireBug for Firefox, you can also see that an HTTP 201 was returned (if successful). If you don’t enter any data in the form then you should get an HTTP 412 back. With not much more work you could just as easily use something like jQuery and submit the form via AJAX.