- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
环境:
应用程序代码:
通过 SocketInputStream 进行域套接字通信
行动:
使用异步分析器运行应用程序:-d 60 -e alloc -f/tmp/alloc.svg
问题:
来自 SocketInputStream#socketRead0 的意外字符串分配
(青色:TLAB 分配)
socketRead 和 socketRead0 的 JDK 代码
private int socketRead(FileDescriptor fd,
byte b[], int off, int len,
int timeout)
throws IOException {
return socketRead0(fd, b, off, len, timeout);
}
private native int socketRead0(FileDescriptor fd,
byte b[], int off, int len,
int timeout)
native Socket-Impl:
假设:
在下面的代码中,字符串可能是通过 JNI 在 java 堆中分配的,因为在 StackTrace 中字符串分配旁边有一个 SocketTimeoutException
Java_java_net_SocketInputStream_socketRead0(JNIEnv *env, jobject this,
jobject fdObj, jbyteArray data,
jint off, jint len, jint timeout)
{
[...]
if (timeout) {
nread = NET_ReadWithTimeout(env, fd, bufP, len, timeout);
if ((*env)->ExceptionCheck(env)) {
if (bufP != BUF) {
free(bufP);
}
return nread;
}
} else {
nread = NET_Read(fd, bufP, len);
}
[...]
}
static int NET_ReadWithTimeout(JNIEnv *env, int fd, char *bufP, int len, long timeout) {
int result = 0;
long prevtime = NET_GetCurrentTime(), newtime;
while (timeout > 0) {
result = NET_TimeoutWithCurrentTime(fd, timeout, prevtime);
if (result <= 0) {
if (result == 0) {
JNU_ThrowByName(env, "java/net/SocketTimeoutException", "Read timed out");
} else if (result == -1) {
if (errno == EBADF) {
JNU_ThrowByName(env, "java/net/SocketException", "Socket closed");
} else if (errno == ENOMEM) {
JNU_ThrowOutOfMemoryError(env, "NET_Timeout native heap allocation failed");
} else {
JNU_ThrowByNameWithMessageAndLastError
(env, "java/net/SocketException", "select/poll failed");
}
}
return -1;
}
result = NET_NonBlockingRead(fd, bufP, len);
if (result == -1 && ((errno == EAGAIN) || (errno == EWOULDBLOCK))) {
newtime = NET_GetCurrentTime();
timeout -= newtime - prevtime;
if (timeout > 0) {
prevtime = newtime;
}
} else {
break;
}
}
return result;
}
我搜索了C代码,没有找到任何jString分配,因此我有一些想法。
有人知道字符串分配可能发生在哪里吗?
最佳答案
你的假设是正确的。配置文件中的SocketTimeoutException
告诉该方法分配了一个异常对象,并且这个异常对象有一个String
消息也需要分配。
我刚刚对 async-profiler 进行了更改,添加了 --cstack
选项,用于在分配分析模式下记录 C 堆栈和 Java 堆栈。证明java.lang.String
确实是从JNI ThrowNew
函数分配的:
关于java - 由socketRead0分配的字符串TLAB,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59601848/
我们正在将一个应用程序从 Java 6 迁移到 Java 7。在高层,问题是它的速度较慢。 更详细地说,我们一直在升级类,因为分析器会告诉我们热点。此时我们有 java.net.SocketInput
我有一个应用程序使用保持连接持续轮询远程 HTTP API。 很少但偶尔应用程序会在工作一段时间后静默挂起。 套接字仍处于打开状态,但远程 API 未返回任何数据。 Httpclient 有套接字超时
关闭。此题需要details or clarity 。目前不接受答案。 想要改进这个问题吗?通过 editing this post 添加详细信息并澄清问题. 已关闭 9 年前。 Improve th
我正在使用 Apache HttpClient(来自 Apache HTTP Components 4.3)来对 ShoutCast 流执行 GET: CloseableHttpClient clie
我是一名优秀的程序员,十分优秀!