Skip to content

Instantly share code, notes, and snippets.

@JuniperPhoton
Created July 11, 2019 11:55
Show Gist options
  • Save JuniperPhoton/75ba5dafd6cb817e2b706e4a5cbb57bb to your computer and use it in GitHub Desktop.
Save JuniperPhoton/75ba5dafd6cb817e2b706e4a5cbb57bb to your computer and use it in GitHub Desktop.
Convert Android hprof to Java SE hprof
using System;
using System.Diagnostics;
using System.IO;
namespace AdbTest {
class Program {
static void Main (string[] args) {
if (args.Length == 0) {
Console.WriteLine ("Please specified folder path");
Environment.Exit (-1);
}
var folderPath = args[0];
string[] files = null;
try {
files = Directory.GetFiles (folderPath, "*.hprof");
} catch (DirectoryNotFoundException) {
Console.WriteLine ("Directory not found");
Environment.Exit (-1);
}
if (files.Length == 0) {
Console.WriteLine ("Folder has no *.hprof file");
Environment.Exit (-1);
}
var folder = Directory.CreateDirectory ($"{folderPath}/outputs");
foreach (var path in files) {
var fileName = Path.GetFileName(path);
var command = $"hprof-conv -z {path} {folder}/{fileName}";
Console.WriteLine($"Running command {command}");
command.RunAsBash ();
}
}
}
public static class ShellHelper {
public static string RunAsBash (this string cmd) {
var escapedArgs = cmd.Replace ("\"", "\\\"");
var process = new Process () {
StartInfo = new ProcessStartInfo {
FileName = "/bin/bash",
Arguments = $"-c \"{escapedArgs}\"",
RedirectStandardOutput = true,
UseShellExecute = false,
CreateNoWindow = true,
}
};
process.Start ();
string result = process.StandardOutput.ReadToEnd ();
process.WaitForExit ();
return result;
}
}
}
@JuniperPhoton
Copy link
Author

To run this code:

  1. Make sure you have installed the .Net Core in Windows or macOS or Linux;
  2. New a console project named AdbTest and copy the code;
  3. dotnet build and dotnet /bin/AdbTest.dll ~/path/to/your/hprof/folder

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment