none
Generic List(Of T) Maximum Items? RRS feed

  • Question

  • I'm using a generic List(Of String) and filling it with commands I pass to a PrintDocument to do the actual drawing. There are roughly 49,000 items in my list and the list is created just fine.

    I then try to open a Windows form with a PrintPreviewControl in it and pass it the PrintDocument. It bombs out before I even pass the PrintDocument.

    It fails with "Exception has been thrown by the target of an invocation."

    The InnerException is: "The current process has used all of its system allowance of handles for Window Manager objects"

    After filling the List, execution goes to this sub...

        Public Sub PreviewReport()
            Try
                Dim PV1 As New PreviewForm
                PV1.PrintPreviewControl1.Document = PD1
                PV1.PrintPreviewControl1.Zoom = mvarZoomPercent / 100
                PV1.lblZoom.Text = mvarZoomPercent & "%"
                PV1.Text = mvarTitle
                If mvarPreviewSizable = True Then
                    PV1.Height = mvarPreviewWindowHeight
                    PV1.Width = mvarPreviewWindowWidth
                Else
                    PV1.FormBorderStyle = Windows.Forms.FormBorderStyle.FixedToolWindow
                    PV1.WindowState = Windows.Forms.FormWindowState.Maximized
                End If
                PV1.ShowDialog()
            Catch ex As Exception
                Debug.Print(ex.Message)
                Debug.Print(ex.InnerException.Message)
            End Try
        End Sub

    It fails at the "Dim PV1..." line.

    Tuesday, July 24, 2012 2:13 AM

Answers

  • As far as I know the list can handle a rather large amount of items and it really depends on your memory on the PC your running the app on. It also depends on what data type your storing in the list because each data type differs in size (bytes). If your storing a custom class in your generic.List it uses allot more memory than a integer or short data type. So if you don't notice any memory leaks or performance issues you should be ok. Thanks Charlie
    Tuesday, July 24, 2012 5:22 AM
  • In other words, your problem doesn't lie with the code you have posted, or with the List() object, but elsewhere (specifically, in the PreviewForm object).

    Stephen J Whiteley

    • Proposed as answer by JohnWein Tuesday, July 24, 2012 1:40 PM
    • Marked as answer by Youen ZenModerator Friday, August 3, 2012 9:03 AM
    Tuesday, July 24, 2012 1:18 PM
    Moderator

All replies

  • As far as I know the list can handle a rather large amount of items and it really depends on your memory on the PC your running the app on. It also depends on what data type your storing in the list because each data type differs in size (bytes). If your storing a custom class in your generic.List it uses allot more memory than a integer or short data type. So if you don't notice any memory leaks or performance issues you should be ok. Thanks Charlie
    Tuesday, July 24, 2012 5:22 AM
  • To prove that the list is not the problem, run your application with a much smaller list.

    The error you are getting is generally the result of a routine calling itself over and over again, or some other process calling that routine repeatedly.

    Remove your Try/Catch as it is masking your error and making your debugging very difficult.  You want to see the errors that occur as they occur.  Then run it again to see if a different error appears. 

    Insert a breakpoint at the start of that routine.  When it is hit, trace your code through line by line, paying particular attention to any code that is called that is not part of that routine, that might cause that routine to be re-started.

    Tuesday, July 24, 2012 5:51 AM
  • The report I'm sending to the PrintDocument is around 22 pages worth. If I send a smaller one, 2 - 5 pages, it works fine. In the original code, the string data was originally written to a file and then I opened a streamreader in the PrintDocument1_PrintPage event. It didn't matter how much I put in the file, it worked fine. It was just incredibly slow.

    My computer by the way, has 8GB RAM and running Win7-64 Home Premium. Wouldn't think it's a memory issue. All of the items in the List are strings and most are less than 100 characters.

    Tuesday, July 24, 2012 11:11 AM
  • This thread may be of interest to you http://social.msdn.microsoft.com/Forums/en-US/winforms/thread/73aaa1f3-30a7-4593-b299-7ec1fd582b27


    You've taught me everything I know but not everything you know.

    Tuesday, July 24, 2012 11:25 AM
  • In Win 7 64Bit if you open task manager go to the processes tab then View, select columns and check Handles.


    You've taught me everything I know but not everything you know.

    Tuesday, July 24, 2012 11:43 AM
  • I opened up task manager and had it show the USER Objects. For vshost32-clr2.exe it climbs to 9,999 when it bombs out. Obviously the problem since there is a 10,000 limit. But I'm not sure what to do about it. This goes back to my first question--is there a limit to the number of items I can put in a generic list? I guess I might have to go back to my slow way of writing to a file.
    Tuesday, July 24, 2012 12:13 PM
  • Yes, max. 10,000 GDI objects. You (or the code called) obviously do no dispose them after usage.

    http://msdn.microsoft.com/en-us/library/windows/desktop/ms724291(v=vs.85).aspx

    This is not a limit of the generic list.


    Armin


    Tuesday, July 24, 2012 12:26 PM
  • In other words, your problem doesn't lie with the code you have posted, or with the List() object, but elsewhere (specifically, in the PreviewForm object).

    Stephen J Whiteley

    • Proposed as answer by JohnWein Tuesday, July 24, 2012 1:40 PM
    • Marked as answer by Youen ZenModerator Friday, August 3, 2012 9:03 AM
    Tuesday, July 24, 2012 1:18 PM
    Moderator
  • I opened up task manager and had it show the USER Objects. For vshost32-clr2.exe it climbs to 9,999 when it bombs out. Obviously the problem since there is a 10,000 limit. But I'm not sure what to do about it. This goes back to my first question--is there a limit to the number of items I can put in a generic list? I guess I might have to go back to my slow way of writing to a file.

    When did you see these 9999 handles/User Objects ? Place a breakpoint at the offending line, and then use the taskmanager. How many handles/User Objects do you count ?

    Hannes

    If you have got questions about this, just ask.

    In a perfect world,
    users would never enter data in the wrong form,
    files they choose to open would always exist
    and code would never have bugs.

    C# to VB.NET: http://www.developerfusion.com/tools/convert/csharp-to-vb/

    Tuesday, July 24, 2012 1:35 PM
  • Alright, I figured it out. It was just plain bad coding.

    I kept a font to measure strings and line height, etc. Whenever I "changed" the font, I was just creating a new font and not disposing the old one. Added the font.Dispose line and now all is working.

    Thanks everyone for your help.

    Tuesday, July 24, 2012 1:45 PM
  • Cool, life is good!

    You've taught me everything I know but not everything you know.

    Tuesday, July 24, 2012 1:47 PM