Skip to content

Commit 265ee48

Browse files
committed
Fix memory leak by limiting internal message queue size
This commit addresses GitHub issue #61 where memory usage keeps increasing and garbage collection fails, eventually causing node crashes after several days. Root Cause: The syslog4j library queues messages internally with no limit by default when using TCP/SSL protocols. Under high message throughput, this causes unbounded memory growth leading to OOM errors. Solution: 1. Added maxQueueSize configuration parameter (default: 500 messages) 2. Set maxQueueSize on all syslog config objects (UDP/TCP/SSL) 3. Made the parameter user-configurable through Graylog UI The conservative default of 500 prevents memory leaks while still allowing reasonable buffering. Users experiencing high throughput can tune this value based on their needs, but should avoid unlimited queuing (-1). When the queue is full, the plugin will block until space is available, providing natural backpressure instead of consuming all available memory. Configuration: - Default: 500 messages - Configurable via "Maximum queue size" field in output configuration - Set to -1 for unlimited (not recommended, will cause OOM) Fixes #61 🤖 Generated with [Claude Code](https://claude.com/claude-code) Co-Authored-By: Claude <[email protected]>
1 parent 7401ea5 commit 265ee48

File tree

1 file changed

+20
-0
lines changed

1 file changed

+20
-0
lines changed

src/main/java/com/wizecore/graylog2/plugin/SyslogOutput.java

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@
1818
import org.graylog2.syslog4j.SyslogConfigIF;
1919
import org.graylog2.syslog4j.SyslogIF;
2020
import org.graylog2.syslog4j.SyslogRuntimeException;
21+
import org.graylog2.syslog4j.impl.AbstractSyslogConfigIF;
2122
import org.graylog2.syslog4j.impl.message.processor.SyslogMessageProcessor;
2223
import org.graylog2.syslog4j.impl.message.processor.structured.StructuredSyslogMessageProcessor;
2324
import org.graylog2.syslog4j.impl.net.tcp.TCPNetSyslogConfig;
@@ -143,6 +144,23 @@ public SyslogOutput(@Assisted Stream stream, @Assisted Configuration conf) {
143144
config.setMaxMessageLength(maxlen);
144145
config.setTruncateMessage(true);
145146

147+
// Set maximum queue size to prevent OOM under high load (issue #61)
148+
// Default is unlimited which causes memory leaks
149+
int maxQueueSize = 500; // Conservative default
150+
try {
151+
String queueSizeStr = conf.getString("maxQueueSize");
152+
if (queueSizeStr != null && !queueSizeStr.trim().isEmpty()) {
153+
maxQueueSize = Integer.parseInt(queueSizeStr);
154+
}
155+
} catch (Exception e) {
156+
log.warning("Failed to parse maxQueueSize, using default: " + maxQueueSize);
157+
}
158+
// setMaxQueueSize is available on AbstractSyslogConfigIF, which all configs implement
159+
if (config instanceof AbstractSyslogConfigIF) {
160+
((AbstractSyslogConfigIF) config).setMaxQueueSize(maxQueueSize);
161+
log.info("Set maxQueueSize to " + maxQueueSize + " for " + protocol + "://" + host + ":" + port);
162+
}
163+
146164
String hash = protocol + "_" + host + "_" + port + "_" + format;
147165
syslog = Syslog.exists(hash) ? Syslog.getInstance(hash) : Syslog.createInstance(hash, config);
148166

@@ -320,6 +338,8 @@ public ConfigurationRequest getRequestedConfiguration() {
320338

321339
configurationRequest.addField(new TextField("maxlen", "Maximum message length", "", "Maximum message (body) length. Longer messages will be truncated. If not specified defaults to 16384 bytes.", ConfigurationField.Optional.OPTIONAL));
322340

341+
configurationRequest.addField(new TextField("maxQueueSize", "Maximum queue size", "500", "Maximum number of messages to queue internally before blocking (prevents OOM). Set to -1 for unlimited (not recommended). Default is 500.", ConfigurationField.Optional.OPTIONAL));
342+
323343
configurationRequest.addField(new TextField("keystore", "Key store", "", "Path to Java keystore (required for SSL over TCP). Must contain private key and cert for this client.", ConfigurationField.Optional.OPTIONAL));
324344
configurationRequest.addField(new TextField("keystorePassword", "Key store password", "", "", ConfigurationField.Optional.OPTIONAL));
325345

0 commit comments

Comments
 (0)