Skip to content

Fix a memory leak in test. #588

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Aug 19, 2024
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -77,15 +77,17 @@ public void checkBadSerVer() {
@Test(expectedExceptions = SketchesArgumentException.class)
public void checkConstructorKtooSmall() {
int k = 8;
WritableMemory mem = makeNativeMemory(k);
UpdateSketch.builder().setNominalEntries(k).build(mem);
try (WritableMemory mem = makeNativeMemory(k)) {
UpdateSketch.builder().setNominalEntries(k).build(mem);
}
}

@Test(expectedExceptions = SketchesArgumentException.class)
public void checkConstructorMemTooSmall() {
int k = 16;
WritableMemory mem = makeNativeMemory(k/2);
UpdateSketch.builder().setNominalEntries(k).build(mem);
try (WritableMemory mem = makeNativeMemory(k/2)) {
UpdateSketch.builder().setNominalEntries(k).build(mem);
}
}

@Test(expectedExceptions = SketchesArgumentException.class)
Expand Down Expand Up @@ -589,7 +591,7 @@ public void checkEstModeNativeMemory() {
int u = 2*k;
int memCapacity = (k << 4) + (Family.QUICKSELECT.getMinPreLongs() << 3);

try(WritableMemory mem = WritableMemory.allocateDirect(memCapacity)) {
try(WritableMemory mem = WritableMemory.allocateDirect(memCapacity)) { //will not request more memory
UpdateSketch usk = UpdateSketch.builder().setNominalEntries(k).build(mem);
DirectQuickSelectSketch sk1 = (DirectQuickSelectSketch)usk; //for internal checks
assertTrue(usk.isEmpty());
Expand All @@ -599,6 +601,10 @@ public void checkEstModeNativeMemory() {
println(""+est);
assertEquals(usk.getEstimate(), u, u*.05);
assertTrue(sk1.getRetainedEntries(false) > k);
Memory mem2 = usk.getMemory();
assertTrue(mem2.isAlive());
assertTrue(mem2.isDirect()); //still off heap.
assertTrue(mem2.isSameResource(mem));
}
}

Expand Down Expand Up @@ -774,20 +780,22 @@ public void checkMoveAndResize() {
int k = 1 << 12;
int u = 2 * k;
int bytes = Sketches.getMaxUpdateSketchBytes(k);
WritableMemory wmem = WritableMemory.allocateDirect(bytes/2); //will request
WritableMemory wmem = WritableMemory.allocateDirect(bytes/2); //will request more memory
UpdateSketch sketch = Sketches.updateSketchBuilder().setNominalEntries(k).build(wmem);
assertTrue(sketch.isSameResource(wmem));
for (int i = 0; i < u; i++) { sketch.update(i); }
assertTrue(sketch.getMemory().isAlive());
assertFalse(wmem.isAlive());
Memory mem = sketch.getMemory();
assertTrue(mem.isAlive());
assertFalse(mem.isDirect()); //now on heap.
assertFalse(wmem.isAlive()); //wmem closed by MemoryRequestServer
}

@Test
public void checkReadOnlyRebuildResize() {
int k = 1 << 12;
int u = 2 * k;
int bytes = Sketches.getMaxUpdateSketchBytes(k);
WritableMemory wmem = WritableMemory.allocateDirect(bytes/2); //will request
WritableMemory wmem = WritableMemory.allocateDirect(bytes/2); //will request more memory
UpdateSketch sketch = Sketches.updateSketchBuilder().setNominalEntries(k).build(wmem);
for (int i = 0; i < u; i++) { sketch.update(i); }
double est1 = sketch.getEstimate();
Expand All @@ -796,6 +804,10 @@ public void checkReadOnlyRebuildResize() {
UpdateSketch roSketch = (UpdateSketch) Sketches.wrapSketch(mem);
double est2 = roSketch.getEstimate();
assertEquals(est2, est1);
Memory mem2 = sketch.getMemory();
assertTrue(mem2.isAlive());
assertFalse(mem2.isDirect()); //now on heap
assertFalse(wmem.isAlive()); //wmem closed by MemoryRequestServer
try {
roSketch.rebuild();
fail();
Expand Down
14 changes: 7 additions & 7 deletions src/test/java/org/apache/datasketches/theta/UnionImplTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -191,21 +191,21 @@ public void checkMoveAndResizeOffHeap() {
final int k = 1 << 12;
final int u = 2 * k;
final int bytes = Sketches.getMaxUpdateSketchBytes(k);
WritableMemory wmem = WritableMemory.allocateDirect(bytes / 2); //too small, forces new allocation on heap
WritableMemory wmem2 = WritableMemory.allocateDirect(bytes / 2);
WritableMemory wmem = WritableMemory.allocateDirect(bytes / 2); //not really used, except as a reference.
WritableMemory wmem2 = WritableMemory.allocateDirect(bytes / 2); //too small, forces new allocation on heap
final UpdateSketch sketch = Sketches.updateSketchBuilder().setNominalEntries(k).build(wmem);
assertTrue(sketch.isSameResource(wmem)); //also testing the isSameResource function

final Union union = SetOperation.builder().buildUnion(wmem2);
assertTrue(union.isSameResource(wmem2));

for (int i = 0; i < u; i++) { union.update(i); }
assertFalse(union.isSameResource(wmem));
assertFalse(union.isSameResource(wmem)); //different Memories altogether

final Union union2 = SetOperation.builder().buildUnion(); //on-heap union
assertFalse(union2.isSameResource(wmem2)); //obviously not
wmem.close();
//note wmem2 has already been closed by the DefaultMemoryRequestServer
wmem.close(); //empty, but we must close it anyway.
//note wmem2 has already been closed by the DefaultMemoryRequestServer.
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

could do assertFalse(wmem2.isAlive()) here rather than just a comment?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Fixed.

}

@Test
Expand All @@ -226,13 +226,13 @@ public void checkUnionCompactOrderedSource() {
final double est1 = sk.getEstimate();

final int bytes = Sketches.getMaxCompactSketchBytes(sk.getRetainedEntries(true));
try (WritableMemory wmem = WritableMemory.allocateDirect(bytes)) {
try (WritableMemory wmem = WritableMemory.allocateDirect(bytes)) { //sufficient memory
final CompactSketch csk = sk.compact(true, wmem); //ordered, direct
final Union union = Sketches.setOperationBuilder().buildUnion();
union.union(csk);
final double est2 = union.getResult().getEstimate();
assertEquals(est2, est1);
}
} //wmem is closed here
}

@Test(expectedExceptions = SketchesArgumentException.class)
Expand Down
Loading