The number of connections that can be open at the same time in is typically 128, where the first three are occupied by the always open stdin(), stdout(), and stderr() connections, which leaves 125 slots available for other types of connections. Connections are used in many places, e.g. reading and writing to file, downloading URLs, communicating with parallel processes over a socket connections (e.g. parallel::makeCluster() and makeClusterPSOCK()), and capturing standard output via text connections (e.g. utils::capture.output()).
availableConnections()freeConnections()
Returns
A non-negative integer, or +Inf if the available number of connections is greater than 16384, which is a limit be set via option parallelly.availableConnections.tries.
How to increase the limit
In R (>= 4.4.0), it is possible to increase the limit of 128 connections to a greater number via command-line option --max-connections=N, e.g.
For R (< 4.4.0), the limit can only be changed by rebuilding from source, because the limited is hardcoded as a
#define NCONNECTIONS 128
in src/main/connections.c .
How the limit is identified
Since the limit might changed, for instance in custom builds or in future releases of , we do not want to assume that the limit is 128 for all installation. Unfortunately, it is not possible to query for what the limit is. Instead, availableConnections() infers it from trial-and-error. until it fails. For efficiency, the result is memoized throughout the current session.
Examples
total <- availableConnections()message("You can have ", total," connections open in this R installation")free <- freeConnections()message("There are ", free," connections remaining")