0

Possible Duplicate:
How do i free objects in C#

my problem is i am using ..

 video = new Video(vpath[a]);
 video1 = new Video(vpath[a + 1]);
 video2 = new Video(vpath[a - 1]);

to show 3 videos at same time on a winform.. and this in a function which is called by an button event ... but it consuming to much memory ...

i did this

 video = null;
 video1 = null;
 video2 = null;

but its still not helping me out ... how i can reduce the memory consumption by this 3 objects??

my question will be flagged as possible duplicate of

https://stackoverflow.com/questions/2406794/how-do-i-free-objects-in-c-sharp

but i still want to get over this thing bcoz i didn't get help from that

Community
  • 1
  • 1
Drone
  • 181
  • 8
  • 22

3 Answers3

2

Setting object to null does not mean that it won't stay in memory until garbage collection will happen. You can explicitly call garbage collection (GC.Collect()) but this is not desirable unless you don't have another choice.

You can use dispose instead of forcing garbage collection.

class Video : IDisposable
{
    public void Dispose()
    {
        //Close managed resources and etc. 
    }
}

And use instances of class with using statement. Here is good answer how to use IDisposable pattern.

Community
  • 1
  • 1
Leri
  • 12,367
  • 7
  • 43
  • 60
  • i don't want use `GC.Collect()` any other option ? – Drone Oct 15 '12 at 07:01
  • Please read http://blogs.msdn.com/b/ricom/archive/2004/11/29/271829.aspx before you consider using `GC.Collect()`. I would use it only I am really sure at what I want to do and there is no alternative way. – Ekk Oct 15 '12 at 07:03
  • @Ekk You are completely right. I've just listed as a possible way. – Leri Oct 15 '12 at 07:04
  • ok can't i call `video.Dispose()` will this help? – Drone Oct 15 '12 at 07:10
  • @Drone after leaving `using` block `Dispose` is automatically called. of course you can call it manually to avoid `using` statements. – Leri Oct 15 '12 at 07:13
  • Ok its seems like by using `video1.Dispose()` and `video2.Dispose()` i am able to release its resources but i couldn't use `Dispose()` on `video` object bcoz only this object is used to play the video so now memory consumption is much less .. – Drone Oct 15 '12 at 07:35
  • @PLB thanks i am able to do it and maintain the memory to a certain limit.... thanks every one – Drone Oct 15 '12 at 07:44
0

you need release the resource

   using (Video video = new Video(vpath[a]))
   {
    .....
   }

and at the end, you can use SetProcessWorkingSetSize to trim the memory it uses, see answer at here: webbrowser control with memory increasing problem

Community
  • 1
  • 1
urlreader
  • 6,319
  • 7
  • 57
  • 91
  • Ok its seems like by using video1.Dispose() and video2.Dispose() i am able to release its resources but i couldn't use Dispose() on video object bcoz only this object is used to play the video so now memory consumption is much less – Drone Oct 15 '12 at 07:36
0

You could use WMP to display your videos instead, that way the WMP politely handles streaming of your video instead of loading it up all at once in memory.

How to: Embed Windows Media Player on a Form

You would then just use:

var video1 = new Uri("c:\\somefolder\\myVideo1.avi");
axWindowsMediaPlayer1.URL = video1.AbsoluteUri;

var video2 = new Uri("c:\\somefolder\\myVideo2.avi");
axWindowsMediaPlayer2.URL = video2.AbsoluteUri;

// and so on...
Vedran
  • 10,369
  • 5
  • 50
  • 57