GV

GeezerVison

The World Wide Web, restored to 800 pixels of glory.

Home|About|Gaming cabinet|Archives|How it works|Starter sites

On this site

  • java.evolved
  • ←
  • →

This view

  • Original page
  • New address

GeezerVison is showing javaevolved.dev — Lazy stack inspection with StackWalker | java.evolved

Home / Tooling / Lazy stack inspection with StackWalker
Tooling Intermediate

Lazy stack inspection with StackWalker

  • 🐛 Report a code issue
  • 🌐 Report a translation issue
  • 💡 Suggest a new pattern

Inspect stack frames lazily with StackWalker instead of materializing a complete stack trace.

Tooling Performance
Code Comparison
✕ Materialized stack trace
StackTraceElement caller = Thread.currentThread()
        .getStackTrace()[2];
String callerClass = caller.getClassName();
✓ Java 9+
String callerClass = StackWalker.getInstance()
        .walk(frames -> frames.skip(1)
                .findFirst()
                .orElseThrow()
                .getClassName());
Share 𝕏
See a problem with this code? Let us know.
Why the modern way wins
⚡

Lazy traversal

Visits only the frames the operation needs.

🧩

Stream-friendly

Filtering and selection use standard stream operations.

🎛

Configurable

Options support class references and hidden or reflective frames.

Old Approach
Thread.getStackTrace()
Modern Approach
StackWalker
Since JDK
9
Difficulty
Intermediate
JDK Support
Lazy stack inspection with StackWalker
Available

Widely available since JDK 9 (September 2017)

How it works

StackWalker exposes a lazy stream of stack frames and configurable access to retained class references. Code can stop once it finds the needed frame rather than allocating an entire stack-trace array.

Related Documentation
StackWalker ↗ Stack-Walking API (JEP 259) ↗
Proof
View proof source ↗
Related patterns
Tooling Intermediate

JFR for profiling

Java 8
// Install VisualVM / YourKit / JProfiler
// Attach to running process
// Configure sampling
// Export and analyze
// External tool required
Java 9+
// Start with profiling enabled
$ java -XX:StartFlightRecording=
    filename=rec.jfr MyApp

// Or attach to running app:
$ jcmd <pid> JFR.start
Hover to see modern ➜
JDK 9+ →
Concurrency Intermediate

Modern Process API

Java 8
Process p = Runtime.getRuntime()
    .exec("ls -la");
int code = p.waitFor();
// no way to get PID
// no easy process info
Java 9+
ProcessHandle ph =
    ProcessHandle.current();
long pid = ph.pid();
ph.info().command()
    .ifPresent(IO::println);
ph.children().forEach(
    c -> IO.println(c.pid()));
Hover to see modern ➜
JDK 9+ →
Errors Beginner

Helpful NullPointerExceptions

Java 8
// Old NPE message:
// "NullPointerException"
// at MyApp.main(MyApp.java:42)
// Which variable was null?!
Java 14+
// Modern NPE message:
// Cannot invoke "String.length()"
// because "user.address().city()"
// is null
// Exact variable identified!
Hover to see modern ➜
JDK 14+ →
You are visitor 01402503 — GeezerVison does not store the pages you look up.
Best viewed with Netscape Navigator 4.7 or Internet Explorer 6 at 800×600. No cookies required.