Java SE 6 with HotSpot VM

本文介绍了 HotSpot 虚拟机中用于诊断和故障排查的命令行选项,包括动态更改标志值的方法、堆内存溢出时的处理选项、错误发生时的响应策略等,并详细解释了几个常用的 -XX 选项。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

Command-Line Options

This section describes some command line options that can be useful in diagnosing problems.

B.1 HotSpot VM Command-Line Options

Command-line options that are prefixed with -XX are specific to the Java HotSpot Virtual Machine. Many of these options are important for performance tuning and diagnostic purposes, and are therefore described in this appendix. All the -XX options are described at http://java.sun.com/docs/hotspot/VMOptions.html.

B.1.1 Dynamic Changing of Flag Values

With the jinfo -flag command (2.6 jinfo Utility) and with the JConsole utility (2.3 JConsole Utility), you can dynamically set, unset, or change the value of certain Java VM flags for a specified Java process.

For the complete list of these flags, use the MBeans tab of the JConsole utility. See the list of values for the DiagnosticOptions attribute of the HotSpotDiagnosticMBean, which is in the com.sun.management domain. These flags are the following:

  • HeapDumpOnOutOfMemoryError

  • HeapDumpPath

  • PrintGC

  • PrintGCDetails

  • PrintGCTimeStamps

  • PrintClassHistogram

  • PrintConcurrentLocks

B.1.2 -XX:+HeapDumpOnOutOfMemoryError Option

The -XX:+HeapDumpOnOutOfMemoryError command-line option tells the HotSpot VM to generate a heap dump when an allocation from the Java heap or the permanent generation cannot be satisfied. There is no overhead in running with this option, and so it can be useful for production systems where OutOfMemoryError takes a long time to surface.

You can also specify this option at runtime with the MBeans tab in the jconsole utility.

The heap dump is in HPROF binary format, and so it can be analyzed using any tools that can import this format. For example, the jhat tool can be used to do rudimentary analysis of the dump. See 2.5 jhat Utility.

The following example shows the result of running out of memory with this flag set.

$ java -XX:+HeapDumpOnOutOfMemoryError -mn256m -mx512m ConsumeHeap
java.lang.OutOfMemoryError: Java heap space
Dumping heap to java_pid2262.hprof ...
Heap dump file created [531535128 bytes in 14.691 secs]
Exception in thread "main" java.lang.OutOfMemoryError: Java heap space
        at ConsumeHeap$BigObject.(ConsumeHeap.java:22)
        at ConsumeHeap.main(ConsumeHeap.java:32)

The ConsumeHeap fills up the Java heap and runs out of memory. When java.lang.OutOfMemoryError is thrown, a heap dump file is created. In this case the file is 507MB and is created as java_pid2262.hprof in the current directory.

By default the heap dump is created in a file called java_pidpid.hprof in the working directory of the VM, as in the example above. You can specify an alternative file name or directory with the -XX:HeapDumpPath= option. For example -XX:HeapDumpPath=/disk2/dumps will cause the heap dump to be generated in the /disk2/dumpsdirectory.

B.1.3 -XX:OnError= Option

When a fatal error occurs, the HotSpot Virtual Machine can optionally execute a user-supplied script or command. The script or command is specified using the -XX:OnError=string command line option, where string is a single command, or a list of commands separated by a semicolon. Within string, all occurrences of %p are replaced with the current process ID (pid), and all occurrences of %% are replaced by a single %. The following examples demonstrate how this option can be used.

On Solaris OS the pmap command displays information about the address space of a process. In the following example, if a fatal error occurs, the pmap command is executed to display the address space of the process.

java -XX:OnError="pmap %p" MyApplication

The following example shows how the fatal error report can be mailed to a support alias when a fatal error is encountered

java -XX:OnError="cat hs_err_pid%p.log|mail support@acme.com" \ MyApplication

On Solaris OS the gcore command creates a core image of the specified process, and the dbx command launches the debugger. In the following example, the gcorecommand is executed to create the core image, and the debugger is started to attach to the process.

java -XX:OnError="gcore %p;dbx - %p" MyApplication

In the following Linux example, the gdb debugger is launched when an unexpected error is encountered. Once launched, gdb will attach to the VM process

java -XX:OnError="gdb - %p" MyApplication

On Windows the Dr. Watson debugger can be configured as the post-mortem debugger so that a crash dump is created when an unexpected error is encountered. See 7.4.4 Collecting Crash Dumps on Windows for other details.

An alternate approach to obtaining a crash dump on Windows it to use the -XX:OnError option to execute the userdump.exe utility, as follows.

