locked
Save many CodeElements can cause VS crash? RRS feed

  • Question

  •  private List<CodeElement2> allCodeElementList = new List<CodeElement2>(1000);
    
          
    
            private void SaveItems()
    
            {
    
                Solution2 sln = this._applicationObject.Solution as Solution2;
    
                System.Diagnostics.Stopwatch watch = System.Diagnostics.Stopwatch.StartNew();
    
                this.allCodeElementList.Clear();
    
                Stack<ProjectItem> stack = new Stack<ProjectItem>(400);
    
    
    
                foreach (Project project in sln.Projects)
    
                {
    
                    CollectionHelper<ProjectItem>.PushToStack(stack, project.ProjectItems as IEnumerable);
    
                }
    
    
    
                while (stack.Count > 0)
    
                {
    
                    ProjectItem pi = stack.Pop();
    
                    FileCodeModel2 codeMode = SafeGetItemFileCodeMode(pi);
    
    
    
                    if (codeMode != null)
    
                    {
    
                        AddElements(codeMode.CodeElements, allCodeElementList);
    
                    }
    
    
    
                    CollectionHelper<ProjectItem>.PushToStack(stack, pi.ProjectItems as IEnumerable);
    
                }
    
    
    
                watch.Stop();
    
                MessageBox.Show("CreateTypeIndex spend time is " + watch.Elapsed.TotalSeconds + ". Total index file count is " + allCodeElementList.Count);
    
            }
    
    
    
          
    
            private void AddElements(CodeElements codes, List<CodeElement2> types)
    
            {
    
                if (codes == null)
    
                {
    
                    return;
    
                }
    
    
    
                Stack<CodeElement2> stack = new Stack<CodeElement2>(10);
    
                CollectionHelper<CodeElement2>.PushToStack(stack, codes as IEnumerable);
    
    
    
                while (stack.Count > 0)
    
                {
    
                    CodeElement2 codeElement = stack.Pop();
    
                    types.Add(codeElement);
    
    
    
                    try
    
                    {
    
                        vsCMElement kind = codeElement.Kind;
    
    
    
                        switch (kind)
    
                        {
    
                            case vsCMElement.vsCMElementNamespace:
    
                            case vsCMElement.vsCMElementStruct:
    
                            case vsCMElement.vsCMElementClass:
    
                                CollectionHelper<CodeElement2>.PushToStack(stack, codeElement.Children as IEnumerable);
    
                                break;
    
                        }
    
                    }
    
                    catch (Exception e)
    
                    {
    
                        global::System.Windows.Forms.MessageBox.Show(e.ToString());
    
                    }
    
                }
    
            }
    
    
    
            private FileCodeModel2 SafeGetItemFileCodeMode(ProjectItem pi)
    
            {
    
                FileCodeModel2 result = null;
    
    
    
                try
    
                {
    
                    result = pi.FileCodeModel as FileCodeModel2;
    
                }
    
    
    
                catch (Exception error)
    
                {
    
                    MessageBox.Show(error.ToString());
    
                }
    
    
    
                return result;
    
            }
    
    
    
    static class CollectionHelper<T>
    
        {
    
            public static void PushToStack(Stack<T> stack, IEnumerable enumerable)
    
            {
    
                if (enumerable == null)
    
                {
    
                    return;
    
                }
    
    
    
                foreach (T obj in enumerable)
    
                {
    
                    stack.Push(obj);
    
                }
    
            }
    
        }
    
    
    I open a solution which may be has 1000 classes (may be more) to execute the "SaveItems" method, then the VS broken down every time.
    The memory of VS is not high, and the system still has many free memory.
    Who can tell the reason? How can I solve this? Thanks!
    I am using the VS 08.
    Monday, January 25, 2010 3:33 AM

Answers

All replies

  • Hello,

    See:

    The strange case of Visual Studio getting "out of memory"
    http://msmvps.com/blogs/carlosq/archive/2009/06/09/the-strange-case-of-visual-studio-getting-quot-out-of-memory-quot.aspx

    The possible workarounds (none of them a happy solution) are:

    - Try using Project.CodeModel rather than iterating files.
    - Use the /3GB option of boot.ini if your OS doesn't crash. VS 2008 is already marked with the LARGEADDRESSAWARE flag.
    - Move to Windows 64-bit to get 4 GB of address space.
    MZ-Tools: Productivity add-ins for Visual Studio: http://www.mztools.com. My blog about developing add-ins: http://msmvps.com/blogs/carlosq/
    Monday, January 25, 2010 3:19 PM
  • Hello,

    See:

    The strange case of Visual Studio getting "out of memory"
    http://msmvps.com/blogs/carlosq/archive/2009/06/09/the-strange-case-of-visual-studio-getting-quot-out-of-memory-quot.aspx

    The possible workarounds (none of them a happy solution) are:

    - Try using Project.CodeModel rather than iterating files.
    - Use the /3GB option of boot.ini if your OS doesn't crash. VS 2008 is already marked with the LARGEADDRESSAWARE flag.
    - Move to Windows 64-bit to get 4 GB of address space.
    MZ-Tools: Productivity add-ins for Visual Studio: http://www.mztools.com. My blog about developing add-ins: http://msmvps.com/blogs/carlosq/


    But, the memory of VS is not high, and the system still has many free memory.
    My OS is windows 2003 enterprise edittion which surports more than 3GB memory. And actually it has 4GB memory.
    So I think this is anther problem.
    Monday, January 25, 2010 8:05 PM
  • Then you will have to send a reproducible case to Microsoft Connect:

    https://connect.microsoft.com/feedback/default.aspx?SiteID=210&wa=wsignin1.0

    MZ-Tools: Productivity add-ins for Visual Studio: http://www.mztools.com. My blog about developing add-ins: http://msmvps.com/blogs/carlosq/
    • Marked as answer by xiaoaowanghu Thursday, January 28, 2010 12:46 PM
    Tuesday, January 26, 2010 9:08 AM