简体   繁体   English

Java7 DatagramSocket.setReceiveBufferSize在Windows和Linux之间有什么不同?

[英]Java7 DatagramSocket.setReceiveBufferSize way different between Windows and Linux?

invoking this code on both Linux (Ubuntu 12.04) and Windows 7 you get very different results: 在Linux(Ubuntu 12.04)和Windows 7上调用此代码会得到非常不同的结果:

DatagramSocket socket = new DatagramSocket(4445);
socket.setReceiveBufferSize(Integer.MAX_VALUE);
System.out.println("SO_RX_BUFFER SET TO:"+socket.getReceiveBufferSize());

Linux you get: SO_RX_BUFFER SET TO:131071 您得到的Linux:SO_RX_BUFFER设置为:131071

and on Windows you get: SO_RX_BUFFER SET TO:2147483647 在Windows上,您得到:SO_RX_BUFFER设置为:2147483647

Why the drastic different between operating systems? 为什么操作系统之间存在巨大差异? Why such a small value for linux? 为什么linux这么小的价值? Is there anyway to get the linux buffer size up? 反正有没有让linux缓冲区大小增加?

What is going on here is that the host operating systems are behaving differently. 这里发生的是主机操作系统的行为不同。

  • In the Linux case, the OS is placing an upper limit on what you can ask for from an application. 在Linux案例中,操作系统对应用程序的要求设置了上限。 To address this, you would need to change the OS configuration settings. 要解决此问题,您需要更改操作系统配置设置。 That will require "root" privilege and should not be done by an application. 这将需要“root”特权,不应由应用程序完成。

    This page has some basic information on Linux network tuning. 此页面包含有关Linux网络调整的一些基本信息。

  • In the Windows case, the OS either doesn't place a limit or (more likely) it silently overrides the user requested buffer size if it is larger than an OS imposed limit. 在Windows情况下,操作系统要么没有限制,要么(更有可能)静默覆盖用户请求的缓冲区大小(如果它大于操作系统强加的限制)。


Having said this, you need think about what you are doing here. 说完这个,你需要考虑一下你在这里做了什么。 Buffered network packets (RX and TX) most likely have to be held in physical memory. 缓冲网络数据包(RX和TX)很可能必须保存在物理内存中。 When you increase the limits, you are forcing the OS to reserve more RAM pages for itself, reducing the memory available for normal applications. 当您增加限制时,您强制操作系统为自己保留更多RAM页面,从而减少了正常应用程序可用的内存。

The second issue is that increasing the amount of network buffering can be harmful if the basic problem is that your application has difficulty keeping up. 第二个问题是,如果基本问题是您的应用程序难以跟上,那么增加网络缓冲量可能会有害。 In the worst case, your application will be continually processing old packets, with new packets being discarded. 在最坏的情况下,您的应用程序将持续处理旧数据包,丢弃新数据包。 In short, the extra buffering makes the latency worse without (necessarily) eliminating data loss. 简而言之,额外的缓冲会使延迟变得更糟,而不必(必然)消除数据丢失。

This appears to be a duplicate: Why changing value of SO_RCVBUF doesn't work? 这似乎是重复的: 为什么更改SO_RCVBUF的值不起作用?

Looks like linux has a config file limit. 看起来linux有配置文件限制。

Adding the following: 添加以下内容:

net.core.rmem_max = 16777216 (or whatever you want your limit to be) net.core.rmem_max = 16777216(或任何你想要的限制)

to /etc/sysctl.conf 到/etc/sysctl.conf

resolved the issue. 解决了这个问题。

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

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