1

I have declared an array in C# this way:

  public ushort[][] pixels16;

then I fill it with pixel data of 500 images, each element in the array contains 262144 items. Problem is that gets too big so after I use it so I need to get rid of it to free up memory.. How can I do this?

braX
  • 11,506
  • 5
  • 20
  • 33
Matimont
  • 739
  • 3
  • 14
  • 33

4 Answers4

8

You wrote:

I have declared an array in C# this way:
public ushort[][] pixels16;

To be clear: You have NOT declared an array. You have declared a reference, called pixels16.

That reference may be to any 2-D array of ushorts, but it initially references nothing (null).

You actually create the array when you do:

pixels16 = new ushort[500, 500];

Similarly, you can make that array eligible for garbage collection when you no longer reference it:

pixels16 = null;  // Now the array is ready for Garbage Collection.

The garbage collector is very good at its job, and it is not recommended that you manually call for garbage collection. It will, in all likelyhood, collect the garbage very quickly and efficiently, and keep your program running with no problems.

abelenky
  • 63,815
  • 23
  • 109
  • 159
5

you can do :

pixels16 = null;
GC.Collect() // to free memory
Akrem
  • 5,033
  • 8
  • 37
  • 64
  • 5
    Its not a good practice to call the garbage collector manually!! Have a look [Best Practice for Forcing Garbage Collection in C#](http://stackoverflow.com/questions/233596/best-practice-for-forcing-garbage-collection-in-c-sharp) – huMpty duMpty Dec 31 '13 at 15:26
  • 1
    @Matimont - how are you measuring memory usage? Some methods are not all that responsive or accurate. Also, calling GC.Collect() may not force *immediate* garbage collection. It's best to just let .NET handle this and leave it alone unless you have a specific problem. – Jon B Dec 31 '13 at 15:34
  • @Jon, I finally changed my code to not fill in the array and instead fill it when was needed. I thought pre filling the array would increase speed but I was wrong. Thanks for your answers all – Matimont Dec 31 '13 at 18:31
1

I think you can use:

pixels16 = null;

And use System.GC.Collect() for force GC to run but this is not recommend.

It is possible to force garbage collection by calling Collect, but most of the time, this should be avoided because it may create performance issu

0

As Akrem implied, it is enough to remove all existing references to object (pixels=null). Then GC.Collect(); will forcibly awake Garbage Collector to do its job.

Unless you are worried about memory, you can just ignore it, the moment code block containing variable (for example function) will end then in its next pass GC will clean up everything. However you must remember that as long as even single reference to object will exist, then GC will not touch it. Just remember that GC works in mysterious ways so object might be removed a tad later.

However the question arises, why would you store 500 images in memory at once? You might want to seek way of putting it into a loop that will load single image at once, re-using same buffer multiple times.

PTwr
  • 1,225
  • 1
  • 11
  • 16