Java Thread Dumps and Other MacOS Hacks
By seapegasus on Feb 16, 2007
Since recently, my copy of NetBeans ponders the developments of world history after each launch, before it deigns to become responsive after 3-4 minutes. So Petr N. suggested to have a look at the Java thread dump. But how?
First I looked in the wrong place: I noticed the IDE was wasting cycles because I saw it in the Activity Monitor utility that I had put into the dock. You can set this utility to show the CPU state (or network load or RAM usage) in the dock's icon. So, first I clicked the Activity Monitor's Analysis button to get a thread dump; but then I noticed this only shows system threads, and not java threads...
So Petr pointed out to me the generating thread dumps section on the NetBeans QA homepage. The tips there do half the trick. It tells you to start the application from the Terminal, and then hit ctrl-\\ to generate the thread dump. Usually, it is output to a file.
But how do I start a MacOS .app file in the Terminal? You can't just type in
/Applications/NetBeans.app into the Terminal utility and hit return.
only works to execute the app in the Finder, but that's not what we want. What we need is the app's package content. Inside the package you will find the UNIX-style executable.
In the Terminal, the right path to NetBeans' package content looks like this:
If you get an error message, go to the Finder and right-click onto the NetBeans.app symbol, then select "show package content" to identify the correct path by looking at the directory structure in the Finder. You can drag and drop the last
...bin/netbeans file icon from the Finder into the Terminal window to make sure to get the path right!
Press enter to start the application in the Terminal. To trigger the thread dump of the running application, hold down the controlkey and hit backslash (ctrl-\\). On Mac OS, the output goes straight to the Terminal, not into a file. Copy and paste it into a text editor if you want to keep it. Notice that the terminal is irresponsive after you started NetBeans from it that way -- this is normal, the freshly started foreground process blocks further input to the terminal until you switch the process to the background. If you don't need the Terminal anymore, leave it open.
If you need to use the terminal (and don't want to open another window), do the following:
- Press ctrl-Z -- this stops the currently running process and you regain access to the Terminal. Note the application (NetBeans) will be paused and irresponsive now!
- Then type
bginto the Terminal to make the last stopped process run in the background. Note how NetBeans becomes responsive again, and the terminal accepts input too.
- Alternatively, you could have typed
fg. This would make the last stopped process run in the foreground again -- and like before, it would be blocking the terminal.
The latter option is not very useful for applications like NetBeans, but for Terminal applications that actually run in the Terminal window (such as
top). If you want to stop such a process for a second to quickly check another command, go back to the Terminal by pressing ctrl-Z, and resume the process by typing fg.
Watch out, if you close the Terminal now, it will take the child process with it -- and thusly force-quit NetBeans! When you click the close button or quit the Terminal utility, you will get a warning. Note how it says "[...] will quit the processes bash [...] and java" -- the java process is of course NetBeans. But you only need to run the IDE this way to get a thread dump once, you would not do that each day.
I detected some other useful MacOS shortcuts this week (you might know them already, but in case not): Most people know they can switch applications by pressing Apple-tab and Apple-shift-tab. Similarly you can switch windows within one application by pressing Apple-`on a US keyboard or respectively Apple-< on a German one. Part of the reason why I never used some of those was my confusion about the various keyboard layouts. I switch so often between different keyboards (Apple, PC, Sun; laptop and desktop), different languages (German, US, British, Czech), and different operating systems (Mac, Linux, Windows) that my brain gave up keeping track with all the shortcuts. E.g. on a standard Mac or PC desktop keyboard, the ctrl key is in a different place as on a Sun keyboard, and it's different again on a MacBook... Aaaargh... But this window-switcher shortcut is always at the bottom left of the keyboard, so I trust my brain will be able to handle it. ;)
Another useful keyboard shortcut from a similar context is the one that switches keyboard layouts. I need it quit often (reason: see above). It used to be Apple-space, but I think this is Spotlight now. This is why I always go to the System Preferences for Keyboard & Mouse first to fix the keyboard shortcuts: The yellow warning sign next to a shortcut means it's double-booked. In this case double-click the entry to change it by typing in the new shortcut. I chose ctrl-space to switch the keyboard layout because that's similar to Apple-space. Now you can quickly switch between international layouts, which is useful for people who are able to touch-type in another language, e.g. if you quickly need a Czech Haček or a Germän Umlaut.
Have a look at the other shortcuts too, while you are there. Did you know that Apple-Shift-D looks up a word in the dictionary? I couldn't get it working with a German locale to look up English words, but it works well in an anglophone locale. (But don't even think of using it while writing a message in Apple Mail, where this shortcut will send your email half-finished!) And if you don't know the screenshot schortcuts yet, they are worth knowing by heart. Tip: If you press Apple-shift-4 and then immediatly the spacebar, you can select a window to take a screenshot of.
PS: Woah, when I wrote that about the dictionary pop-up, I just recalled a dream I had last night. I don't remember anymore who the dream character was, I only remember that I called Apple-shift-D on him while he was walking around... So the window popped up... and he fell over it... \*giggle\* Children, don't try that at home!