简体   繁体   English

Java正在分配额外的2GB内存

[英]Java is Allocating Extra 2gb of Memory

I got a new VPS to run some java programs that me and some buddies have made. 我有一个新的VPS来运行我和一些好友制作的一些java程序。 I start the process with a line like this: 我用这样的一行开始这个过程:

java -Xmx512M -jar program.jar

On our old VPS, you could use the 'top' command to see how much virtual and resident memory was being used. 在我们的旧VPS上,您可以使用'top'命令查看正在使用的虚拟和驻留内存量。 It would use like 600-700mb of virtual memory. 它会使用600-700mb的虚拟内存。 Now on our new VPS, with that same command, the virtual memory seems to always be an extra ~2gb over the -Xmx value. 现在在我们的新VPS上,使用相同的命令,虚拟内存似乎总是比-Xmx值多出约2GB。 So instead of the virtual memory being around 600-700mb, it's instead 2700-3000mb. 因此,虚拟内存大约在600-700mb左右,而不是2700-3000mb。

The old VPS is running CentOS 5.7 and the new is running CentOS 6.2. 旧的VPS运行CentOS 5.7,新的运行CentOS 6.2。 Both are running JRE 1.7u3 64bit. 两者都运行JRE 1.7u3 64bit。

Why is this and how can I fix it? 为什么这样,我该如何解决?

edit: top 编辑:顶部

PID   USER    PR  NI  VIRT  RES  SHR S %CPU %MEM    TIME+  COMMAND
27645 pyro    20   0 3003m 270m  10m S  5.0  1.7   1:19.18 java -Xmx512M -jar cserver.jar

another edit: I am not questioning why virtual memory is using more memory than specified in the java command line. 另一个编辑:我不怀疑为什么虚拟内存使用的内存比java命令行中指定的多。 I am questioning why it is using so much more than it used to. 我在质疑为什么它比以前使用了更多。

The heap is not the only thing which consumes virtual memory. 堆不是唯一消耗虚拟内存的东西。 Virtual memory is the amount of address space the application has rather than the amount of memory it is using (the resident is a better indicator) 虚拟内存是应用程序具有的地址空间量,而不是它使用的内存量(驻留程序是更好的指标)

Virtual memory includes all the thread stack space, direct memory and memory mapped files. 虚拟内存包括所有线程堆栈空间,直接内存和内存映射文件。

The first thing I would check is the number of threads your application uses, the more threads, the more virtual memory. 我要检查的第一件事是应用程序使用的线程数,线程越多,虚拟内存越多。

Virtual memory usage means how much address space is used and does not necessarily translate directly into RAM usage. 虚拟内存使用意味着使用了多少地址空间,并不一定直接转换为RAM使用。 Stack, mapped files (including binaries and libraries), etc. all contribute to virtual memory but not always to the RAM actually used. 堆栈,映射文件(包括二进制文件和库)等都有助于虚拟内存,但并不总是对实际使用的RAM有用。 Note that your RES (resident in RAM) memory usage is a rather nice 270 MB only. 请注意,您的RES(驻留在RAM中)内存使用量仅为270 MB。 On a 32-bit machine, you may run into address space limitations, so there virtual memory is a scarce resource if you get close to the 2 GB mark (the value may also be 1 GB or 3 GB depending on OS). 在32位计算机上,您可能会遇到地址空间限制,因此如果接近2 GB标记,则虚拟内存是稀缺资源(该值也可能是1 GB或3 GB,具体取决于操作系统)。 On a 64-bit system, virtual memory (address space) is close to unlimited, so by itself, a high value need not be treated as a risk. 在64位系统上,虚拟内存(地址空间)接近无限制,因此高价值本身不需要被视为风险。 Of course, if it is also related to actual high RAM usage or lots of mapped files which you cannot find out why are used, it's worth looking into. 当然,如果它也与实际的高RAM使用率或许多映射文件有关,你无法找到使用它们的原因,那么值得研究。

Of course, JVM also has some actual overhead (in physically allocated memory), related to garbage collector's house keeping, compiler's work, native code etc., and this will also be reflected in virtual memory usage. 当然,JVM也有一些实际的开销(在物理分配的内存中),与垃圾收集器的内务管理,编译器的工作,本机代码等有关,这也将反映在虚拟内存使用中。 But since the RES and SHR positions are not very high, I'd say there's no reason to panic, especially if you're 64-bit. 但由于RES和SHR位置不是很高,我认为没有理由恐慌,特别是如果你是64位。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM