import java.net.Socket; import java.net.ServerSocket; import java.net.InetAddress; import java.io.DataOutputStream; import java.io.OutputStream; import java.io.BufferedOutputStream; import java.io.FileOutputStream; import java.io.InputStream; import java.io.BufferedInputStream; import java.io.IOException; import java.io.EOFException; import java.util.Date; /** * This hacky class is used to trigger a heap dump on a running VM. To * use, start this program like this:
* * java NetDumper /tmp/foo.hprof 7000 * ** This will cause it to listen to a ServerSocket on port 7000. Next, * invoke the program you want a heap dump for, like this:
* * java -Xhprof:net=localhost:7000,format=b com.random.MyProgram * ** It will connect to NetDumper. Whenever you press return in the NetDumper * window, it will request a heap dump, and write it to /tmp/foo.hprof. **/ public class NetDumper implements Runnable { // // Data members // private InputStream in; private OutputStream out; private int identifierSize; private byte[] buf = new byte[16 * 1024]; public NetDumper(InputStream in, OutputStream out) { this.in = in; this.out = out; } public void run() { try { for(;;) { int read = in.read(buf); if (read == -1) { System.out.println("EOF seen, terminating."); out.close(); System.exit(0); } out.write(buf, 0, read); } } catch (IOException ex) { ex.printStackTrace(); System.exit(1); } } private static void usage() { System.out.println("Usage: java NetDumper