none
ArrayList in a loop creates OutOfMemory Exception RRS feed

  • Question

  • I am having a problem with using ArrayList in a loop. Here is what I am doing

    byte[] buffer;
    ArrayList bufferList=new ArrayList();
    
    buffer=new byte[bufferSize];
    
    for(int i=0;i<bufferSize;i++){
        buffer[i]=i;
        bufferList.Add(buffer);
    }
    

    bufferSize is set by the user from a text box.

    What this does is that instead of adding a new entry of buffer into the Array List it keeps updating the entries to the latest value of buffer, so by the end of it I have bufferSize entries of the last buffer value in my bufferList.

    If I move the line buffer=new byte[bufferSize] inside the for loop it works fine. Except that when I run my program over and over again it gives an OutOfMemory exception. I tried calling GC.Collect() before I enter the for loop but that did not help.

    Any ideas????

    Regards,

    Yash

    Thursday, February 24, 2011 10:17 PM

Answers

  • Hi shah_yash,

    Based on your descriptions, you met with an OOM issue inside a loop.

    To the best of my knowledge, then the GC will automatically clean the resource if you are not keeping a reference to your object. So in your case, please first make sure that you don't hold any reference to buffer. Or could you please share more codes with us?

    In case that the GC doesn't clean the resource in time, you should call GC explicitly. So if you have any problem here, I think it is better to use the debugger tool like Windbg to debug your application and watch all objects (or the specific object) in the process. And try to monitor the objects and the GC. I think more information could be found in the debugging.

    Best regards


    Yves Zhang [MSFT]
    MSDN Community Support | Feedback to us
    Get or Request Code Sample from Microsoft
    Please remember to mark the replies as answers if they help and unmark them if they provide no help.

    Wednesday, March 2, 2011 3:50 AM
    Moderator

All replies

  • There are several problems here.

    1. Why are you adding the buffer (array) to the list? What you are doing is to add the same instance to the list repeatedly. Change that to bufferList.Add(buffer[i]) or even bufferList.Add(i) since it's the same thing here.
    2. Yes if you put it inside the loop and then add it to the list, you'll run out of memory because the GC cannot collect it anymore due to the references in the list.
    3. Unless you are still on .NET 1.1, why are you using an ArrayList? Why not use Collection<T> or List<T> here?
    4. What's the purpose of duplicating data in a byte array as well as a list? It's redundant.

    http://blog.voidnish.com
    Thursday, February 24, 2011 10:26 PM
    Moderator
  • Sorry,

    My example did not convey what exactly I am trying to achieve. I am trying to maintain a List of arrays, each array bufferSize long. Here is a better example:

    byte[] buffer;
    List<byte[]> bufferList=new List<byte[]>();
    Stream file;
    Socket testSocket;
    
    buffer=new buffer[bufferSize];
    
    for(int i=0;i<file.Length;i+=bufferSize){
      file.Read(buffer,0,buffer.Length);
      bufferList.Add(buffer);
    }
    
    for(int i=0;i<bufferList.Length;i++){
      testSocket.Send((byte[])bufferList[i]);
    }
    
    

    I am trying to read a file using a stream and send it out of a socket bufferSize bytes at a time.

    The file is selected from a OpenFileDialog. I read the file in chunks of bufferSize bytes into the buffer and add each buffer to a List. Once I am with that I send the file out in chunks of bufferSize bytes using the List.

    As I described my problem earlier, this only works if I add buffer=new[bufferSize] inside the first for loop. If I do that I get a OutOfMemoryException.

    Any ideas will be appreciated.

    Thanks,

    Yash

    Monday, February 28, 2011 11:38 PM
  • Hi shah_yash,

    Based on your descriptions, you met with an OOM issue inside a loop.

    To the best of my knowledge, then the GC will automatically clean the resource if you are not keeping a reference to your object. So in your case, please first make sure that you don't hold any reference to buffer. Or could you please share more codes with us?

    In case that the GC doesn't clean the resource in time, you should call GC explicitly. So if you have any problem here, I think it is better to use the debugger tool like Windbg to debug your application and watch all objects (or the specific object) in the process. And try to monitor the objects and the GC. I think more information could be found in the debugging.

    Best regards


    Yves Zhang [MSFT]
    MSDN Community Support | Feedback to us
    Get or Request Code Sample from Microsoft
    Please remember to mark the replies as answers if they help and unmark them if they provide no help.

    Wednesday, March 2, 2011 3:50 AM
    Moderator