How do I trace methods calls in Java? -


consider 2 simple java classes below:

first example

class computer {     computer() {         system.out.println("constructor of computer class.");     }     void on() {         system.out.println("pc turning on...");     }     void working() {         system.out.println("pc working...");     }     void off() {         system.out.println("pc shuting down...");     }     public static void main(string[] args) {         computer = new computer();         laptop = new laptop();         my.on();         my.working();         your.on();         your.working();         my.off();         your.off();    } } 

second example

class laptop {     laptop() {         system.out.println("constructor of laptop class.");     }     void on() {         system.out.println("laptop turning on...");     }     void working() {         system.out.println("laptop working...");     }     void off() {         system.out.println("laptop shuting down...");     } } 

after program run, how trace (1) object call method (2) , how many times?

just little precision, might have 100 classes , 1000s of objects each of them calling 100s of methods. want able trace (after run program), object called method , how many times.

thanks suggestion.

this prints line each method call of objects in threads:

 runtime.tracemethodcalls() 

you can use call tracer housemd or btrace or intrace

or more involved analysis, use call graph utility 1 of these:

here article on subject

update:

the following methods deprecated in java 9 (and report might nops in release).

runtime.tracemethodcalls() runtime.traceinstructions() 

fortunately there alternatives. above methods slated removal because there jvm-specific alternatives.

  • java flight recorder     part of jdk 7 of build 56. requires commercial license use in production
  • visualvm                       free/popular 3rd party

they both pretty easy setup , start collecting information , have
nice gui interfaces. attach running jvm process , allow thread snapshots , various other kinds of diagnosis.


Comments

Popular posts from this blog

javascript - AngularJS custom datepicker directive -

javascript - jQuery date picker - Disable dates after the selection from the first date picker -