簡體   English   中英

如何在沒有OutOfMemoryError的情況下迭代大型Mongo集合

[英]How do i iterate over large Mongo collections without OutOfMemoryError

在我的Spring應用程序中,我想迭代一個Mongo集合來執行每個條目的一些工作。 集合可能非常大,因此不能簡單地獲得所有條目的列表,因為這將導致OutOfMemoryErrors。

我最近的嘗試是這樣的:

void m(MongoOperations ops, Set<String> ids) {
   Query query = new Query().addCriteria(Criteria.where("id").in(ids));
   CloseableIterator<Foo> it = ops.stream(query, Foo.class);
   it.forEachRemaining(foo -> {
       System.out.println(foo.getName());
   }
}

看到我在這里得到OutOfMemoryErrors讓我感到很驚訝。 看起來匹配查詢的所有Foo條目it.forEachRemaining在調用it.forEachRemaining加載到內存中。

Heapdump顯示CloseableIteratorAdaptor包含一個DBCursor ,它包含一個QueryResultIterator ,而QueryResultIterator又包含一個包含所有條目的ArrayList

難道我做錯了什么? stream()總是將所有條目加載到內存中嗎? 我必須實現分頁嗎?

這是我的堆轉儲的支配樹的相關部分。

Class Name                                                                                                           | Shallow Heap | Retained Heap | Percentage
-----------------------------------------------------------------------------------------------------------------------------------------------------------------
org.example.server.OrganizationScopedThreadFactory$OrganizationScopedThread @ 0x81f71718  pool-1-thread-1 Thread|          128 | 1,453,308,456 |     87,50%
|- org.springframework.data.mongodb.core.MongoTemplate$CloseableIterableCursorAdapter @ 0x8b2df1c0                   |           24 | 1,432,708,656 |     86,26%
|  |- com.mongodb.DBCursor @ 0x8b3bb0f8                                                                              |           96 | 1,432,708,600 |     86,26%
|  |  |- com.mongodb.QueryResultIterator @ 0x8b5e7c70                                                                |           72 | 1,431,064,320 |     86,16%
|  |  |  |- java.util.ArrayList$Itr @ 0x8b5e7cb8                                                                     |           32 | 1,431,064,152 |     86,16%
|  |  |  |  '- java.util.ArrayList @ 0x8b5e7cd8                                                                      |           24 | 1,431,064,120 |     86,16%
|  |  |  |     '- java.lang.Object[30391] @ 0x8b5e8ed8                                                               |      121,584 | 1,431,064,096 |     86,16%
|  |  |  |        |- com.mongodb.BasicDBObject @ 0x8c2eed70                                                          |           64 |       123,528 |      0,01%
|  |  |  |        |  |- java.util.LinkedHashMap$Entry @ 0x8c2eef18                                                   |           40 |       122,240 |      0,01%
|  |  |  |        |  |  |- com.mongodb.BasicDBList @ 0x8c2eef78                                                      |           32 |       122,144 |      0,01%
|  |  |  |        |  |  |  '- java.lang.Object[10] @ 0x8c2eef98                                                      |           56 |       122,112 |      0,01%
|  |  |  |        |  |  |     '- com.mongodb.DBRef @ 0x8c2eefd0                                                      |           32 |       122,056 |      0,01%
|  |  |  |        |  |  |        |- com.mongodb.BasicDBObject @ 0xc5dbc778                                           |           64 |       121,992 |      0,01%
|  |  |  |        |  |  |        |- org.bson.types.ObjectId @ 0x8c2eeff0                                             |           32 |            32 |      0,00%
|  |  |  |        |  |  |        '- Total: 2 entries                                                                 |              |               |           
|  |  |  |        |  |  |- java.lang.String @ 0x8c2eef40  projects                                                   |           24 |            56 |      0,00%
|  |  |  |        |  |  '- Total: 2 entries                                                                          |              |               |           
|  |  |  |        |  |- java.util.LinkedHashMap$Entry @ 0x8c2eee00                                                   |           40 |         1,024 |      0,00%
|  |  |  |        |  |- java.util.LinkedHashMap$Entry @ 0x8c2eeea0                                                   |           40 |           120 |      0,00%
|  |  |  |        |  |- java.util.HashMap$Node[16] @ 0x8c2eedb0                                                      |           80 |            80 |      0,00%
|  |  |  |        |  '- Total: 4 entries                                                                             |              |               |           
|  |  |  |        |- com.mongodb.BasicDBObject @ 0x8bcfd4c0                                                          |           64 |       123,480 |      0,01%
|  |  |  |        |- com.mongodb.BasicDBObject @ 0x8c48e2c8                                                          |           64 |       113,520 |      0,01%
|  |  |  |        |- com.mongodb.BasicDBObject @ 0x8c66f668                                                          |           64 |       112,296 |      0,01%
|  |  |  |        |- com.mongodb.BasicDBObject @ 0x8c87afe0                                                          |           64 |       112,120 |      0,01%
|  |  |  |        |- com.mongodb.BasicDBObject @ 0x8c521008                                                          |           64 |       106,096 |      0,01%
|  |  |  |        |- com.mongodb.BasicDBObject @ 0x8cdc68d0                                                          |           64 |        99,576 |      0,01%
|  |  |  |        |- com.mongodb.BasicDBObject @ 0x8c8efa40                                                          |           64 |        90,456 |      0,01%
|  |  |  |        |- com.mongodb.BasicDBObject @ 0x8b8e0d18                                                          |           64 |        77,600 |      0,00%
|  |  |  |        |- com.mongodb.BasicDBObject @ 0x8bbc5a30                                                          |           64 |        77,600 |      0,00%
|  |  |  |        |- com.mongodb.BasicDBObject @ 0x8bc446e8                                                          |           64 |        77,600 |      0,00%
|  |  |  |        |- com.mongodb.BasicDBObject @ 0x8bcc0ca0                                                          |           64 |        77,600 |      0,00%
|  |  |  |        |- com.mongodb.BasicDBObject @ 0x8bda1d30                                                          |           64 |        77,600 |      0,00%
|  |  |  |        |- com.mongodb.BasicDBObject @ 0x8be46048                                                          |           64 |        77,600 |      0,00%
|  |  |  |        |- com.mongodb.BasicDBObject @ 0x8be462e8                                                          |           64 |        77,600 |      0,00%
|  |  |  |        |- com.mongodb.BasicDBObject @ 0x8beb24f8                                                          |           64 |        77,600 |      0,00%
|  |  |  |        |- com.mongodb.BasicDBObject @ 0x8beb2798                                                          |           64 |        77,600 |      0,00%
|  |  |  |        |- com.mongodb.BasicDBObject @ 0x8bee79d0                                                          |           64 |        77,600 |      0,00%
|  |  |  |        |- com.mongodb.BasicDBObject @ 0x8bf04f38                                                          |           64 |        77,600 |      0,00%
|  |  |  |        |- com.mongodb.BasicDBObject @ 0x8bf0eae8                                                          |           64 |        77,600 |      0,00%
|  |  |  |        |- com.mongodb.BasicDBObject @ 0x8bf0ed88                                                          |           64 |        77,600 |      0,00%
|  |  |  |        |- com.mongodb.BasicDBObject @ 0x8bf14220                                                          |           64 |        77,600 |      0,00%
|  |  |  |        |- com.mongodb.BasicDBObject @ 0x8bf3edf0                                                          |           64 |        77,600 |      0,00%
|  |  |  |        |- com.mongodb.BasicDBObject @ 0x8bf78640                                                          |           64 |        77,600 |      0,00%
|  |  |  |        |- com.mongodb.BasicDBObject @ 0x8bf7dd30                                                          |           64 |        77,600 |      0,00%
|  |  |  |        '- Total: 25 of 30.276 entries; 30.251 more                                                        |              |               |           
-----------------------------------------------------------------------------------------------------------------------------------------------------------------

更新:我已經嘗試通過設置查詢的限制並對其進行排序來實現分頁,然后重復跳過條目,直到我迭代了所有內容。 雖然這確實解決了我的內存問題,但它顯着降低了性能(大約慢了100倍),可能是由於需要排序。

我使用的是Spring Boot 1.3.7

對於游標應用了16 MB的內存限制,您可能需要編寫應用程序級別循環以通過使用跳過和限制以及排序來選擇1000的批次。

首先選擇1000,然后是1000到2000,依此類推,直到文檔結束,確保在查詢中使用排序,以便數據一致。

此外,如果要修改要查詢和排序的字段,則必須注意,以便相同的數據不會一次又一次地彈出。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM