Java series: "Java Core Technology Vol. 1" Learning notes, Chapter 11 Debugging Tips

Source: Internet
Author: User
Tags throwable jconsole

11.6 Debugging Tips 1) A less well-known but very effective technique is to put a main method in each class so that each class can be unit tested. This method can be preserved because the Java Virtual machine only calls the main method of the startup class. 2) Log agent, (logging proxy), through a subclass object, stealing the parent class's method call, and logging in it, the sample code is as follows:
  
 
  1. Random random = new Random{
  2. public double nextDouble(){
  3. double result = nextDouble();
  4. Logger.getGlobal().info("nextDouble:" + result);
  5. return result;
  6. }
  7. }
3) Use Throwable's Printstacktrace method to print the call stack, but it is not necessary to print the stack information by exception, as long as you can print the current call stack information by calling Thread.dumpstack () anywhere in the code. Such information is generally displayed in the System.err.
4) How to capture the output stream and error stream in a file
  
 
  1. 捕获输出流
  2. java MyProg >log.txt
  3. 捕获错误流
  4. java MyProg 2>log.txt
  5. 同时捕获输出流和错误流
  6. java MyProg >&log.txt
5) The processor that can change non-default catch exception
  
 
  1. Thread.setDefaultUncaughtExceptionHandler{
  2. new Thread.UncaughtExceptionHandler(){
  3. public void uncaughtException(Thread t, Throwable e){
  4. //save info to file
  5. }
  6. }
  7. }
6) If you want to see the class loading process, you can invoke the-verbose flag to start the Java Virtual machine, so you can see some output similar to the following. This method is useful for diagnosing problems that arise as a result of classpath.



7) Use Java-xlint:xxxx to tell the compiler to do some grammar checking.
8) There is a jconsole program that can be used to monitor the various parameters of a Java Virtual machine jconsole virtual machine Process ID

9) You can use the Jmap utility to capture a heap dump that shows each object in the heap, using the following command: Jmap-dump:format=b, File=dumpfilename processId
Jhat Dumpfilename

You can then use your browser to enter http://localhost:7000 to view the objects in the pair in the dump pair.
10) If you run a Java virtual machine using the JAVA-XPROF flag, you run a basic profiler to analyze the methods that are often called in your code. The profiling information is sent to the System.out output.

11.8 Using the debuggerThis is a basic meeting, nothing to look at.






From for notes (Wiz)

Java series: Java Core Technology Vol. 1 Learning notes, Chapter 11 Debugging Tips

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.