Skip to content

Commit 354bbec

Browse files
author
Ben Osheroff
committed
take another try at preventing memory issues.
ObjectInputStream.readObject and ObjectOutputStream.writeObject do a semi-pathological thing where they retain references to every object that you've written into them (for efficiency purposes). This means that my whole premise about using them to flush ram out to disk was flawed.
1 parent f72fbac commit 354bbec

File tree

2 files changed

+12
-7
lines changed

2 files changed

+12
-7
lines changed

src/main/java/com/zendesk/maxwell/MaxwellReplicator.java

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -155,11 +155,11 @@ private RowMapBuffer getTransactionRows() throws Exception {
155155

156156
RowMapBuffer buffer = new RowMapBuffer(MAX_TX_ELEMENTS);
157157

158-
// currently to satisfy the test interface, the contract is to return null
159-
// if the queue is empty. should probably just replace this with an optional timeout...
160-
161158
while ( true ) {
162159
v4Event = pollV4EventFromQueue();
160+
161+
// currently to satisfy the test interface, the contract is to return null
162+
// if the queue is empty. should probably just replace this with an optional timeout...
163163
if (v4Event == null) {
164164
ensureReplicatorThread();
165165
continue;

src/main/java/com/zendesk/maxwell/util/ListWithDiskBuffer.java

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -31,14 +31,19 @@ public void add(T element) throws IOException {
3131
if ( file == null ) {
3232
file = File.createTempFile("maxwell", "events");
3333
file.deleteOnExit();
34-
os = new ObjectOutputStream(new FileOutputStream(file));
35-
is = new ObjectInputStream(new FileInputStream(file));
34+
os = new ObjectOutputStream(new BufferedOutputStream(new FileOutputStream(file)));
3635
}
3736

3837
if ( elementsInFile == 0 )
3938
LOGGER.debug("Overflowed in-memory buffer, spilling over into " + file);
4039

41-
os.writeObject(this.list.removeFirst());
40+
os.writeUnshared(this.list.removeFirst());
41+
42+
if ( is == null && os != null ) {
43+
os.flush();
44+
is = new ObjectInputStream(new BufferedInputStream(new FileInputStream(file)));
45+
}
46+
4247
elementsInFile++;
4348
}
4449
}
@@ -53,7 +58,7 @@ public T getLast() {
5358

5459
public T removeFirst() throws IOException, ClassNotFoundException {
5560
if ( elementsInFile > 0 && is != null ) {
56-
T element = (T) is.readObject();
61+
T element = (T) is.readUnshared();
5762
elementsInFile--;
5863
return element;
5964
} else {

0 commit comments

Comments
 (0)