java -XX:OnError="userdump.exe %p" MyApplication

The userdump utility is part of the Microsoft OEM Support Tools package, which can be downloaded from the Microsoft site. See 7.4.4 Collecting Crash Dumps on Windows for further details.

The above example assumes that the path to the userdump.exe utility is defined in the PATH variable.

B.1.4 -XX:+ShowMessageBoxOnError Option

When the option -XX:+ShowMessageBoxOnError is set and a fatal error is encountered, the HotSpot VM will display information about the fatal error and prompt the user to specify whether the native debugger is to be launched. In the case of Solaris OS and Linux, the output and prompt are sent to the application console (standard input and standard output). In the case of Windows, a Windows message box pops up.

Below is an example from a fatal error encountered on a Linux system.

==============================================================================
Unexpected Error
------------------------------------------------------------------------------
SIGSEGV (0xb) at pc=0x2000000001164db1, pid=10791, tid=1026

Do you want to debug the problem?

To debug, run 'gdb /proc/10791/exe 10791'; then switch to thread 1026
Enter 'yes' to launch gdb automatically (PATH must include gdb)
Otherwise, press RETURN to abort...
==============================================================================

In this case a SIGSEGV error has occurred and the user is prompted to specify whether the gdb debugger is to be launched to attach to the process. If the user enters y oryesgdb will be launched (assuming it is on the PATH variable).

On Solaris OS the message is similar to the above except that the user is prompted to start the dbx debugger. On Windows a message box is displayed. If the user presses the YES button, the VM will attempt to start the default debugger. This debugger is configured by a registry setting; see 7.4.4 Collecting Crash Dumps on Windows for further details. If Microsoft Visual Studio is installed, the default debugger is typically configured to be msdev.exe.

In the above example the output includes the process ID (10791 in this case) and also the thread ID (1026 in this case). If the debugger is launched, one of the initial steps in the debugger might be to select the thread and obtain its stack trace.

As the process is waiting for a response it is possible to use other tools to obtain a crash dump or query the state of the process. On Solaris OS, for example, a core dump can be obtained using the gcore utility.

On Windows a Dr. Watson crash dump can be obtained using the userdump or windbg programs. The windbg program is included in Microsoft's Debugging Tools for Windows. See 7.4.4 Collecting Crash Dumps on Windows for further information on windbg and the link to the download location. In windbg select the Attach to a Process menu option, which displays the list of processes and prompts for the process ID. The HotSpot VM displays a message box, which includes the process ID. Once selected the .dump /f command can be used to force a crash dump. In the following example a crash dump is created in file crash.dump.

Creating Crash Dump with windbg
Sample output of crash dump from windbg

In general the -XX:+ShowMessageBoxOnError option is more useful in a development environment where debugger tools are available. The -XX:OnError option is more suitable for production environments where a fixed sequence of commands or scripts are executed when a fatal error is encountered.

B.1.5 Other -XX Options

Several other -XX command-line options can be useful in troubleshooting.

  • -XX:OnOutOfMemoryError=string is used to specify a command or script to execute when an OutOfMemoryError is first thrown.

  • -XX:ErrorFile=filename is used to specify a location for the fatal error log file. See C.1 Location of Fatal Error Log.

  • -XX:HeapDumpPath=path is used to specify a location for a heap dump. See B.1.2 -XX:+HeapDumpOnOutOfMemoryError Option.

  • -XX:MaxPermSize=size is used to specify the size of the permanent generation memory. See 3.1.2 Detail Message: PermGen space.

  • -XX:+PrintCommandLineFlags is used to print all the VM command-line flags. See 7.3 Collecting Data for a Bug Report.

  • -XX:+PrintConcurrentLocks will cause the Ctrl-Break handler to print a list of concurrent locks owned by each thread.

  • -XX:+PrintClassHistogram will cause the Ctrl-Break handler to print a heap histogram.

  • -XX:+PrintGCDetails and -XX:+PrintGCTimeStamps are used to print detailed information about garbage collection. See B.2.3 -verbose:gc Option.

  • -XX:+UseAltSigs is used (on Solaris 8 and 9 OS) to instruct the HotSpot VM to use alternate signals to SIGUSR1 and SIGUSR2. See 6.1 Signal Handling on Solaris OS and Linux.

  • -XX:+UseConcMarkSweepGC-XX:+UseSerialGC, and -XX:+UseParallelGC specify the garbage collection policy to be used. See 4.2.2 Crash During Garbage Collection.

B.2 Other Command-Line Options

In addition to the -XX options, many other command-line options can provide troubleshooting information. This section describes a few of these options.

B.2.1 -Xcheck:jni Option

The -Xcheck:jni option is useful in diagnosing problems with applications that use the Java Native Interface (JNI). Sometimes bugs in the native code can cause the HotSpot VM to crash or behave incorrectly.

The -Xcheck:jni option is added to the command line that starts the application, as in the following example.

java -Xcheck:jni MyApplication

The -Xcheck:jni option causes the VM to do additional validation on the arguments passed to JNI functions. Note that the option is not guaranteed to find all invalid arguments or diagnose logic bugs in the application code, but it can help diagnose a large number of such problems.

When an invalid argument is detected, the VM prints a message to the application console or to standard output, prints the stack trace of the offending thread, and aborts the VM.

In the following example, a NULL value was incorrectly passed to a JNI function that does not allow a NULL value.

FATAL ERROR in native method: Null object passed to JNI
    at java.net.PlainSocketImpl.socketAccept(Native Method)
    at java.net.PlainSocketImpl.accept(PlainSocketImpl.java:343)
    - locked <0x450b9f70> (a java.net.PlainSocketImpl)
    at java.net.ServerSocket.implAccept(ServerSocket.java:439)
    at java.net.ServerSocket.accept(ServerSocket.java:410)
    at org.apache.tomcat.service.PoolTcpEndpoint.acceptSocket
                        (PoolTcpEndpoint.java:286)
    at org.apache.tomcat.service.TcpWorkerThread.runIt
                        (PoolTcpEndpoint.java:402)
    at org.apache.tomcat.util.ThreadPool$ControlRunnable.run
                        (ThreadPool.java:498)
    at java.lang.Thread.run(Thread.java:536)

In the following example, an incorrect argument was provided to a JNI function that expects a jfieldID argument.

FATAL ERROR in native method: Instance field not found in JNI get/set 
                        field operations
        at java.net.PlainSocketImpl.socketBind(Native Method)
        at java.net.PlainSocketImpl.bind(PlainSocketImpl.java:359)
        - locked <0xf082f290> (a java.net.PlainSocketImpl)
        at java.net.ServerSocket.bind(ServerSocket.java:318)
        at java.net.ServerSocket.<init>(ServerSocket.java:185)
        at jvm003a.<init>(jvm003.java:190)
        at jvm003a.<init>(jvm003.java:151)
        at jvm003.run(jvm003.java:51)
        at jvm003.main(jvm003.java:30)

The following list presents examples of other problems that the -Xcheck:jni option can help diagnose.

  • Cases where the JNI environment for the wrong thread is used

  • Cases where an invalid JNI reference is used

  • Cases where a reference to a non-array type is provided to a function that requires an array type

  • Cases where a non-static field ID is provided to a function that expects a static field ID

  • Cases where a JNI call is made with an exception pending

In general, all errors detected by the -Xcheck:jni option are fatal errors, that is, the error is printed and the VM is aborted. There is one exception to this behavior. When a JNI call is made within a JNI critical region, the following non-fatal warning message is printed.

Warning: Calling other JNI functions in the scope of 
Get/ReleasePrimitiveArrayCritical or Get/ReleaseStringCritical

A JNI critical region is created when native code uses the JNI functions GetPrimitiveArrayCritical or GetStringCritical to obtain a reference to an array or string in the Java heap. The reference is held until the native code calls the corresponding release function. The code between the get and release is called a JNI critical section and during that time the HotSpot VM cannot bring the VM to a state that allows garbage collection to occur. The general recommendation is not to use other JNI functions within a JNI critical section, and in particular any JNI function that could potentially cause a deadlock. The warning printed above by the -Xcheck:jni option is thus an indication of a potential issue; it does not always indicate an application bug.

For more information on JNI, refer to the Java Native Interface documentation web site.

B.2.2 -verbose:class Option

The -verbose:class option enables logging of class loading and unloading.

B.2.3 -verbose:gc Option

The -verbose:gc option enables logging of garbage collection (GC) information. It can be combined with other HotSpot VM specific options such as -XX:+PrintGCDetails and -XX:+PrintGCTimeStamps to get further information about the GC. The information output includes the size of the generations before and after each GC, total size of the heap, the size of objects promoted, and the time taken.

These options, together with detailed information about GC analysis and tuning, are described at the GC Portal site.

The -verbose:gc option can be dynamically enabled at runtime using the management API or JVM TI. See section 2.17 Developing Diagnostic Tools for further information on these APIs.

The jconsole monitoring and management tool can also enable or disable the option when the tool is attached to a management VM. See 2.3 JConsole Utility.

