mirror of
https://github.com/ebean-orm/ebean.git
synced 2026-11-12 22:41:35 +00:00
#2406 - Memory leak in Query.findEach
This commit is contained in:
1 parent
0a0d67585c
commit
2549eace09
3 files changed
+36
-9
No files matched your search
@@ -2,11 +2,15 @@ package org.tests.basic;
|
||||
|
||||
import io.ebean.BaseTestCase;
|
||||
import io.ebean.DB;
|
||||
import org.junit.jupiter.api.Disabled;
|
||||
import org.junit.jupiter.api.Test;
|
||||
import org.tests.model.basic.Customer;
|
||||
import org.tests.model.basic.Order;
|
||||
import org.tests.model.basic.ResetBasicData;
|
||||
|
||||
import java.util.WeakHashMap;
|
||||
import java.util.concurrent.atomic.AtomicInteger;
|
||||
|
||||
import static org.junit.jupiter.api.Assertions.assertEquals;
|
||||
import static org.junit.jupiter.api.Assertions.assertTrue;
|
||||
|
||||
@@ -78,4 +82,36 @@ public class TestPersistenceContext extends BaseTestCase {
|
||||
}
|
||||
}
|
||||
|
||||
@Disabled
|
||||
@Test
|
||||
void findWithGcTest() {
|
||||
for (int j = 0; j < 20; j++) {
|
||||
for (int i = 0; i < 500; i++) {
|
||||
Customer c = new Customer();
|
||||
c.setName("Customer #" + i);
|
||||
DB.save(c);
|
||||
}
|
||||
int customerCount = DB.find(Customer.class).findCount();
|
||||
AtomicInteger count = new AtomicInteger(customerCount);
|
||||
|
||||
WeakHashMap<Customer, Integer> customers = new WeakHashMap<>();
|
||||
|
||||
DB.find(Customer.class).fetch("orders").findEach(customer -> {
|
||||
customers.put(customer, customer.getId());
|
||||
if (count.decrementAndGet() == 0) {
|
||||
// Trigger garbage collection on last iteration and check if beans disappear from memory
|
||||
System.gc();
|
||||
try {
|
||||
Thread.sleep(100);
|
||||
} catch (InterruptedException e) {
|
||||
Thread.currentThread().interrupt();
|
||||
e.printStackTrace();
|
||||
}
|
||||
customers.size(); // expunge stale entries
|
||||
System.out.println("Total instances: " + customerCount + ", instances left in memory: " + customers.size());
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user