Array allocation and garbage collection

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Jon

    #1

    Array allocation and garbage collection

    If I allocate an array, eg:

    double[] x = new double[10000];

    then use the array, then at some later point, allocate it again, eg:

    double[] x = new double[15000];

    does the garbage collector know that the memory allocated the first time round can be garbage
    collected, or do I have to set x to null first before allocating the second time around?


  • Jon Skeet [C# MVP]

    #2
    Re: Array allocation and garbage collection

    On Jan 17, 1:24 pm, "Jon" <.wrote:
    If I allocate an array, eg:
    >
    double[] x = new double[10000];
    >
    then use the array, then at some later point, allocate it again, eg:
    >
    double[] x = new double[15000];
    >
    does the garbage collector know that the memory allocated the first time round can be garbage
    collected, or do I have to set x to null first before allocating the second time around?
    I assume the latter line was meant to just be:

    x = new double[15000];

    otherwise you're declaring a different variable.

    However, the garbage collector is smart enough to know that if nothing
    currently refers to the array, it can be garbage collected. Setting
    the variable to null before setting it to another value wouldn't make
    any difference (beyond being more fluff to understand, basically).

    Jon

    Comment

    • Ignacio Machin \( .NET/ C# MVP \)

      #3
      Re: Array allocation and garbage collection

      Hi,




      "Jon" <.wrote in message news:OlDznjRWIH A.4712@TK2MSFTN GP04.phx.gbl...
      Thanks Jon and Ignacio,
      >
      Jon:
      "I assume the latter line was meant to just be: x = new double[15000];"
      Yes, sorry about this mistake.
      >
      Ignacio:
      It's defined within a class, as part of the member variables section, then
      it is used and
      re-allocated in various different methods within that class.
      >
      class Class1{
      double[] x = new double[10000];
      ...
      method1(){
      // use x
      }
      method1(){
      // use x
      }
      method1(){
      x = new double[15000];
      }
      >
      The question is, do more than one method use the same values of X or you
      create it each time you start a new method?

      --
      Ignacio Machin
      The #1 Warehouse Management System & Direct Store Delivery Software (DSD) for QuickBooks & ERP Systems – LaceUp Solutions

      Mobile & warehouse Solutions.


      Comment

      • =?ISO-8859-1?Q?Arne_Vajh=F8j?=

        #4
        Re: Array allocation and garbage collection

        Jon wrote:
        It's defined within a class, as part of the member variables section, then it is used and
        re-allocated in various different methods within that class.
        >
        class Class1{
        double[] x = new double[10000];
        ...
        method1(){
        // use x
        }
        method1(){
        // use x
        }
        method1(){
        x = new double[15000];
        }
        Yes.

        When you reallocate the original 10000 element array will
        be ready for GC unless you somehow managed to get another
        variable (that are still reachable) to point to it.

        Arne

        Comment

        • =?ISO-8859-1?Q?Arne_Vajh=F8j?=

          #5
          Re: Array allocation and garbage collection

          Jon Skeet [C# MVP] wrote:
          However, the garbage collector is smart enough to know that if nothing
          currently refers to the array, it can be garbage collected.
          Refer as in reachable not as in points to.

          Arne

          Comment

          • Jon

            #6
            Re: Array allocation and garbage collection

            Thanks everyone for your replies, it's now very clear to me.

            Jon


            "Jon" <.wrote in message news:OHpAPxQWIH A.3556@TK2MSFTN GP02.phx.gbl...
            If I allocate an array, eg:

            double[] x = new double[10000];

            then use the array, then at some later point, allocate it again, eg:

            double[] x = new double[15000];

            does the garbage collector know that the memory allocated the first time round can be garbage
            collected, or do I have to set x to null first before allocating the second time around?



            Comment

            Working...