unsafe operations

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

    #1

    unsafe operations

    if I have an unsafe method that takes in two arrays, performs some
    operations and returns; I know that the garbage collector will not
    collect objects allocated inside the unsafe method. However, if I'm
    calling this method from a main() method of a class which is safe (i.e
    managed), will the allocated array (which, suppose, has been allocated
    inside the main() method) be collected by the garbage collector after
    it's been used? Also, how do we delete heap allocated objects (suppose a
    temporary array) from within the unsafe method? Is the unsafe method
    treated as a region (term taken from strict Realitime environment) and
    discarded by the GC once it's been used (or is it the method stack
    that's discarded)? Just making sure I know what my code does before I
    implement it :) thanks

    -Andre

  • Rob Teixeira [MVP]

    #2
    Re: unsafe operations

    The GC will collect objects in an unsafe block. The trick is to pin the
    objects so the GC *doesn't* collect them while you're using them. That's
    what the "fixed" block is for. Look up the Fixed block in the help file, and
    there's a sample. Once the code gets passed the fixed block, the objects
    become unpinned, and the GC is able to collect them on the next round.

    -Rob [MVP]

    "Andre" <food_Crazy@hot mail.com> wrote in message
    news:3F1F6280.2 000704@hotmail. com...[color=blue]
    > if I have an unsafe method that takes in two arrays, performs some
    > operations and returns; I know that the garbage collector will not
    > collect objects allocated inside the unsafe method. However, if I'm
    > calling this method from a main() method of a class which is safe (i.e
    > managed), will the allocated array (which, suppose, has been allocated
    > inside the main() method) be collected by the garbage collector after
    > it's been used? Also, how do we delete heap allocated objects (suppose a
    > temporary array) from within the unsafe method? Is the unsafe method
    > treated as a region (term taken from strict Realitime environment) and
    > discarded by the GC once it's been used (or is it the method stack
    > that's discarded)? Just making sure I know what my code does before I
    > implement it :) thanks
    >
    > -Andre
    >[/color]


    Comment

    • Andre

      #3
      Re: unsafe operations

      Thanks Rob, that answers my question.


      cheers,

      -Andre

      Rob Teixeira [MVP] wrote:[color=blue]
      > The GC will collect objects in an unsafe block. The trick is to pin the
      > objects so the GC *doesn't* collect them while you're using them. That's
      > what the "fixed" block is for. Look up the Fixed block in the help file, and
      > there's a sample. Once the code gets passed the fixed block, the objects
      > become unpinned, and the GC is able to collect them on the next round.
      >
      > -Rob [MVP]
      >
      > "Andre" <food_Crazy@hot mail.com> wrote in message
      > news:3F1F6280.2 000704@hotmail. com...
      >[color=green]
      >>if I have an unsafe method that takes in two arrays, performs some
      >>operations and returns; I know that the garbage collector will not
      >>collect objects allocated inside the unsafe method. However, if I'm
      >>calling this method from a main() method of a class which is safe (i.e
      >>managed), will the allocated array (which, suppose, has been allocated
      >>inside the main() method) be collected by the garbage collector after
      >>it's been used? Also, how do we delete heap allocated objects (suppose a
      >>temporary array) from within the unsafe method? Is the unsafe method
      >>treated as a region (term taken from strict Realitime environment) and
      >>discarded by the GC once it's been used (or is it the method stack
      >>that's discarded)? Just making sure I know what my code does before I
      >>implement it :) thanks
      >>
      >>-Andre
      >>[/color]
      >
      >
      >[/color]

      Comment

      • Rob Teixeira [MVP]

        #4
        Re: unsafe operations

        I'd have to re-read the specs on the current GC, but the bottom line is that
        at some point, everything gets collected.
        There is no explicit delete of managed memory.

        -Rob

        "Andre" <food_Crazy@hot mail.com> wrote in message
        news:3F1F7C5B.2 060002@hotmail. com...[color=blue]
        > An afterthought - how do I explicitly 'delete' an object from within an
        > unsafe block like in C++? Is there an analogous operation available for
        > that? thanks
        >
        > -Andre
        >
        > Andre wrote:[color=green]
        > > One more thing... what if the unsafe code block allocated space larger
        > > than 85Kb (i.e a single object was more than 85Kb), this would mean that
        > > the GC would store this in the Large Object Space, which isn't
        > > collected, and we'll end up with no or little memory? Or is this not the
        > > case with unsafe code blocks?
        > >
        > > -Andre
        > >
        > >
        > > Rob Teixeira [MVP] wrote:
        > >[color=darkred]
        > >> The GC will collect objects in an unsafe block. The trick is to pin the
        > >> objects so the GC *doesn't* collect them while you're using them.[/color][/color][/color]
        That's[color=blue][color=green][color=darkred]
        > >> what the "fixed" block is for. Look up the Fixed block in the help
        > >> file, and
        > >> there's a sample. Once the code gets passed the fixed block, the[/color][/color][/color]
        objects[color=blue][color=green][color=darkred]
        > >> become unpinned, and the GC is able to collect them on the next round.
        > >>
        > >> -Rob [MVP]
        > >>
        > >> "Andre" <food_Crazy@hot mail.com> wrote in message
        > >> news:3F1F6280.2 000704@hotmail. com...
        > >>
        > >>> if I have an unsafe method that takes in two arrays, performs some
        > >>> operations and returns; I know that the garbage collector will not
        > >>> collect objects allocated inside the unsafe method. However, if I'm
        > >>> calling this method from a main() method of a class which is safe (i.e
        > >>> managed), will the allocated array (which, suppose, has been allocated
        > >>> inside the main() method) be collected by the garbage collector after
        > >>> it's been used? Also, how do we delete heap allocated objects (suppose[/color][/color][/color]
        a[color=blue][color=green][color=darkred]
        > >>> temporary array) from within the unsafe method? Is the unsafe method
        > >>> treated as a region (term taken from strict Realitime environment) and
        > >>> discarded by the GC once it's been used (or is it the method stack
        > >>> that's discarded)? Just making sure I know what my code does before I
        > >>> implement it :) thanks
        > >>>
        > >>> -Andre
        > >>>
        > >>
        > >>
        > >>[/color]
        > >[/color]
        >[/color]


        Comment

        Working...