Access stack stacks with good performance?

Recently, we applied a caching solution that is used by almost all application modules / components (about 50 projects). In order to better understand what cache operations are performed in different "places" of the system, we have added logging for the current cache operations, including stack tracing, to know exactly what caused the cache operation.

Our current approach is this: we take the stack trace from the new Throwable (), filter out unnecessary lines and register the remaining stack trace. However, creating a new exception for the journal, unfortunately, is not a cheap operation. Since we do not use the cache directly, but through sleep mode, it is not so easy to find out which caller was invoking the operation without access to the stack trace.

So my question is: is there a more efficient solution for accessing the current glass, then Throwable (). getStackTrace or Thread.currentThread (). getStackTrace ()?

+4
source share
3 answers

Thread.currentThread().getStackTrace() - , @apangin, , Throwable.

, , . N- . 10- , 90%, , .

- , YourKit, . , ( 10)

+4

:

  • 10% backtrace - Throwable();
  • 90% StackTraceElement[] - getStackTrace().

, , new Exception() ( ), e.getStackTrace() .

, ( ) . , . sun.misc.SharedSectets .

. 2 5

Exception e = new Exception();
int depth = Math.min(5, SharedSecrets.getJavaLangAccess().getStackTraceDepth(e));

for (int frame = 2; frame < depth; frame++) {
    StackTraceElement elem = SharedSecrets.getJavaLangAccess().getStackTraceElement(e, frame);
    System.out.println(elem);
}
+7

All Articles