2

I've looked at answers that are similar to this issue, but I still don't think I understand why I'm getting an exception.

I'm attempting to debug a section of code that is throwing out an "OutOfMemoryException" when writing to a text file, according to the stacktrace. The exception is being thrown from the following line:

WriteToLogFile("Found in EnumerateActiveDirectoryFilteredMembers: " + e.Message);

The WriteToLogFile method is as follows:

    static void WriteToLogFile(string strLine)
    {
        using (StreamWriter sw = new StreamWriter(strLogFileName, true))
        {
            sw.WriteLine("[" + DateTime.Now.ToString() + "]: " + strLine);
        }
    }

It looks like StreamWriter objects can throw this exception sometimes, but isn't the memory being reallocated at the end of this method every time it is called? And isn't the use of the "using" keyword ensuring tht this object is being thrown away?

The file, by the time the exception is thrown, is only 13 KB - so what exactly is going on here?

UPDATE: The OutOfMemoryException is not the WriteToLogFile method. It's being thrown earlier, but I don't know where this is coming from... I'll add a skeleton of the try statement before the catch:

        try
        {
            if (objSearchResults.Count != 0)
            {
                // ADD TO DATASET HERE //
                string tableName = "tempADhold";
                DataSet domains = new DataSet();
                domains.Tables.Add(tableName);

                //add x number of columns
                domains.Tables[tableName].Columns.Add("DomainID", typeof(string));
                .
                .
                .

                // Get the object from AD.
                foreach (SearchResult objResultUser in objSearchResults)
                {
                    // Init variables and init formatting of strings for adding objects
                    string DomainID = ""; string UserName = ""; string Title = "";
                    .
                    .
                    .

                    objUserEntry = objResultUser.GetDirectoryEntry();
                    // Populate obj and LDAP path variables.  
                    // Perform edits before setting variables. 
                    //   Match strings to SQL Server table sizes.

                    // add objects in same manner:
                    // DomainID
                    if (objUserEntry.Properties["samAccountName"].Count > 0)
                    {
                        DomainID = objUserEntry.Properties["samAccountName"].Value.ToString();
                        if (DomainID.Substring(0, 1).ToUpper() != "N")
                        {
                            WriteToLogFile("RECORD SKIPPED: Invalid N_Number: " + DomainID);
                            continue;  //skip this record
                        }
                        else if (DomainID.Length > 8)
                        {
                            WriteToLogFile("RECORD SKIPPED: Invalid N_Number length. #s ending in 'c' should be skipped: " + DomainID);
                            continue;  //skip this record
                        }

                    }
                    .
                    .
                    .

                    //set up array struct for adding obj

                    ADUserList[0] = DomainID; ADUserList[1] = UserName;
                    ADUserList[2] = ...; ADUserList[3] = ...;
                    ADUserList[4] = ...; ADUserList[5] = ...;
                    ADUserList[6] = ...; ADUserList[7] = ...;
                    ADUserList[8] = ...; ADUserList[9] = ...;
                    ADUserList[10] = ...; ADUserList[11] = ...;
                    ADUserList[12] = ...; ADUserList[13] = ...;
                    ADUserList[14] = ...; ADUserList[15] = ...;
                    ADUserList[16] = ...;

                    if (ADUsersIndex > 0 && ADUsersIndex % 2500 == 0)
                    {
                        //Add to dataset instead of array here...
                        if (InsertRows(domains.Tables[tableName]).Equals(false)) { return false; }
                        domains.Tables[tableName].Rows.Clear();
                    }

                    DataRow myRow = domains.Tables[tableName].NewRow();
                    myRow.ItemArray = ADUserList;
                    domains.Tables[tableName].Rows.Add(myRow);
                    ADUsersIndex++;

                }
                //Write the last rows to the database.
                if (InsertRows(domains.Tables[tableName]).Equals(false)) { return false; }
                objSearchResults.Dispose();


            } //end "if (objSearchResults.Count != 0)"

            else
            {
                WriteToLogFile("Results: No Active Directory filtered members found.");
            }
        }
        catch (Exception e)
        {
           if (e is OutOfMemoryException) throw;
           WriteToConsole("Error in EnumerateActiveDirectoryFilteredMembers: " + e.Message);
           return true;
        }
12
  • 1
    Out of memory exception is triggered by the last drop, but may not be the main contributor to the problem. The code as posted is not the culprit. Commented Aug 8, 2014 at 18:00
  • Following the trace only leads me to the WriteToLogFile call... I'm new to using Visual Studio. Do you have a recommendation on tracking where this is being thrown if not at this call? Or perhaps figure out what is out of memory, such as a particular object, allocated space, etc? Commented Aug 8, 2014 at 18:04
  • 1
    Check this question and its answers: Possible reasons for FileStream.Write() to throw an OutOfMemoryException?. I am not sure whether it will really help you (but it seems to be, assuming that WriteToLogFile is perhaps called frequently), but it is at least worth a look... Commented Aug 8, 2014 at 18:05
  • What was the total working set of the process at the time when exception was thrown? Commented Aug 8, 2014 at 18:05
  • 1
    It's not closed, it's not opened, it's in a using clause, it's not the problem.. Commented Aug 8, 2014 at 18:52

1 Answer 1

2

This message is being throw trying to run

WriteToLogFile("Found in EnumerateActiveDirectoryFilteredMembers: " + e.Message);

inside an error handler

e is almost certainly an OutOfMemory exception already, but you handler is throwing when trying to log the message, 'hiding' the real stacktrace.

Add this to your error handler BEFORE the WriteToLogFile line:

if (e is OutOfMemoryException) throw;

This will then still cause the app to die, but give you the real StackTrace of original error.

Sign up to request clarification or add additional context in comments.

5 Comments

Will do. The program has to run for a while to throw the exception, so I'll get back to you with results.
Okay - I re-ran, and you were right, the exception is being thrown before. I've updated the above to include my try statement before the catch. The state of the objects are in fine order, nothing is above the allowable size limits. But basically this program is handling upwards of 32,000 transactions at it's peak. What could be throwing the Exception in the try statement? I'm not seeing anything that would cause this type of error... Then again, I could be missing something obvious.
Can you post the stack trace?
It wasn't all that helpful. In summary it traced origin to the call of the above try stmnt's fnctn, which is as follows: ' if (objADtoTable.EnumerateActiveDirectoryFilteredMembers(AD_R16_F1, AD_R16_F2).Equals(false)) { return -1; }' this call is in main. Main is getting group information from a config file (setting variables about yes or no to truncate, and assigning group "values" like 001, 002, etc. to local vars like 'AD_R16_F1'. Then it calls the function 'EnumerateActiveDirectoryFilteredMembers', passing in these groups. The try stmnt above is the meat of "Enumerate" fnctn.
But I'll post it as soon this current run is complete.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.