Java 8 HotSpot VM中任何String对象构造的通知 [英] Notification of any String object construction in Java 8 HotSpot VM

查看:161
本文介绍了Java 8 HotSpot VM中任何String对象构造的通知的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

  1. 有没有一种方法可以在不编织或检测rt.jar的情况下(直接或使用反射)获得对String类的构造函数的所有调用?

  1. Is there a way to get notified on all invocations to constructor of String class (either directly or using reflection) without weaving or instrumenting rt.jar?

是否可以仅针对特定程序包中的呼叫过滤这些通知?

Further is it possible to filter these notifications only for calls within a specific package?

还可以使这些通知异步(例如事件),以免降低实际的JVM调用速度

Further is it possible to make these notifications async (like events) so that actual JVM invocations are not slowed down

我的用例是拦截所有正在创建的字符串,在内容上进行模式匹配,并根据某些规则(全部在后端)引发更改,作为某些平台组件的一部分.

My use-case is to intercept all strings being created, make a pattern match on the content and raise alters based on some rules (all in backend) as part of some platform component.

由于我不想使用rt.jar,因此AspectJ似乎毫无疑问(因为LTW无法在Java核心类上完成). JVM TI看来是潜在的工具,但我不确定如何实现.

As I don't want to instrument rt.jar, AspectJ seems to be out of question (as LTW can't be done on java core classes). The potential tool seems to JVM TI, but I am not exactly sure how to achieve it.

谢谢, 赶快

推荐答案

是否有一种方法可以直接(或直接使用反射)在String类的构造函数的所有调用上得到通知,而无需在编译时编织或检测rt.jar?

Is there a way to get notified on all invocations to constructor of String class (either directly or using reflection) without weaving or instrumenting rt.jar in compile time?

您没有编译String类,因此只能在运行时进行编织.是的,这是不创建自定义JVM的唯一方法.

You are not compiling the String class, so you can only do weaving at runtime. And yes, this is the only way without creating a custom JVM.

是否还可以仅针对特定程序包中的呼叫过滤这些通知?

Further is it possible to filter these notifications only for calls within a specific package?

可以使用Reflection.getCallerClass(n)检查呼叫者

It is possible to check the caller with Reflection.getCallerClass(n)

还可以使这些通知异步(例如事件),以免降低实际的JVM调用速度

Further is it possible to make these notifications async (like events) so that actual JVM invocations are not slowed down

所有这些都是非常昂贵的,因为将工作传递给另一个线程.

All this is very expensive as is passing work to another thread.

使内容与模式匹配

make a pattern match on the content

与创建字符串相比,模式匹配非常昂贵.如果您不小心,将使应用程序速度降低一两个数量级.我建议您重新考虑自己的实际需求,看看是否有另一种方法可以解决您想要做的事情.

Pattern matching is very expensive compared to creating a String. If you are not careful you will slow down your application by an order of magnitude or two. I suggest you reconsider your real requirements and see if there is another way to some what you are trying to do.

确定要不要使用探查器来执行此操作.注意:即使是剖析器,通常也只采样子样本,例如每10个分配.有很多免费的,实际上有两个是JVM附带的.我建议使用Flight Recorder跟踪分配情况,因为这样做的开销非常低.

Are you sure you don't want to use a profiler to do this. Note: even profilers generally only sub-sample e.g. every 10th allocation. There is plenty of free ones, in fact two come with the JVM. I suggest using Flight Recorder to track allocations as this has a very low overhead.

这篇关于Java 8 HotSpot VM中任何String对象构造的通知的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