Automatically launching a pre-set configuration in VisualVM - java

I am trying to profile functions in my java application using VisualVM (eclipse plug-in). Right now when I run an application, eclipse opens VisualVM and takes me to the profilers tab where I can start profiling by pressing the CPU or Memory buttons. Unfortunately, in the time that I take to manually do this, I lose significant number to iterations in my application to get a precise result.
I know I can set the default profiler setting for an application under Tools->Options->Profiling. But is there an option in VisualVM where it can automatically fire the profiler on seeing a particular main function/application being launched.

Related

Profiling junit tests with intellij and jprofiler

I'm trying to profile a junit test - all I care about is hot spots and call tree - I want to see where the test is taking time. I'm running the test by right clicking in intellij idea and saying "profile ..." The test runs in less than a second, and then finishes - even if I can get there quick enough to click call tree and click start recording (haven't found any way of getting it recording immediately) - anything I haven't managed to open by the time it finishes is not available, saying things along the lines of "no data was received because the profiled JVM has terminated.
How do I right click and run a test, have it capture all call history and timings in such a way that I can browse it all once the test has completed? (or is there an alternative/better way of doing this?)
In the profiling settings, on the "Miscellaneous" tab, there is a setting called "Keep VM alive". If you select that check box, the VM will not terminate as long as the JProfiler GUI is connected to it.
Update 2017-11-28
Since JProfiler 10, this setting is no longer in the profiling settings, but in the startup dialog. Also, there is now an option to automatically save a snapshot when the JVM exits. This has the advantage of not blocking the test runner in the IDE.

Automatically start CPU profiling with VisualVM

I'm totally new to VisualVM.
Is there a way to automatically start CPU profiling? My issue is that I start a program from Eclipse which runs for approx. 20 seconds. By the time I can open up the tab and click on CPU profiling, the app has almost finished processing.
I don't know how to automatically start profiling but I have a workaround.
You can set a breakpoint at the start of your program's main and start the program from Eclipse in debug mode. After beginning to profile in VisualVM resume your program in Eclipse.
https://visualvm.github.io/startupprofiler.html
Startup Profiler plugin for VisualVM 1.3.6 and newer enables instrumented profiling of local Java 5+ applications from their startup. It is also helpful when profiling short-running processes.
I added a delay at the first line of the executing code, so I have time to start VisualVM profiling:
TimeUnit.SECONDS.sleep(10);
Using VisualVM's Startup Profiler plugin was time consuming to configure, and I wasn't able to get the debug/breakpoint method working.

CPU/Memory Tab Not Showing Up When Profiling With Java Visual VM

When I try to profile my java application with Java VisualVM the Threads, CPU and Memory profiling tabs do not show up. I do not have any JVM arguments for my program apart from "-Djava.library.path=lib/native", is there an specific argument that I need to use to enable profiling?
Other information:
Built with Netbeans
Uses LWJGL
Just so this will get removed from the Unanswered list:
The problem was resolved by running the same version of VisualVM as the JVM (64-bits in this case). The successful profiling can be version specific.

Profile Entire Java Program Execution in VisualVM

In Java profiling, it seems like all (free) roads nowadays lead to the VisualVM profiler included with JDK6. It looks like a fine program, and everyone touts how you can "attach it to a running process" as a major feature. The problem is, that seems to be the only way to use it on a local process. I want to be able to start my program in the profiler, and track its entire execution.
I have tried using the -Xrunjdwp option described in how to profile application startup with visualvm, but between the two transport methods (shared memory and server), neither is useful for me. VisualVM doesn't seem to have any integration with the former, and VisualVM refuses to connect to localhost or 127.0.0.1, so the latter is no good either. I also tried inserting a simple read of System.in into my program to insert a pause in execution, but in that case VisualVM blocks until the read completes, and doesn't allow you to start profiling until after execution is under way. I have also tried looking into the Eclipse plugin but the website is full of dead links and the launcher just crashes with a NullPointerException when I try to use it (this may no longer be accurate).
Coming from C, this doesn't seem like a particularly difficult task to me. Am I just missing something or is this really an impossible request? I'm open to any kinds of suggestions, including using a different (also free) profiler, and I'm not averse to the command line.
Consider using HPROF and opening the data file with a tool like HPjmeter - or just reading the resulting text file in your favorite editor.
Command used: javac -J-agentlib:hprof=heap=sites Hello.java
SITES BEGIN (ordered by live bytes) Fri Oct 22 11:52:24 2004
percent live alloc'ed stack class rank self accum bytes objs bytes objs trace name
1 44.73% 44.73% 1161280 14516 1161280 14516 302032 java.util.zip.ZipEntry
2 8.95% 53.67% 232256 14516 232256 14516 302033 com.sun.tools.javac.util.List
3 5.06% 58.74% 131504 2 131504 2 301029 com.sun.tools.javac.util.Name[]
4 5.05% 63.79% 131088 1 131088 1 301030 byte[]
5 5.05% 68.84% 131072 1 131072 1 301710 byte[]
HPROF is capable of presenting CPU usage, heap allocation statistics,
and monitor contention profiles. In addition, it can also report
complete heap dumps and states of all the monitors and threads in the
Java virtual machine.
The best way to solve this problem without modifying your application, is to not use VisualVM at all. As far as other free options are concerned, you could use either Eclipse TPTP or the Netbeans profiler, or whatever comes with your IDE.
If you can modify your application, to suspend it's state while you setup the profiler in VisualVM, it is quite possible to do so, using the VisualVM Eclipse plugin. I'm not sure why you are getting the NullPointerException, since it appears to work on my workstation. You'll need to configure the plugin by providing the path to the jvisualvm binary and the path of the JDK; this is done by visiting the VisualVM configuration dialog at Windows -> Preferences -> Run/Debug - > Launching -> VisualVM Configuration (as shown in the below screenshot).
You'll also need to configure your application to start with the VisualVM launcher, instead of the default JDT launcher.
All application launches from Eclipse, will now result in VisualVM tracking the new local JVM automatically, provided that VisualVM is already running. If you do not have VisualVM running, then the plugin will launch VisualVM, but it will also continue running the application.
Inferring from the previous sentence, it is evident that having the application halt in the main() method before performing any processing is quite useful. But, that is not the main reason for suspending the application. Apparently, VisualVM or its Eclipse plugin does not allow for automatically starting the CPU or memory profilers. This would mean that these profilers would have to be started manually, thereby necessitating the need to suspend the application.
Additionally, it is worth noting that adding the flags: -Xrunjdwp:transport=dt_socket,address=8000,server=y,suspend=y to the JVM startup will not help you in the case of VisualVM, to suspend the application and setup up the profilers. The flags are meant to help you in the case of profilers that can actually connect to the open port of the JVM, using the JDWP protocol. VisualVM does not use this protocol and therefore you would have to connect to the application using JDB or a remote debugger; but that would not resolve the problem associated with profiler configuration, as VisualVM (at least as of Java 6 update 26) does not allow you to configure the profilers on a suspended process as it simply does not display the Profiler tab.
This is now possible with the startup profiler plugin to VisualVM.
The advice with -Xrunjdwp is incorrect. It just enables debugger and with suspend=y it waits for debugger to attach. Since VisualVM is not debugger, it does not help you. However inserting System.in or Thread.sleep() will pause the startup and allows VisualVM to attach to your application. Be sure to read Profiling with VisualVM 1 and Profiling with VisualVM 2 to better understand profiler settings. Note also that instead of profiling, you can use 'Sampler' tab in VisualVM, which is more suitable for profiling entire java program execution. As other mentioned you can also use NetBeans Profiler, which directly support profiling of the application startup.

monitor Java process memory in realtime

I have a java application that its memory starting to jump and fall after few days.
Is there a tool that can show me the variables/members sizes during run/debug in real time?
Debugging it with eclipce is impossible.
Check out jhat and jmap.
In the longer run, consider adding a monitor to your app (with JMX, or write your own). It may help you in many situations in future.
Install this: http://visualvm.java.net/eclipse-launcher.html and then launch using Eclipse. This does not use the debugger, but launches the VisualVM application which lets you monitor the app. You'll need to go into the Run Configurations.. settings to set this up, and select VisualVM launcher at the bottom (Select Other... -> VisualVM launcher). You'll need to go into Preferences to set up the location of VisualVM too.
(On later Eclipse versions, drop it into the drops folder, unpacked).
If you're using >JDK6 and can get access to the machine then jvisualvm.exe may work, part of the JDK (in the bin dir)
yourkit (http://www.yourkit.com/) can do that, but expect a HUGE degradation on the performance of the app, as it has to track the size of each object.
I think it's better if you run yourkit with a few less invasive options, and then you can take snapshots of the memory usage.
I forgot to add, that with yourkit you can run the application without almost any instrumentation (which will slightly degrade the performance of the app) and when the problems start ocurring, you can enable the more heavy weight profiling while the app is running.

Categories