Simple program memory usage?

brechtjah

Member
Joined
Mar 23, 2008
Messages
23
Programming Experience
Beginner
Hi,
I am working on a simple organiser that just reads info from a file then displays it and stuff. However, I seem to have two problems, but I'll keep this thread to just one cause they are both different, well kind of...
Anyway,
I've noticed that my program takes a lot of memory, usually around 18 mb. While the only thing I'm using is a few timers (2) and pfff let's say 2 pages of code for the main form. How can this be? It's not because I have a lot of variables taking a lot of space. I was hoping to keep my program rather small in memory usage (something around 10 mb max.)
If you guys could help me with this, or if you need to see the code to help just post it here...
Thanks
 
We would need to know what you're program's doing and how it's being done before we can begin to answer that
 
Ok well, this is my code so far, for the Main form.
VB.NET:
Public Class Main
    Dim boolVerkleindVenster = True
    Dim onChange As Boolean = True
    Dim uurWissel As Boolean = False
    Dim intUurAanpassing As Integer = 0
    Dim dteDatumAanpassing As Date = Today
    Dim intUur1, intUur2 As Integer
    Dim dteDatum1, dteDatum2 As Date

    Private Sub LayoutCTRLTimer_Tick(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles LayoutCTRLTimer.Tick
        Call VeranderingCheck()

        If onChange = True Then
            Dim intUur, intI, intTeller As Integer
            intUur = TimeOfDay.Hour + intUurAanpassing
            intTeller = 0

            'DISPLAY AANPASSEN AAN HUIDIG UUR
            For intI = intUur - 6 To intUur + 6
                'UUR KAN NIET GROTER DAN 23 of KLEINER DAN NUL
                If intI < 24 And intI > -1 Then
                    Me.Controls("lbl" & intTeller).Text = intI
                    intTeller = intTeller + 1
                Else
                    Me.Controls("lbl" & intTeller).Text = ""
                    intTeller = intTeller + 1
                End If
            Next


            'JUISTE DATUM TONEN IN LABEL
            Dim dteHuidigeDatum As Date
            Dim tspDatumsVerschil As TimeSpan

            dteHuidigeDatum = Date.Today
            tspDatumsVerschil = dteDatumAanpassing.Subtract(dteHuidigeDatum)
            lblHuidigePlanning.Text = dteDatumAanpassing.ToLongDateString & " (" & tspDatumsVerschil.Days & ")"


            'GEPLANDE ITEMS LEGEN
            Dim intA As Integer
            For intA = 0 To 12
                Me.Controls("lblUur" & intA).Text = ""
            Next


            'GEPLANDE ITEMS AANPASSEN
            Dim strDatum, strLijn As String
            Dim intNummer, intMinRange As Integer
            strDatum = DatumWeergeven()


            If My.Computer.FileSystem.FileExists(Application.StartupPath & "\" & strDatum & ".txt") = True Then
                Dim Lezer As System.IO.StreamReader
                lblGeenPlanning.Visible = False
                Aanduiding.Visible = True
                Lezer = My.Computer.FileSystem.OpenTextFileReader(Application.StartupPath & "\" & strDatum & ".txt")

                intNummer = 0
                intUur = TimeOfDay.Hour + intUurAanpassing
                intMinRange = intUur - 6
                If intMinRange < 0 Then
                    intNummer = intNummer - intMinRange
                End If
                strLijn = Lezer.ReadLine()
                For intTeller = 0 To 23
                    If intTeller >= intUur - 6 And intTeller <= intUur + 6 Then
                        Me.Controls("lblUur" & intNummer).Text = strLijn
                        intNummer = intNummer + 1
                    End If
                    strLijn = Lezer.ReadLine()
                Next

                Lezer.Close()
            Else
                'BESTAND BESTAAT NIET; labels leegmaken
                For intTeller = 0 To 12
                    Me.Controls("lblUur" & intTeller).Text = ""
                    Me.Controls("lbl" & intTeller).Text = ""
                    lblGeenPlanning.Visible = True
                    Aanduiding.Visible = False
                Next
            End If

            onChange = False
        End If
    End Sub

    Function DatumWeergeven() As String
        Dim intJaar, intMaand, intDag As Integer

        'DATUM MET -TEKENS MAKEN
        intJaar = dteDatumAanpassing.Year
        intMaand = dteDatumAanpassing.Month
        intDag = dteDatumAanpassing.Day
        Return intJaar & "-" & intMaand & "-" & intDag
    End Function

    Function VeranderingCheck() As Boolean
        'onChange BEPALEN
        If uurWissel = False Then
            intUur1 = intUurAanpassing
            dteDatum1 = dteDatumAanpassing
            uurWissel = True
        Else
            intUur2 = intUurAanpassing
            dteDatum2 = dteDatumAanpassing
            uurWissel = False
        End If

        'UREN VERGELIJKEN
        If Not intUur1 = intUur2 Or Not dteDatum1 = dteDatum2 Then
            onChange = True
        End If
    End Function

    Private Sub btnVolgendUur_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnVolgendUur.Click

        'DISPLAY AANDUIDING KAN NIET HOGER DAN UUR 23
        If TimeOfDay.Hour + intUurAanpassing < 23 Then
            intUurAanpassing = intUurAanpassing + 1
        End If
    End Sub

    Private Sub btnVorigUur_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnVorigUur.Click

        'DISPLAY AANDUIDING KAN NIET LAGER DAN UUR 0
        If TimeOfDay.Hour + intUurAanpassing > 0 Then
            intUurAanpassing = intUurAanpassing - 1
        End If
    End Sub

    Private Sub btnVorigeDag_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnVorigeDag.Click
        dteDatumAanpassing = dteDatumAanpassing.AddDays(-1)
    End Sub

    Private Sub btnVolgende_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnVolgende.Click
        dteDatumAanpassing = dteDatumAanpassing.AddDays(1)
    End Sub

    Private Sub btnReset_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnReset.Click
        intUurAanpassing = 0
        dteDatumAanpassing = Today
    End Sub

    Private Sub btnAfsluiten_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnAfsluiten.Click
        Application.Exit()
    End Sub

    Private Sub btnGrootteAanpassen_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnGrootteAanpassen.Click
        'ALS MAIN.VB KLEIN IS => GROTER
        If boolVerkleindVenster = True Then
            Me.Size = New Point(477, 500)
            btnGrootteAanpassen.Text = "<"
            boolVerkleindVenster = False
        Else
            Me.Size = New Point(345, 500)
            btnGrootteAanpassen.Text = ">"
            boolVerkleindVenster = True
        End If
    End Sub

    Private Sub btnDagoverzicht_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnDagoverzicht.Click
        Dagoverzicht.Show()
    End Sub

    Private Sub btnMaandoverzicht_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnMaandoverzicht.Click

    End Sub

    Private Sub btnOpties_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnOpties.Click
        Opties.Show()
    End Sub

    Private Sub Main_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        Me.Hide()
        Splash.Show()
    End Sub
End Class

The timer doesn't execute much of the code in it, if onChange is false, (onChange is false when there is no change detected, that is if the hour doesn't change or if the date doesn't change or if the user doesn't change either)
 
.Net Framework always allocates lots of memory even though your app doesn't use all that currently, it's how the automatic memory management works. Minimize the app and check again (1-2mb now?). I wouldn't consider an 18mb allocation high to start with.
 
Wow! When I minimize it it's like 1mb, now that's what I want :p
Anyway, is there a way to disallow automatic memory allocation?
 
Anyway, is there a way to disallow automatic memory allocation?
No, there isn't. It's very nice, though, giving back resources at will to system if necessary.
 
So let me get this clear...
There is absolutely NO way that I make it so my program only takes like half of the memory it takes now?

Sorry for the nagging :p
 
Your program isn't actually using 18mb of memory, the .Net framework preallocates what it thinks your program *might* need. If the actual memory footprint is seen (well, close enough, it is very difficult to find out how much memory a .Net framework app actually uses) when you minimize your application. The .Net framework will give up all the memory the system needs, that it has preallocated for your application, when it is requested.

In other words, don't worry about what task manager shows your application as using.
 
Back
Top