Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Create MessagePack singleton provider to avoid PermGen leak.
Prepare the MessagePack instance with a RadioMessage because generating code for the MessagePack template registry is not thread-safe. Before this, a new MessagePack instance got created for each processed message which eventually lead to PermGen exhaustion. Not sure why the generated classes haven't been garbage collected. (bug in msgpack?) Fixes #907.
- Loading branch information
Showing
2 changed files
with
57 additions
and
3 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
50 changes: 50 additions & 0 deletions
50
...log2-shared/src/main/java/org/graylog2/shared/bindings/providers/MessagePackProvider.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,50 @@ | ||
/** | ||
* This file is part of Graylog2. | ||
* | ||
* Graylog2 is free software: you can redistribute it and/or modify | ||
* it under the terms of the GNU General Public License as published by | ||
* the Free Software Foundation, either version 3 of the License, or | ||
* (at your option) any later version. | ||
* | ||
* Graylog2 is distributed in the hope that it will be useful, | ||
* but WITHOUT ANY WARRANTY; without even the implied warranty of | ||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | ||
* GNU General Public License for more details. | ||
* | ||
* You should have received a copy of the GNU General Public License | ||
* along with Graylog2. If not, see <http://www.gnu.org/licenses/>. | ||
*/ | ||
|
||
package org.graylog2.shared.bindings.providers; | ||
|
||
import org.graylog2.plugin.RadioMessage; | ||
import org.msgpack.MessagePack; | ||
|
||
import javax.inject.Provider; | ||
import java.io.ByteArrayOutputStream; | ||
import java.io.IOException; | ||
|
||
/** | ||
* MessagePack generates classes for each new instance and message type which can leak into PermGen. | ||
* | ||
* Must be a singleton provider! | ||
*/ | ||
public class MessagePackProvider implements Provider<MessagePack> { | ||
@Override | ||
public MessagePack get() { | ||
final MessagePack messagePack = new MessagePack(); | ||
|
||
try { | ||
// Eagerly generate RadioMessage classes in the MessagePack object to avoid doing it on runtime. | ||
// The generated code is thread-safe, but generating it is not. | ||
final RadioMessage radioMessage = new RadioMessage(); | ||
final ByteArrayOutputStream stream = new ByteArrayOutputStream(); | ||
messagePack.write(stream, radioMessage); | ||
final byte[] bytes = stream.toByteArray(); | ||
messagePack.read(bytes, RadioMessage.class); | ||
} catch (IOException ignore) { | ||
} | ||
|
||
return messagePack; | ||
} | ||
} |