系统内存不足异常

发布于 2024-09-13 03:11:43 字数 1355 浏览 1 评论 0原文

我有一个用 lucene.net 用 asp.net 编写的程序。首先,我根据 28000 个文档创建一个索引。 其次,我正在执行搜索,但有时会出现错误。 (我认为当有很多结果时会抛出此错误)

代码的重要部分:

Dim hits As Hits = searcher.Search(query)
Dim results As Integer = hits.Length()  'ergebnisse (größe der hits)

'#####################
'####### RESULTS #####
'#####################

trefferanzahl = results

If (results > 0) Then  
    Dim i As Integer
    Dim h As Integer = results - 1
    ReDim array_results(h, 6) 'array zum speichern von den "feldern"
    Dim cellX As New TableCell()

    For i = 0 To results - 1 Step 1 

        Dim tmpdoc As Document = hits.Doc(i)   ' HERE THE ERROR!
        Dim score As Double = hits.Score(i)     

        MsgBox("2. Docname: " & hits.Doc(i).Get("title"))


        array_results(i, 0) = tmpdoc.Get("title")
        array_results(i, 0) += tmpdoc.Get("doc_typ") 
        array_results(i, 1) = tmpdoc.Get("pfad")
        array_results(i, 2) = tmpdoc.Get("date_of_create")
        array_results(i, 3) = tmpdoc.Get("last_change")
        array_results(i, 4) = tmpdoc.Get("id")
        array_results(i, 5) = tmpdoc.Get("doc_typ")
        array_results(i, 6) = CStr(score)
    Next

    ' Load this data only once.

    ItemsGrid.DataSource = CreateDataSource()   
    ItemsGrid.DataBind()
Else
    bool_Suchergebnis = False
End If

searcher.Close()

提前致谢

I have a program written in asp.net with lucene.net. At first I create an index from 28000 documents.
Secondly I'm executing a search, but sometimes there is an error. (I think this error is thrown when there are many results)

The important part of code:

Dim hits As Hits = searcher.Search(query)
Dim results As Integer = hits.Length()  'ergebnisse (größe der hits)

'#####################
'####### RESULTS #####
'#####################

trefferanzahl = results

If (results > 0) Then  
    Dim i As Integer
    Dim h As Integer = results - 1
    ReDim array_results(h, 6) 'array zum speichern von den "feldern"
    Dim cellX As New TableCell()

    For i = 0 To results - 1 Step 1 

        Dim tmpdoc As Document = hits.Doc(i)   ' HERE THE ERROR!
        Dim score As Double = hits.Score(i)     

        MsgBox("2. Docname: " & hits.Doc(i).Get("title"))


        array_results(i, 0) = tmpdoc.Get("title")
        array_results(i, 0) += tmpdoc.Get("doc_typ") 
        array_results(i, 1) = tmpdoc.Get("pfad")
        array_results(i, 2) = tmpdoc.Get("date_of_create")
        array_results(i, 3) = tmpdoc.Get("last_change")
        array_results(i, 4) = tmpdoc.Get("id")
        array_results(i, 5) = tmpdoc.Get("doc_typ")
        array_results(i, 6) = CStr(score)
    Next

    ' Load this data only once.

    ItemsGrid.DataSource = CreateDataSource()   
    ItemsGrid.DataBind()
Else
    bool_Suchergebnis = False
End If

searcher.Close()

Thanks in advance

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

窗影残 2024-09-20 03:11:43

在非常大的集合中执行搜索时,一个好的原则是尽快限制正在处理的结果。我假设您正在网格中实现分页。假设 PageSize 是 20。

您需要做的是确保您可以在此方法中访问 PageSize 和当前的 PageNo。然后使用 Linq 跨结果集获取 Take(PageSize) 和 Skip (PageNo * PageSize)。那么您只需处理 20 条记录。

然后,你有两个选择。如果您直接绑定到数组,您可能能够摆脱空项目,但我不确定,因此您可能必须将虚拟项目放入数据源数组中不会显示的所有位置。并不理想,但肯定比处理数千个点击要快。

第二个选项是仅将 20 个项目绑定到网格,这会很快,关闭网格上的分页,因为它只会显示一页,然后根据您知道的 PageSize 和当前 PageNo 来实现您自己的分页行为。这将需要更多的工作,但它的执行速度比开箱即用的 gridview 绑定到大型数据源要快得多。

它将帮助您解决记忆问题。

A good principle when performing searches accross very large collections is to limit the results that you are processing as soon as possible. I will assume that you are implementing paging in your grid. And lets assume that PageSize is 20.

What you need to do is make sure that you have access to the PageSize and the current PageNo within this method. Then use Linq accross the result set to Take(PageSize) and Skip (PageNo * PageSize). Then you will only have to process 20 records.

Then, you have two options. If you are binding directly to the array, you might be able to get away with empty items, but I am not sure, so you might have to place dummy items into the datasource array in all positions that won't be displayed. Not ideal, but certainly quicker than processing 1000s of Hits.

The second option is to bind only the 20 items to the grid, which will be quick, switch off paging on the grid as it will only show one page and then implement your own paging behaviour as you know the PageSize, and the current PageNo. This will take more work but it will perform a lot faster than the out-of-the-box gridview binding to a large datasource.

And it will help you solve your memory problem.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文