locked
Problem with exsessive memory usage RRS feed

  • Question



  • Hi all,

    This is my second post. I'm busy working on a personal app that does everything windows does..... Play music, display pics, move copy.... files ens. Just the way i want windows to work. Its all for the sake of learning programming. Anyway. My program works rather well i just picked up something on my taskmanager the other day. When i initialy start up my app it uses around 23MB. As i go along playing music or video files the amount of memory it uses keeps on going up and up and after a while my app bombs out.... It looks like it happens around 100MB. This only happens after a long time playing all sorts of media on my app. I've used a variable "MYVideo" and "MYAudio" that uses directx to play files. Here is my code:

    VIDEO
        Public Sub PlayVideo(ByVal FullPath As String, ByVal VideoOutput As Panel)
            Try
                VideoOutput.Visible = False
                ' Set size to panel size
                DefaultVideoSize = VideoOutput.Size
                ' If video is running stop it
                If MYAudio IsNot Nothing Then
                    MsgBox("audio")
                    MYAudio.Stop()
                    MYAudio = Nothing
                End If
                ' If MYVideo doesn't exist then create a new instance
                If MYVideo Is Nothing Then
                    MYVideo = New Video(FullPath)
                    m_CurrentLoadedFile = FullPath
                    MYVideo.Owner = VideoOutput
                Else
                    MYVideo.Stop()
                    MYVideo = AudioVideoPlayback.Video.FromFile(FullPath)
                    m_CurrentLoadedFile = FullPath
                    MYVideo.Owner = VideoOutput
                End If
                VideoOutput.Size = DefaultVideoSize
                MYVideo.Play()

                ' Enable Timer
                MainForm.Timer.Enabled = True
                videooutput.Visible = True
                VideoFine = True
            Catch ex As Exception
                videofine = False
            End Try
        End Sub

    AUDIO
        Public Sub PlayAudio(ByVal FullPath As String)
            Try
                ' If video is running stop it
                If MYVideo IsNot Nothing Then
                    MYVideo.Stop()
                    MYVideo = Nothing
                End If
                ' If Myaudio doesn't exist then create a new instance
                If MYAudio Is Nothing Then
                    MYAudio = New Audio(FullPath)
                    m_CurrentLoadedFile = FullPath
                Else
                    MYAudio.Stop()
                    MYAudio = AudioVideoPlayback.Audio.FromFile(FullPath)
                    m_CurrentLoadedFile = FullPath
                End If
                MYAudio.Play()

                ' Enable Timer
                MainForm.Timer.Enabled = True
                AudioFine = True
            Catch ex As Exception
                AudioFine = False
            End Try
        End Sub

    I assumed that making the Audio or video NOTHING that it would clear the memory they used... Apparently not. I also tried putting in MYVideo.Dispose() but that doesn't seem to work. My app's mem usage still goes up. I'm still rather new to programming so i dont really know how to clear memory. I just wanted some advice. If anyone has some advice it would be appreciated..... Thanks

    Friday, January 4, 2008 11:54 AM

Answers

  • You probably want to learn about Garbage Collection (System.GC).

    Setting an object to Nothing doesn't mean it's cleared the memory space (as you've already found out) until GC handles it which isn't always when you expect it to.

     

    Also, what happens if you hit a Catch block? There isn't any tidy-up code there if anything goes wrong since you would have skipped any tidy-up code in the Try block.

     

    I don't know anything about DirectX, so guessing here.

    Is there any sort of "Flush" or "Clear" in an attempt to free up existing memory before loading the new object?

     

    A few code snippets you can try:

     

    Code Block

    _myObject= Nothing

    GC.Collect()

     

     

     

    This one is slight overkill but had to use something like this to kill off remote instances of Excel

    Code Block

    Friend Class myClass

        Implements IDisposable

     

    Public Sub Dispose() Implements System.IDisposable.Dispose

        GC.SuppressFinalize(Me)

     

        Try

            System.Runtime.InteropServices.Marshal.ReleaseComObject(_myObject)

        Catch ex As Exception

        Finally

            Dim index As Int64 = GC.GetGeneration(_myObject)

            _myObject = Nothing

            GC.Collect(index)

        End Try

    End Sub

     

    End Class

     

     

    Hope this is of some help.
    Friday, January 4, 2008 12:51 PM

All replies

  • You probably want to learn about Garbage Collection (System.GC).

    Setting an object to Nothing doesn't mean it's cleared the memory space (as you've already found out) until GC handles it which isn't always when you expect it to.

     

    Also, what happens if you hit a Catch block? There isn't any tidy-up code there if anything goes wrong since you would have skipped any tidy-up code in the Try block.

     

    I don't know anything about DirectX, so guessing here.

    Is there any sort of "Flush" or "Clear" in an attempt to free up existing memory before loading the new object?

     

    A few code snippets you can try:

     

    Code Block

    _myObject= Nothing

    GC.Collect()

     

     

     

    This one is slight overkill but had to use something like this to kill off remote instances of Excel

    Code Block

    Friend Class myClass

        Implements IDisposable

     

    Public Sub Dispose() Implements System.IDisposable.Dispose

        GC.SuppressFinalize(Me)

     

        Try

            System.Runtime.InteropServices.Marshal.ReleaseComObject(_myObject)

        Catch ex As Exception

        Finally

            Dim index As Int64 = GC.GetGeneration(_myObject)

            _myObject = Nothing

            GC.Collect(index)

        End Try

    End Sub

     

    End Class

     

     

    Hope this is of some help.
    Friday, January 4, 2008 12:51 PM

  • I looked for something in directx to clear up the object before i use a new one but didn't see anything..... I don't know directx either but i'm trying.... Thanks for your help i'll try out the code you sent me. I noticed something else on my app though. I also have a tab to view pictures in. I see that once my app reaches +-100MB of ram it starts back up at around 20MB again. It looks like it. For some reason this doesn't happen when i play music. Anyway. Thanks for all your help. I appreciate it.
    Monday, January 7, 2008 2:35 PM