英文:
Why I java telling me that I don't have 3G ram when I have 7,5G unused on my Raspberry pi?
问题
什么地方做错了?
答案1
得分: 2
让我猜猜:32位操作系统,或者32位的JVM(java -version
和 uname -a
命令可以帮助你部分确定这一点)。
32位寻址能提供的总共可访问内存只有4GB(因为 2 ^ 32
等于 4GB),在这4GB的地址空间中,通常大约有1GB被操作系统标记为“抱歉,不行,我不会让你使用这其中的10亿个地址,因为我把那些留给了图形卡的DMA、内核管道或其他我也不知道的东西。我是内核,我说你不能使用那些,就这样,运气不好啊”。这意味着,即使你把多少内存塞进那台机器,32位系统在大约3GB左右的位置调用malloc
时都会失败。
把它降低到大约2.5GB(因为Java需要的不仅仅是堆内存,还有其他一些额外的内存需求,所以要留些余地),或者确保你的操作系统和虚拟机都是64位的,那么这个问题就不存在了。
注:我假设你已经知道这一点,但一般来说,如果你只用-Xmx
选项启动java
,而没有使用-Xms
选项,Java会立即(尝试)分配最大的内存。到目前为止,一些其他的答案似乎误解了你的问题,但需要注意的是,类似java -Xms1g -Xmx3g
这样的命令会让Java正常启动,但当它接近3GB内存使用时可能会崩溃。对于服务器,虽然同时指定-Xms
和-Xmx
是一种常见的建议,但通常不是一个明智的做法 - 你不希望服务器的内存需求如此波动不定,最好让进程立即失败,而不是在后来某个模糊的时间点,恰好在所有客户都连接的时候才失败。将虚拟机从操作系统那里请求的内存的大小调整放到桌面部署中去。
英文:
Let me guess: 32-bit OS, or a 32-bit JVM (java -version
and uname -a
will get you some ways in figuring this out).
32-bit addressing gives you a grand total of just 4GB of accessible memory (Because 2 ^ 32
is 4gb), and of that 4 gb of address space, usually about 1gb is marked off by the OS as 'sorry, nope, I wont let you use any of those 1 billion numbers for addressing, because I reserved those for DMA to the graphics card, or pipes arranged by the kernel or who knows what. I'm the kernel, I say you can't have those, tough luck'. That means the area around which a 32-bit will fail a malloc
call regardless of how much RAM you shoved into that box is just around exactly 3GB.
Lower it to 2.5GB or so (it's not just heap that java needs, there's some extras, so leave some room), or ensure both your OS and your VM are 64-bit, then none of this is an issue.
NB: I assume you already know this, but generally if you start java
with just an -Xmx
and no -Xms
option, java will immediately (try to) allocate that maximum. Some of the other answers so far seem to misunderstand your problem, but note that something like java -Xms1g -Xmx3g
would result in java starting normally, but it will then crash later on when it gets near that 3GB mark. For servers, specifying both -Xms
and -Xmx
, whilst somewhat common advice, is generally a dumb idea - you don't want your server to bounce around its memory requirements like that, better for processes to fail immediately, instead of failing at some nebulous later time juuust when all the customers are connecting. Leave the notion of having the VM shrink and grow the memory it claims from the OS to desktop deployments.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论