1

I wrote an algorithm to detect my crystals (circle shape) which have imperfect edges. Since,I don't know how many crystals will appear in my system I had to go for a safe estimation (which was 100 crystals). So, I defined pictureboxes array and bitmap arrays as follow:

    public PictureBox[] segment = new PictureBox[100];
    public Bitmap[] cropped = new Bitmap[100];

when the algorithm is done. I can run a loop to count the number of crystals (which equals to sum of non null member of cropped or segment array above). To make the program better I was wondering if I can Dispose/truncate these two arrays.

  • Go for a List and List instead! But to dispose of them you need to do it piece by piece: `foreach (PictureBox pb in pbList) if (pb.Image != null) pb.Image.Dispose();` and `foreach (Bitmap bmp in bmpList) if (bmp != null) bmp.Dispose();` ! Only after disposing of the GDI element you can set the lists to null. – TaW Oct 18 '14 at 07:25

1 Answers1

1

If you don't know what the final number of crystals will be, I'd suggest to use Collections (for example a list). So you don't have to count the values that are not null, you can simply take the length of the list. A list has a dynamic length. That means you don't have to declare a fixed length and you can add as many elements as you want (in theory).

List<PictureBox> segment = new List<PictureBox>();
List<Bitmap> cropped = new List<Bitmap>();

See: http://www.dotnetperls.com/list

But to return to your question: I assume that by assigning null as the values of the arrays, the .NET garbage collector will handle the reallocation of memory for you.

See: http://msdn.microsoft.com/de-de/library/0xy59wtx(v=vs.110).aspx

I'm sorry for giving you a wrong information, please refer to the following article: How to delete an array in c#?

Community
  • 1
  • 1
WhiteBr0wnie_24
  • 149
  • 1
  • 1
  • 12
  • 1
    No, GC won't collect the GDI resources inside these Lists. – TaW Oct 18 '14 at 07:22
  • Thanks, @WhiteBr0wnie_24. wouldn't a List object (dynamic length array) be slower than simple array since it is dynamic? – maziar derakhshandeh Oct 18 '14 at 07:36
  • Speed of Lists is excellent, both in creating and in acessing. (They are arrays behind the scenes.) But since you are working with large datastructures, their speed should not be an issue anyway.. – TaW Oct 18 '14 at 07:44
  • Thanks :) you two saved my day... actually night :p – maziar derakhshandeh Oct 18 '14 at 07:48
  • I would say it really depends on you algorithm, I have found a really nice article about data structures here: http://msdn.microsoft.com/en-us/library/ms379570(v=vs.80).aspx or a 'shorter version' here: http://stackoverflow.com/questions/434761/array-versus-listt-when-to-use-which – WhiteBr0wnie_24 Oct 18 '14 at 07:48