problemclassifier.appspot.com
The above website can be used to find questions based on keywords.
Wednesday, January 18, 2012
Tuesday, January 10, 2012
Java Profiler
If you launch the Java virtual machine with the -Xprof flag, it runs a rudimentary profiler
that keeps track of the methods in your code that were executed most often. The
profiling information is sent to System.out. The output also tells you which methods
were compiled by the just-in-time compiler.
that keeps track of the methods in your code that were executed most often. The
profiling information is sent to System.out. The output also tells you which methods
were compiled by the just-in-time compiler.
Objects on the heap
You can use the jmap utility to get a heap dump that shows you every object on the
heap. Use these commands:
jmap -dump:format=b,file=dumpFileName processID
jhat dumpFileName
Then, point your browser to localhost:7000. You will get a web application that lets
you drill down into the contents of the heap at the time of the dump.
heap. Use these commands:
jmap -dump:format=b,file=dumpFileName processID
jhat dumpFileName
Then, point your browser to localhost:7000. You will get a web application that lets
you drill down into the contents of the heap at the time of the dump.
JConsole
Java SE 5.0 added support for monitoring and management of Java applications,
allowing the installation of agents in the virtual machine that track memory consumption,
thread usage, class loading, and so on. This feature is particularly important
for large and long-running Java programs such as application servers. As a
demonstration of these capabilities, the JDK ships with a graphical tool called jconsole
that displays statistics about the performance of a virtual machine (see Figure
11–3). Find out the ID of the operating system process that runs the virtual machine.
In UNIX/Linux, run the ps utility; in Windows, use the task manager. Then launch
the jconsole program:
jconsole processID
The console gives you a wealth of information about your running program. See
http://java.sun.com/developer/technicalArticles/J2SE/jconsole.html for more information.
allowing the installation of agents in the virtual machine that track memory consumption,
thread usage, class loading, and so on. This feature is particularly important
for large and long-running Java programs such as application servers. As a
demonstration of these capabilities, the JDK ships with a graphical tool called jconsole
that displays statistics about the performance of a virtual machine (see Figure
11–3). Find out the ID of the operating system process that runs the virtual machine.
In UNIX/Linux, run the ps utility; in Windows, use the task manager. Then launch
the jconsole program:
jconsole processID
The console gives you a wealth of information about your running program. See
http://java.sun.com/developer/technicalArticles/J2SE/jconsole.html for more information.
Java SE 5.0 added the -Xlint option to the compiler for spotting common code problems.
For example, if you compile with the command
javac -Xlint:fallthrough
then the compiler reports missing break statements in switch statements. (The term
“lint” originally described a tool for locating potential problems in C programs, and
is now generically applied to tools that flag constructs that are questionable but not
illegal.)
The following options are available:
-Xlint or -Xlint:all Carries out all checks
-Xlint:deprecation Same as -deprecation, checks for deprecated methods
-Xlint:fallthrough Checks for missing break statements in switch statements
-Xlint:finally Warns about finally clauses that cannot complete normally
-Xlint:none Carries out none of the checks
-Xlint:path Checks that all directories on the class path and source
path exist
-Xlint:serial Warns about serializable classes without serialVersionUID
(see Chapter 1 of Volume II)
-Xlint:unchecked Warns of unsafe conversions between generic and raw
types (see Chapter 12)
For example, if you compile with the command
javac -Xlint:fallthrough
then the compiler reports missing break statements in switch statements. (The term
“lint” originally described a tool for locating potential problems in C programs, and
is now generically applied to tools that flag constructs that are questionable but not
illegal.)
The following options are available:
-Xlint or -Xlint:all Carries out all checks
-Xlint:deprecation Same as -deprecation, checks for deprecated methods
-Xlint:fallthrough Checks for missing break statements in switch statements
-Xlint:finally Warns about finally clauses that cannot complete normally
-Xlint:none Carries out none of the checks
-Xlint:path Checks that all directories on the class path and source
path exist
-Xlint:serial Warns about serializable classes without serialVersionUID
(see Chapter 1 of Volume II)
-Xlint:unchecked Warns of unsafe conversions between generic and raw
types (see Chapter 12)
Catching Uncaught Exceptions
Having stack traces of uncaught exceptions show up in System.err is not ideal. These
messages are confusing to end users if they happen to see them, and they are not
available for diagnostic purposes when you need them. A better approach is to log
them to a file. As of Java SE 5.0, you can change the handler for uncaught exceptions
with the static Thread.setDefaultUncaughtExceptionHandler method:
Thread.setDefaultUncaughtExceptionHandler(
new Thread.UncaughtExceptionHandler()
{
public void uncaughtException(Thread t, Throwable e)
{
save information in log file
};
});
messages are confusing to end users if they happen to see them, and they are not
available for diagnostic purposes when you need them. A better approach is to log
them to a file. As of Java SE 5.0, you can change the handler for uncaught exceptions
with the static Thread.setDefaultUncaughtExceptionHandler method:
Thread.setDefaultUncaughtExceptionHandler(
new Thread.UncaughtExceptionHandler()
{
public void uncaughtException(Thread t, Throwable e)
{
save information in log file
};
});
System err and System out
It is often handy to trap program errors in a file. However, errors are sent to
System.err, not System.out. Therefore, you cannot simply trap them by running
java MyProgram > errors.txt
Instead, capture the error stream as
java MyProgram 2> errors.txt
To capture both System.err and System.out in the same file, use
java MyProgram >& errors.txt
This works in bash and the Windows shell.
System.err, not System.out. Therefore, you cannot simply trap them by running
java MyProgram > errors.txt
Instead, capture the error stream as
java MyProgram 2> errors.txt
To capture both System.err and System.out in the same file, use
java MyProgram >& errors.txt
This works in bash and the Windows shell.
Subscribe to:
Posts (Atom)