如何查看NIO缓冲区的内存使用情况?

发布于 2024-08-29 11:07:34 字数 131 浏览 4 评论 0原文

有没有办法查看 NIO 缓冲区的内存使用情况? JConsole中有非堆内存部分,但我认为这不包括NIO缓冲区?

如果需要的话,操作系统是 Linux(Ubuntu 或 CentOS)。

问候,

维姆

Is there a way to see the memory usage of NIO buffers? There is the non-heap memory section in JConsole, but I do not think this includes the NIO buffers?

Operating system is Linux (Ubuntu or CentOS) if that would matter.

regards,

Wim

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

佞臣 2024-09-05 11:07:34

值得注意的是,直接内存缓冲区仅使用您使用的物理内存(到下一页大小,通常为 4K)。它们在创建时使用虚拟内存,但操作系统足够聪明,不会分配物理内存页面(驻留),直到您使用直接内存缓冲区中的页面。这意味着使用的物理内存大小可能小于您分配的(虚拟)内存量。物理/常驻内存是您应该关心的。

这样做的好处是,即使您只希望使用几 KB,您也可以分配几 MB 缓冲区,并且它只会使用您使用的数量,并且如果由于某种原因您需要更多,您的程序也不会崩溃。即,您不必获得正确的大小,并且可以相当安全地高估分配的大小。

It is worth noting that direct memory buffers only use as much physical memory as you use (to the next page size, typically 4K). They use virtual memory when created but the OS is smart enough not to allocate pages of physical memory (resident) until you use the pages in the direct memory buffer. This means the size of physical memory used can be smaller than the amount of memory you allocate (virtual). The physical/resident memory is the one you should care about.

The benifit of this is you can allocate a few MB buffer even if you only expect to use a few KB and it will only use the amount you use and if for some reason you need more, your program won't blow up. i.e. you don't have to get the size right and can fairly safely over estimate the size allocated.

硪扪都還晓 2024-09-05 11:07:34

是的,从 Linux 通过 ps 命令。示例:

ps x -o command,rss | grep java | grep latest | cut -b 17-

输出将以 KB 为单位。

您可能对我不久前提出的有关 Java、RSS 和 NIO 缓冲区的问题感兴趣:为什么即使堆等大小稳定,Sun JVM 仍继续消耗更多的 RSS 内存?< /em>

Yes, from linux via the ps command. Example:

ps x -o command,rss | grep java | grep latest | cut -b 17-

The output will be in KB.

You may be interested in a question I had a while back regarding Java, RSS and NIO buffers: Why does the Sun JVM continue to consume ever more RSS memory even when the heap, etc sizes are stable?

我的影子我的梦 2024-09-05 11:07:34

我用它来监视虚拟和 RSS 内存以及本机线程的数量:

for((i=0;;++i)) { echo $i ` grep VmSize /proc/\21009/status | grep -o '[0-9]*'` ` grep VmRSS /proc/\21009/status | grep -o '[0-9]*'` ` grep Threads /proc/\21009/status | grep -o '[0-9]*'` ; sleep 1 || break; } > data

它创建一个空格分隔的文件,您可以轻松地将其导入到您最喜欢的电子表格工具中。

当然,您需要将 21009 替换为您要监视的 java 进程的进程 id。

这并不能完全回答我自己的问题,因为我希望能够查看 NIO 分配的内容,而不是 JVM 使用的所有内存,尽管这个小脚本已经证明我足够有用,可以看到没有我正在调查的程序中存在内存泄漏。

I have used this to monitor the Virtual and RSS memory and the amount of native threads:

for((i=0;;++i)) { echo $i ` grep VmSize /proc/\21009/status | grep -o '[0-9]*'` ` grep VmRSS /proc/\21009/status | grep -o '[0-9]*'` ` grep Threads /proc/\21009/status | grep -o '[0-9]*'` ; sleep 1 || break; } > data

It creates a space separated file which you can easily import into your favorite spreadsheet tool.

You need to replace 21009 with the process id of the java process you want to monitor, of course.

This does not fully answer my own question as I wanted to be able to have a look at just what NIO stuff has allocated, not all the memory used by the JVM, although this little script already proved me useful enough to see that there are no memory leaks in a program I was investigating.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文