java的免费内存用法

java的免费内存用法

本文介绍了java的免费内存用法的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用Java构建套接字服务器.

I'm using java to build a socket server.

我记录了服务器运行期间发生的许多操作,在每个日志行旁边,我使用以下命令在JVM中写入当前的可用内存:

I'm logging lots of actions which are happening during the server run,and beside each log line I'm writing the current free memory in the JVM using the:

Runtime.getRuntime().freeMemory()

正如我在日志中看到的那样,我没有很多可用内存(大约14-15 MB).

as I can see in my log, I don't have lots of free memory (around 14-15 MB).

我在具有root访问权限的freeBSD服务器上运行此套接字服务器.

I run this socket server on a freeBSD server with root access.

我真的很想调整分配给我的JVM的内存,但我真的不知道如何,而且对于FreeBSD和linux来说,我还是很陌生.

I really want to tune up the memory allocated to my JVM but I don't really know how, and I'm pretty new to freeBSD and linux in general.

推荐答案

关于内存工作原理的假设(尤其是该调用仅显示JVM堆上的空闲" 内存 分配给java-Xms-Xmx命令行参数.或在某些启动脚本中(如果您使用的是应用服务器).

That call only shows "free" memory on the JVM heap, which is allocated with the -Xms and -Xmx command line arguments to java. Or in some startup script if you are using an app server.

为什么您要做的是浪费时间

查看此答案 -jvm-process-memory-on-ubuntu>问题为什么!

See this answer to this question why!

Java(控制可用内存)中的垃圾收集器并未进行调整,以保持尽可能多的可用内存,而是为了平衡性能和响应能力而进行了调整.它只会按需释放内存,拥有最大可用内存并没有这样做的弊端.

The Garbage Collector in Java ( which controls free memory ) isn't tuned to keep as much memory free as possible, it is tuned for a balance of performance and responsiveness. It only frees memory on demand, there is no benefit in having max free memory and lots of downsides to trying to do so.

这将导致不再引用的对象在使用内存"之前徘徊,直到实际需要它们占用的内存为止.这实际上是最佳选择,因为过早删除它们会导致正在运行的代码的性能下降.

This causes objects that aren't referenced anymore to hang around "using memory" until the memory they occupy is actually needed. This is actually optimal because prematurely removing them would cause performance degradation of the running code.

仅当需要释放内存时才非常快地删除它们,这是垃圾收集器的目标,试图保持尽可能多的内存可用.

Removing them very quickly only when memory needs to be freed is the goal of the Garbage Collector not trying to keep as much memory free as possible.

您应该从不必须在正确实施的Java程序中调用System.gc().

You should never have to call System.gc() in a correctly implemented Java program.

避免OutOfMemoryExceptions

  1. -Xmx设置为在服务器上可以承受的最大设置.将-Xms设置为您认为是名义上的最小内存块大小.太小比太大要糟.

  1. Set -Xmx to as large a setting as you can afford on your server. Set -Xms to the minimum size of block of memory you think is nominal. Too small is worse than too big.

不要泄漏引用.

这篇关于java的免费内存用法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-21 13:47