gpt4 book ai didi

c# - LINQ Join 的内存使用情况是多少?

转载 作者:行者123 更新时间:2023-11-30 14:34:47 34 4
gpt4 key购买 nike

  1. 如果两个 IEnumerable(s) 在内存中,连接它们会占用多少内存?假设选择所有列。是左表大小+右表大小+联表行数吗?

  2. 如果两个 IEnumerable(s) 是由文件流定义的,如果它们太大而无法放入内存,Join 是否会抛出内存不足异常?或者它会一直加载直到接近内存不足并多次运行扫描(类似于数据库连接)吗?

最佳答案

在 Reflector 中查看 Enumerable.JoinIterator,似乎整个 inner(第二个)序列被加载到内存中(到 Lookup ),但外部(第一个)序列随后被流式传输(即不是全部加载到内存中)。

如果 inner 序列太大而无法放入内存,似乎没有什么聪明的事情发生。

Jon Skeets seems to agree on the first point :

The real Join operator uses the same behaviour as Except and Intersect when it comes to how the input sequences are consumed:

...

  • When MoveNext is called on the result sequence for the first time, it immediately consumes the whole of the inner sequence, buffering it.

  • The outer sequence is streamed - it's only read one element at a time. By the time the result sequence has started yielding results from the second element of outer, it's forgotten about the first element.

关于c# - LINQ Join 的内存使用情况是多少?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13372687/

34 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com