Thread dump in Java
Differences between stack and heap in terms of multithreading
Stack is a piece of memory that is closely related to threads. Each thread has its own stack, which stores local variables, method parameters and a call stack. A variable stored on the stack of one thread is not visible to another.
Heap is a shared piece of memory that is shared among all threads. Objects, whether local or of any other level, are created on the heap. To improve performance, a thread usually caches values from the heap onto its stack, in which case the volatile keyword is used to indicate to the thread that a variable should be read from the heap.
How do I share data between two threads?
Data between threads can be shared using a shared object or parallel data structures such as BlockingQueue.
To control the stack size of a thread -Xss JVM startup parameter is used.
Thread dump
HotSpot-based Java runtimes only generate an HPROF dump. The developer has several interactive dump generation methods and one event-based dump method at his disposal.
Interactive methods:
- Using Ctrl+Break: if the command line option -XX:+HeapDumpOnCtrlBreak is set for the running application, then a dump of the HPROF format is generated along with a stream dump when a Ctrl+Break or SIGQUIT event occurs (usually generated using kill -3), which is initiated through the console. This option may not be available in some versions. In this case, you can try using the following option: -Xrunhprof:format=b,file=heapdump.hprof
- Using the jmap tool: the jmap utility, supplied with the /bin/ directory of the JDK, allows you to request an HPROF dump from a running process.
- Using the operating system: you can use the non-destructive gcore command or the destructive kill -6 or kill -11 commands to create a kernel file. Then extract the heap dump from the kernel file using the jmap utility.
- Using the JConsole tool. The dumpHeap operation is exposed in JConsole as a HotSpotDiagnostic MBean. This operation requests the generation of a dump in HPROF format.
Event based method:
- OutOfMemoryError Event: If the -XX:+HeapDumpOnOutOfMemoryError command line option is set for a running application, an HPROF dump is generated when an OutOfMemoryError occurs. This is ideal for "production" systems, as it is almost mandatory for diagnosing memory problems and does not come with constant performance overhead. In older releases of HotSpot Java runtime environments, this event does not have a limit on the number of heap dumps per JVM; newer releases allow a maximum of one heap dump for this event per JVM startup.
Read also:
Comments
Post a Comment