我可以分配多少内存? [英] How Much Memory Can I Allocate?

查看:96
本文介绍了我可以分配多少内存?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如果我的机器上有 16 GB 的 RAM,我可以为我正在执行的 java 命令行程序分配多少内存?我假设 java -Xmx 16g... 会使我的系统崩溃?


根据评论,我尝试了 java -Xmx16g...,它没有使我的机器崩溃.该程序仍然内存不足.我尝试了 java -Xmx32g...,它确实使我的机器崩溃了.

从下面的评论(确实很有启发性),我想我只需要继续玩分配.

解决方案

虚拟机堆内存的大小由选项-Xms控制和 - Xmx .

第一个指定初始堆大小和第二个最大大小.

分配是在JVM 本身内完成的,而不是在操作系统上完成的.随着需要更多内存,JVM 会分配一块内存,直到达到 Xmx.

如果您需要更多,则会抛出 OutOfMemoryError.

XmsXmx 使用相同的值是很常见的,导致 VM 仅在执行开始时在操作系统中分配内存,不取决于操作系统特定行为.

<小时>

在你的情况下,由于你设置了 32g 作为你的 Xmx 并且你的系统崩溃了,你似乎没有这个内存(swap + memory ram)

如果您手动更改系统的虚拟内存大小,配置将起作用.

If I have 16 GB of RAM on my machine, how much can I allocate to a java command line program I'm executing? I assume java -Xmx 16g... will crash my system?

EDIT:
In light of the comments, I tried java -Xmx16g..., and it did not crash my machine. The program still ran out of memory. I tried java -Xmx32g..., which did crash my machine.

From the comments below (which have been really enlightening), I guess I just need to keep playing around with the allocations.

解决方案

The size the heap memory of the virtual machine is controlled by the options - Xms and - Xmx .

The first specifies the initial heap size and the second maximum size.

The allocation is done within the JVM itself ,and not on the OS. As more memory is needed, the JVM allocates a block of memory until the Xmx is reached.

If you need more than that, an OutOfMemoryError is thrown.

It is common to use the same value for Xms and Xmx, causing the VM to allocate memory in the operating system only at the beginning of an execution, not depending on the OS specific behavior.


In your case, since you set 32g as your Xmx and your system crashed, it seems you don't have this memory (swap + memory ram)

If you manually change the virtual memory size of your system, the configuration will work.

这篇关于我可以分配多少内存?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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