B.2.4 -verbose:jni Option

The -verbose:jni option enables logging of JNI. When a JNI or native method is resolved, the HotSpot VM prints a trace message to the application console (standard output). It also prints a trace message when a native method is registered using the JNI RegisterNative function. The -verbose:jni option can be useful in diagnosing issues with applications that use native libraries.

基于数据挖掘的音乐推荐系统设计与实现 需要一个代码说明,不需要论文 采用python语言,django框架,mysql数据库开发 编程环境:pycharm,mysql8.0 系统分为前台+后台模式开发 网站前台: 用户注册, 登录 搜索音乐,音乐欣赏(可以在线进行播放) 用户登陆时选择相关感兴趣的音乐风格 音乐收藏 音乐推荐算法:(重点) 本课题需要大量用户行为(如播放记录、收藏列表)、音乐特征(如音频特征、歌曲元数据)等数据 (1)根据用户之间相似性或关联性,给一个用户推荐与其相似或有关联的其他用户所感兴趣的音乐; (2)根据音乐之间的相似性或关联性,给一个用户推荐与其感兴趣的音乐相似或有关联的其他音乐。 基于用户的推荐和基于物品的推荐 其中基于用户的推荐是基于用户的相似度找出相似相似用户,然后向目标用户推荐其相似用户喜欢的东西(和你类似的人也喜欢**东西); 而基于物品的推荐是基于物品的相似度找出相似的物品做推荐(喜欢该音乐的人还喜欢了**音乐); 管理员 管理员信息管理 注册用户管理,审核 音乐爬虫(爬虫方式爬取网站音乐数据) 音乐信息管理(上传歌曲MP3,以便前台播放) 音乐收藏管理 用户 用户资料修改 我的音乐收藏 完整前后端源码,部署后可正常运行! 环境说明 开发语言:python后端 python版本:3.7 数据库:mysql 5.7+ 数据库工具:Navicat11+ 开发软件:pycharm
MPU6050是一款广泛应用在无人机、机器人和运动设备中的六轴姿态传感器,它集成了三轴陀螺仪和三轴加速度计。这款传感器能够实时监测并提供设备的角速度和线性加速度数据,对于理解物体的动态运动状态至关重要。在Arduino平台上,通过特定的库文件可以方便地与MPU6050进行通信,获取并解析传感器数据。 `MPU6050.cpp`和`MPU6050.h`是Arduino库的关键组成部分。`MPU6050.h`是头文件,包含了定义传感器接口和函数声明。它定义了类`MPU6050`,该类包含了初始化传感器、读取数据等方法。例如,`begin()`函数用于设置传感器的工作模式和I2C地址,`getAcceleration()`和`getGyroscope()`则分别用于获取加速度和角速度数据。 在Arduino项目中,首先需要包含`MPU6050.h`头文件,然后创建`MPU6050`对象,并调用`begin()`函数初始化传感器。之后,可以通过循环调用`getAcceleration()`和`getGyroscope()`来不断更新传感器读数。为了处理这些原始数据,通常还需要进行校准和滤波,以消除噪声和漂移。 I2C通信协议是MPU6050与Arduino交互的基础,它是一种低引脚数的串行通信协议,允许多个设备共享一对数据线。Arduino板上的Wire库提供了I2C通信的底层支持,使得用户无需深入了解通信细节,就能方便地与MPU6050交互。 MPU6050传感器的数据包括加速度(X、Y、Z轴)和角速度(同样为X、Y、Z轴)。加速度数据可以用来计算物体的静态位置和动态运动,而角速度数据则能反映物体转动的速度。结合这两个数据,可以进一步计算出物体的姿态(如角度和角速度变化)。 在嵌入式开发领域,特别是使用STM32微控制器时,也可以找到类似的库来驱动MPU6050。STM32通常具有更强大的处理能力和更多的GPIO口,可以实现更复杂的控制算法。然而,基本的传感器操作流程和数据处理原理与Arduino平台相似。 在实际应用中,除了基本的传感器读取,还可能涉及到温度补偿、低功耗模式设置、DMP(数字运动处理器)功能的利用等高级特性。DMP可以帮助处理传感器数据,实现更高级的运动估计,减轻主控制器的计算负担。 MPU6050是一个强大的六轴传感器,广泛应用于各种需要实时运动追踪的项目中。通过 Arduino 或 STM32 的库文件,开发者可以轻松地与传感器交互,获取并处理数据,实现各种创新应用。博客和其他开源资源是学习和解决问题的重要途径,通过这些资源,开发者可以获得关于MPU6050的详细信息和实践指南
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值