6 JDK built-in tools that ordinary Java programmers learn to use __java

Source: Internet
Author: User

Http://developer.51cto.com/art/201512/502926.htm

Unlike your problem, I think software engineering is mainly used to solve problems. Some bloggers think that "every child should learn to program", "You think learning math is just fun." If you have seen my HTML5 debugger, you will find that I am a programmer, but I do more work than math. Both agree on the idea that software engineering is not just a few words written in computer language. The software solves the problem by interpreting the programmer's value.

The ultimate solution to the problem comes from science, a clear mind and the tools we've been using all the way.

Have you ever noticed the tools that came with the JDK installation? Now that Daniel has agreed to add those tools to the JDK, it should be useful.

Therefore, in this article, I picked a few Hotspot standard installation after the use of small tools to introduce. We decided to ignore those security-related and various remote method calls (RMI), applets, Web-start, web-services tools. Let's focus on the tools that ordinary developers might find useful in developing general applications. Note that if you're just interested in command-line tools, not just Java-related tools, here are 5 very useful command-line tools.

Again, although the following is not a complete list of JDK tools, we would like to give you an essential version. Here are some really useful things you can do with these commands.

0, JAVAP

You can pass these useful parameters to the JAVAP (Java class file counter compiler):

-i– print rows and local variables

-p– print all class and member information, including Non-public.

-c– Print Method byte code

For example, in the famous "Do you really understand Classloader?" "In the speech, when there is a nosuchmethodexception error, we can execute the following command to investigate what member methods the class has and get all the information that the class wants to find:

Javap-l-c-p Util2

JAVAP is useful when debugging class internal information or studying random byte-code order.

1, JJS

The JJS command can start a JavaScript command terminal, which you can use as a calculator or test JS's quirky usage with a random JS string. Don't let another JavaScript puzzle get you off guard.

Ha, see what just happened. But JavaScript is another topic, just know that you can use JJS to know how JS works even if you don't have a node.js or a browser.

2, Jhat

The Java Heap Analysis tool (Jhat), as its name describes, analyzes dump heap information. In the following small example, we construct a outofmemoryerror and then assign-xx:+heapdumponoutofmemoryerror to the Java process so that the runtime produces a dump file for our analysis.

public class ohmymemory {    Private static map map = new  HashMap<> ();    Public static void main (String[] args)  {      runtime.getruntime () Addshutdownhook (       new  Thread ()  {          @Override           public void run ()  {            System.out.println ("we have accumulated "  + map.size ()  +  " entries");          }       }      );     for (int i = 0; ;i++)  {        Map.put (Integer.tobinarystring (i),  i);     } } } 

Creating a OutOfMemoryError is simple (most of the things we don't want to do), we just keep making the garbage collector work.

Running this code produces the following output:

org.shelajev.throwaway.jdktools.ohmymemory  Java.lang.outofmemoryerror: java heap space   dumping heap to java_pid5644.hprof ...  heap dump file created  [73169721 bytes in 0.645 secs]  exception in thread  "main"   java.lang.outofmemoryerror: java heap space  At java.util.hashmap.resize (HashMap.java : 703)   At java.util.hashmap.putval (hashmap.java:662)   At java.util.hashmap.put (HashMap.java : 611)   At org.shelajev.throwaway.jdktools.ohmymemory.main (ohmymemory.java:24)   at  SUN.REFLECT.NATIVEMETHODACCESSORIMPL.INVOKE0 (native method)   at  Sun.reflect.NativeMethodAccessorImpl.invoke (nativemethodaccessorimpl.java:62)   at  Sun.reflect.DelegatingMethodAccessorImpl.invoke (delegatingmethodaccessorimpl.java:43)   at  Java.lang.reflect.Method.invoke (method.java:483)   AT COM.INTELLIJ.RT.Execution.application.AppMain.main (appmain.java:134)   we have accumulated 393217  entries 

Yes, we have a file to analyze now. We started the analysis of this file execution Jhat, Jhat will analyze this file to open an HTTP server for us to view the results.

$ jhat java_pid5644.hprof Reading from Java_pid5644.hprof ...  Dump file created Thu Aug 14:48:19 eest 2014 Snapshot read, resolving ...  Resolving 1581103 objects ...  Chasing references, expect 316 dots ...  Eliminating duplicate references ...  Snapshot resolved. Started HTTP server on the Port 7000 server is ready.

You can view the data for the dump by accessing the http://localhost:7000.

On that page we can learn from the column graph of heap information what exactly is running out of memory.

Now we can see clearly that having a 393567-node HashMap is the culprit that causes the program to crash. Although there are more tools to check for memory distribution usage and heap analysis, Jhat is built in and is a good starting point for analysis.

3, Jmap

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.