pan3793 commented on PR #52665:
URL: https://github.com/apache/spark/pull/52665#issuecomment-3626784001

   @mridulm Spark should correctly resolve `JAVA_HOME` env var or fail fast 
when launching by `spark-submit` or other shell scripts, but you are right, 
it's still possible that the user uses Spark as a library and creates 
`SparkContext` from their Java process.
   
   How do you think about code below for Spark 3.5?
   
   ```scala
     private lazy val jmapCmd: Option[String] = {
       def jmapExists(maybeJavaHome: String): Boolean =
         Files.exists(Paths.get(maybeJavaHome, "/bin/jmap"))
   
       Option(System.getenv("JAVA_HOME")).filter(jmapExists)
         .orElse(Option(System.getProperty("java.home")).filter(jmapExists))
         .map(javaHome => javaHome + "/bin/jmap")
     }
   
     /** Return a heap dump. Used to capture dumps for the web UI */
     def getHeapHistogram(): Array[String] = jmapCmd.map { jmap =>
       // From Java 9+, we can use 'ProcessHandle.current().pid()'
       val pid = getProcessName().split("@").head
       val builder = new ProcessBuilder(jmap, "-histo:live", pid)
       val p = builder.start()
       val rows = ArrayBuffer.empty[String]
       Utils.tryWithResource(new BufferedReader(new 
InputStreamReader(p.getInputStream()))) { r =>
         var line = ""
         while (line != null) {
           if (line.nonEmpty) rows += line
           line = r.readLine()
         }
       }
       rows.toArray
     }.getOrElse {
       logWarning("Return empty heap histogram due to jcmd is unavailable.")
       Array.empty
     }
   ```
   
   Additionally, I found that `jmap` is still possible to be missing after Java 
17, for example,
   
   ```
   $ docker run -it ubuntu:24.04 bash
   # apt update
   # apt install openjdk-25-jre-headless
   # ll /usr/lib/jvm/java-25-openjdk-arm64/bin/
   total 280
   drwxr-xr-x 2 root root  4096 Dec  8 12:30 ./
   drwxr-xr-x 6 root root  4096 Dec  8 12:30 ../
   -rwxr-xr-x 1 root root 67480 Oct 23 09:23 java*
   -rwxr-xr-x 1 root root 67504 Oct 23 09:23 jpackage*
   -rwxr-xr-x 1 root root 67504 Oct 23 09:23 keytool*
   -rwxr-xr-x 1 root root 67512 Oct 23 09:23 rmiregistry*
   ```
   
   So, maybe we also want a variant method to find `jmap` for the master branch?
   
   ```scala
     private lazy val jmapCmd: Option[String] = {
       def jmapExists(maybeJavaHome: String): Boolean =
         Files.exists(Paths.get(maybeJavaHome, "/bin/jmap"))
   
       Option(System.getProperty("java.home")).filter(jmapExists)
         .map(javaHome => javaHome + "/bin/jmap")
     }
   ```


-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: [email protected]

For queries about this service, please contact Infrastructure at:
[email protected]


---------------------------------------------------------------------
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]

Reply via email to