Results 1 to 7 of 7

Thread: releasing memory manually?

  1. #1

    Thread Starter
    l33t! MrPolite's Avatar
    Join Date
    Sep 2001
    Posts
    4,428

    releasing memory manually?

    I'm wondering if it's possible to do it without garbage collection.
    If I have a variable (reference type), can I "delete" it from memory anyhow?
    I think C++ had a delete operator, uh cant remember
    rate my posts if they help ya!
    Extract thumbnail without reading the whole image file: (C# - VB)
    Apply texture to bitmaps: (C# - VB)
    Extended console library: (VB)
    Save JPEG with a certain quality (image compression): (C# - VB )
    VB.NET to C# conversion tips!!

  2. #2
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    111,221

    Re: releasing memory manually?

    I would think that the GC.Collect method should be adequate in most situations. You may also like to read this. It may also interest you to know that .NET 2.0 has a SecureString class, instances of which are removed (somehow) from memory once they are no longer referenced. Good for passwords and the like.

  3. #3
    Retired G&G Mod NoteMe's Avatar
    Join Date
    Oct 2002
    Location
    @ Opera Software
    Posts
    10,190

    Re: releasing memory manually?

    Correct me if I am wrong here. But in C++ you don't delete a reference either. Because a reference is just a reference, and not the actual object. So if you have a reference in C++, you know that it is NOT your job to delete it. It is the guy that sits on the actual object, or the pointer to the object (here it get difficult when you have many pointers too it) that has the actual job to delete it.



    But in C#, you are supposed to let the GC to collect it when it is ready to get deleted. The destructor doesn't work the same way in C++ as in C#. In C++ it deltetes the whole object, and voila. But in C#, it deletes the what you have spesified in the destructor, but not the object it self. The object it self doesn't get delted before the GC is ready to run again. And it doesn't run all the time. But as a final word on this. To make sure your resources gets deleted more or less when you want, you have to implement the IDisposable interface, and then explicitly call the Dispose() method.


    - ØØ -

  4. #4
    Hyperactive Member GlenW's Avatar
    Join Date
    Nov 2001
    Location
    Gateshead, England
    Posts
    479

    Re: releasing memory manually?

    It is bad practice to call GC.Collect, it is a, comparitively, very slow procedure.
    The logic behind GC is that it will be called automatically when it is needed by the operating system, the need not the call, usually very infrequently.
    I'm sure some geek here will be able to cite an scenario when GC.Collect should be called but I can't think of one.
    Basically; create an object, use it, forget it.

  5. #5
    Retired G&G Mod NoteMe's Avatar
    Join Date
    Oct 2002
    Location
    @ Opera Software
    Posts
    10,190

    Re: releasing memory manually?

    I am no geek, but after loading a world in a game, and you are ready to play the level can be a good place to call it..


    But as a general rule, yes, it is not something you are supposed to do during run time.


    - ØØ -

  6. #6

    Thread Starter
    l33t! MrPolite's Avatar
    Join Date
    Sep 2001
    Posts
    4,428

    Re: releasing memory manually?

    Quote Originally Posted by GlenW
    It is bad practice to call GC.Collect, it is a, comparitively, very slow procedure.
    The logic behind GC is that it will be called automatically when it is needed by the operating system, the need not the call, usually very infrequently.
    I'm sure some geek here will be able to cite an scenario when GC.Collect should be called but I can't think of one.
    Basically; create an object, use it, forget it.
    APPARENTLY it's not called automatically
    As an example: I was working on an app where many many instances of images are loaded and then disposed. I was calling the Dispose method of each item properly but the ram usage would reach 2GB and then the operating system would puke
    I "had to" call GC.Collect in my case so I dunno
    that's why I wanted to know if there's a way to get rid of somethign manually, so I wouldn't have to call GC.Collect
    rate my posts if they help ya!
    Extract thumbnail without reading the whole image file: (C# - VB)
    Apply texture to bitmaps: (C# - VB)
    Extended console library: (VB)
    Save JPEG with a certain quality (image compression): (C# - VB )
    VB.NET to C# conversion tips!!

  7. #7
    type Woss is new Grumpy; wossname's Avatar
    Join Date
    Aug 2002
    Location
    #!/bin/bash
    Posts
    5,682

    Re: releasing memory manually?

    My thoughts (feel free to discard them)

    * It is good practice to use GC.Collect because you are telling the app that this is a good time to dump a load of ram. However, this must not be overused, and certainly NEVER inside a loop. You should notify users that a long ram dump is occuring.

    * If you are using 2gb's of ram then its time for a serious rethink of your duty cycle.
    Last edited by wossname; Jul 28th, 2005 at 01:56 PM.
    I don't live here any more.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width