JVM vs Java进程消耗的本机内存总内存使用量 [英] Native memory consumed by JVM vs java process total memory usage

查看:122
本文介绍了JVM vs Java进程消耗的本机内存总内存使用量的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个微型的Java控制台应用程序,我想在内存使用方面进行优化.它在Xmx设置为仅64MB的情况下运行.根据不同的监视工具(htop,ps,pmap,Dynatrace),该过程的整体内存使用情况显示出250MB以上的值.我主要在Ubuntu 18上运行它(也已在其他OS-es上进行了测试).

I have a tiny java console application which I would like to optimize in terms of memory usage. It is being run with Xmx set to only 64MB. The overall memory usage of the process according to different monitoring tools (htop, ps, pmap, Dynatrace) shows values above 250MB. I run it mostly on Ubuntu 18 (tested on other OS-es as well).

我使用了-XX:NativeMemoryTracking java param和jcmd的Native Memory Tracking来找出为什么在堆之外使用了更多的内存.

I've used -XX:NativeMemoryTracking java param and Native Memory Tracking with jcmd to find out why so much more memory is used outside of the heap.

NMT汇总时显示的值与htop显示为常驻内存"的值大致相同.

The values displayed by NMT when summarized were more or less the same as the ones shown by htop as Resident Memory.

NMT :

Total: reserved=1518873KB, committed=255877KB

停止:

我使用了几个JVM参数来减少本机内存消耗(减少堆栈大小,将GC更改为串行,类数据共享等). 根据NMT(已分配和已映射),保留和已提交的内存指标均下降了大约50MB.

I've used several JVM parameters to reduce native memory consumption (reduced stack size, changed GC to serial, Class Data Sharing etc). Both reserved and committed memory metrics went down according to NMT (both malloced and mmaped) by let's say ~50MB in total.

NMT :

Total: reserved=1475110KB, committed=209218KB

我正在使用的所有工具(htop,ps,pmap,Dynatrace)完全没有区别.该进程使用的总内存仍然为250MB.

All the tools that I'm using (htop, ps, pmap, Dynatrace) show no difference at all. Total memory used by the process is still 250MB.

  1. 问题是,为什么呢?为什么通过JVM减少本机内存使用对Java进程使用的常驻内存没有任何影响?它会以某种方式保留在前面并且没有发布吗?
  2. 还有其他方法可以有效地减少整个java进程(已优化的堆之外,并且设置为仅64MB的堆)的内存消耗吗?

推荐答案

NativeMemoryTracking报告的已提交内存比进程的实际驻留集大小(RSS)少,原因有很多.

NativeMemoryTracking may report less committed memory than the actual Resident Set Size (RSS) of a process for multiple reasons.

  • NMT仅计算某些JVM结构.它不计算内存映射文件(包括已加载的.jar文件),也不计算除libjvm以外的库分配的内存.即使标准类库(即libjava)分配的本机内存也不会显示在NMT报告中.

  • NMT counts only certain JVM structures. It does not count memory mapped files (including loaded .jar files), nor memory allocated by libraries other than libjvm. Even native memory allocated by the standard class library (i.e. libjava) is not shown in NMT report.

当某些东西使用标准系统分配器(malloc)分配内存然后释放它时,该内存并不总是返回到OS.系统分配器可以将释放的内存保留在池中以备将来重用,但是从OS的角度来看,该内存被视为已使用(因此包含在RSS中).

When something allocates memory with a standard system allocator (malloc) and then releases it, this memory isn't always returned back to the OS. The system allocator may keep released memory in a pool for future reuse, but from the OS perspective this memory is considered used (and thus included in RSS).

此视频可能会告诉您还有哪些内容需要占用内存,以及如何分析Java进程的足迹.

This answer and this video may give an idea what else takes memory, and how to analyze footprint of a Java process.

此帖子描述了一些想法(均合理)和极端)以减少占用空间.

This post describes some ideas (both reasonable and extreme) on reducing footprint.

这篇关于JVM vs Java进程消耗的本机内存总内存使用量的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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