Results 1 to 16 of 16

Thread: Why VB applications Eat lot of memory

  1. #1

    Thread Starter
    Fanatic Member sridharavijay's Avatar
    Join Date
    Sep 2002
    Location
    http://www.vijaysridhara.in
    Posts
    589

    Why VB applications Eat lot of memory

    I made a syntax highlighting editor like Edit plus... with CodeMax 2.1. I was conscious about memory and made it quite efficient nullifying all the ->New<- objects created.

    However when I finally check its memory consumption it takes 11.5 MB in task manager. Whereas Eclipse IDE with somany features takes around 3.5 MB why is this so? Does VB runtime environment takes lot of memory? I've looked at many VB applications and they take lot of memory. Can somebody explain this?

    Thanks
    Vijay S

  2. #2
    So Unbanned DiGiTaIErRoR's Avatar
    Join Date
    Apr 1999
    Location
    /dev/null
    Posts
    4,111
    Using types effectively will greatly reduce memory consumption.

    For example, strings in VB allocate two times the space of the characters. Variants have a large footprint. Using numeric types efficiently also can help both footprint wise and data-storage wise.

    I have a VB program that takes over 50 MBs(phys. & virt.), but it's heavily graphical.

    Plus using high-level implementations consumes more memory. Like a string compared to a byte array.

    Also, lots of declares = lots of memory used. Try to use temp variables where you can: strTmp, lngTmp, intTmp, etc. Because you'll probably not need a lot of variables, if so, I'd suggest arrays(if applicable), as they're quite fast.
    Last edited by DiGiTaIErRoR; May 14th, 2004 at 04:33 AM.

  3. #3

    Thread Starter
    Fanatic Member sridharavijay's Avatar
    Join Date
    Sep 2002
    Location
    http://www.vijaysridhara.in
    Posts
    589
    I was so memory consicous that wherever I'm sure that a number wont go beyond 100, I declared it as byte instead of integer..
    I used APIS wherever possible.. the disk space it takes is 2.6 MB, leaving MSVBVM60.dll. But this explodes to 11.5 MB in Main memory.. Is this inherent in VB or I have to modify my logic in program?

    Thanks any ways...

  4. #4
    Member sahbasita's Avatar
    Join Date
    Feb 2004
    Location
    Loading ...
    Posts
    57

    Wink Knowledge improvement.

    Dear vijay,

    I don't exactly know why that happens, because that also happens to me when i create a big project, it takes lots of system memory. but this is some hints about how to safe it more

    Unless you're doing tasks like generating fractals, your applications are unlikely to be limited by the actual processing speed of your code. Typically other factors — such as video speed, network delays, or disk activities — are the limiting factor in your applications. For example, when a form is slow to load, the cause might be the number of controls and graphics on the form rather than slow code in the Form_Load event. However, you may find points in your program where the speed of your code is the gating factor, especially for routines that are called frequently (it may allocated in memory and will not released). When that's the case, there are several techniques you can use to increase the real speed of your applications:

    1. Avoid using Variant variables.
    2. Use Long integer variables and integer math.
    3. Cache frequently used properties in variables.
    4. Use module-level variables instead of Static variables
    5. Replace procedure calls with inline code.
    6. Use constants whenever possible.
    7. Pass arguments with ByVal instead of ByRef.
    8. Use typed optional arguments.
    9.Take advantage of collections.

    Even if you’re not optimizing your code for speed, it helps to be aware of these techniques and their underlying principles. If you get in the habit of choosing more efficient algorithms as you code, the incremental gains can add up to a noticeable overall improvement in (memory use) speed.
    BlueE@gles

  5. #5

    Thread Starter
    Fanatic Member sridharavijay's Avatar
    Join Date
    Sep 2002
    Location
    http://www.vijaysridhara.in
    Posts
    589
    Thanks Sahbasita... I've taken enough care of the things you mentioned though not all.. However I dont feel Memory and Speed are interlinked unless the consumption of one of these reaches maximum utilization. Viz.. Your speed wont get affected till you reach maximum utilization of memory so that swapping takes place and system slows down.

    Thank you so much! :-)

  6. #6
    Super Moderator Wokawidget's Avatar
    Join Date
    Nov 2001
    Location
    Headingly Occupation: Classified
    Posts
    9,632
    Can u post the code and I'll have a look at it to see if there are any ways to reduce memory.

    VB in general is more memory hungry that other apps written in different languages

    Woka

  7. #7

    Thread Starter
    Fanatic Member sridharavijay's Avatar
    Join Date
    Sep 2002
    Location
    http://www.vijaysridhara.in
    Posts
    589
    I found out where more memory is eaten suddenly. Please help me how to go about.

    I'm using API to ShowOpen and ShowColor dialog boxes, however I'm releasing the memory after the user chooses or cancels them. However whenever the dialogboxes are opened, there is a drastic rise in memory consumption and it stays there even after the user closes the dialog. What must have gone wrong?
    I'm Using copymemory,releasememory also

    Thanks
    Vijay S

  8. #8

  9. #9
    Frenzied Member Jmacp's Avatar
    Join Date
    Jul 2003
    Location
    UK
    Posts
    1,959
    You could always write parts of your code in C++ compile it into a dll and call it from VB.

    Calling dll's from VB is pretty easy. In fact theres even an app that will convert module level VB code into C++ for you.

    I did a few tests, VB vs C++ using Gettickcount to measure the time taken. Well even simple loops written in C++ when called from VB were over twice as fast and i'm sure used less memory than pure VB.

  10. #10

    Thread Starter
    Fanatic Member sridharavijay's Avatar
    Join Date
    Sep 2002
    Location
    http://www.vijaysridhara.in
    Posts
    589
    Ok.. Woka.. Since the code lies at home where I develop and not at my professional work place, I never have code with me when I post. This time I'll get the code and post. Thanks JMacp. Let me try if I can do that also...
    :-)

  11. #11
    Super Moderator Wokawidget's Avatar
    Join Date
    Nov 2001
    Location
    Headingly Occupation: Classified
    Posts
    9,632
    Why do u use API?
    Why not use the Common Dialog Box provided with VB?

    I don't think this is a VB problem, rather a developer not clearing up after API problem, so writting it in C++ will do the same thing no doubt.

    Woka

  12. #12

    Thread Starter
    Fanatic Member sridharavijay's Avatar
    Join Date
    Sep 2002
    Location
    http://www.vijaysridhara.in
    Posts
    589
    I'm using to minimize the weight of setup and the number of Activex to be ported. I'll certainly post a part of the code tomorrow, you can have a look at it. I think I borrowed it from API Guide. Its evening here.

  13. #13

    Thread Starter
    Fanatic Member sridharavijay's Avatar
    Join Date
    Sep 2002
    Location
    http://www.vijaysridhara.in
    Posts
    589
    Woka.... here is the code....for ShowOpen...

    VB Code:
    1. Public Sub mnuFileOpen_Click()
    2.     Dim filebox As OPENFILENAME  ' open file dialog structure
    3.     Dim fname As String          ' filename the user selected
    4.     Dim result As Long           ' result of opening the dialog
    5. On Error GoTo DisplayError
    6.     ' Configure how the dialog box will look
    7.     With filebox
    8.         ' Size of the structure.
    9.         .lStructSize = Len(filebox)
    10.         ' Handle to window opening the dialog.
    11.             .hwndOwner = Me.hwnd
    12.         ' Handle to calling instance (not needed).
    13.         .hInstance = 0
    14.         ' File filters to make available: Text Files and All Files
    15.         .lpstrFilter = "Text Files (*.txt)" & vbNullChar & "*.txt" & vbNullChar & _
    16.             "All Files (*.*)" & vbNullChar & "*.*" & vbNullChar & vbNullChar
    17.         '.lpstrCustomFilter is ignored -- unused string
    18.         .nMaxCustomFilter = 0
    19.         ' Default filter is the first one (Text Files, in this case).
    20.         .nFilterIndex = 2
    21.         ' No default filename.  Also make room for received
    22.         ' path and filename of the user's selection.
    23.         .lpstrFile = Space(256) & vbNullChar
    24.         .nMaxFile = Len(.lpstrFile)
    25.         ' Make room for filename of the user's selection.
    26.         .lpstrFileTitle = Space(256) & vbNullChar
    27.         .nMaxFileTitle = Len(.lpstrFileTitle)
    28.         ' Initial directory is C:\.
    29.         .lpstrInitialDir = tempPathStri & vbNullChar
    30.         ' Title of file dialog.
    31.         .lpstrTitle = "Select a File" & vbNullChar
    32.         ' The path and file must exist; hide the read-only box.
    33.         .flags = OFN_PATHMUSTEXIST Or OFN_FILEMUSTEXIST Or OFN_HIDEREADONLY
    34.         ' The rest of the options aren't needed.
    35.         .nFileOffset = 0
    36.         .nFileExtension = 0
    37.         '.lpstrDefExt is ignored -- unused string
    38.         .lCustData = 0
    39.         .lpfnHook = 0
    40.         '.lpTemplateName is ignored -- unused string
    41.     End With
    42.    
    43.     ' Display the dialog box.
    44.     result = GetOpenFileName(filebox)
    45.     If result <> 0 Then
    46.         ' Remove null space from the file name.
    47.         fname = Left(filebox.lpstrFile, InStr(filebox.lpstrFile, vbNullChar) - 1)
    48.     OpenThisFile fname
    49.    tempPathStri = Mid(fname, 1, InStrRev(fname, "\", , vbTextCompare) - 1)
    50.     End If
    51. Exit Sub
    52. DisplayError:
    53. DisplayError
    54. End Sub

  14. #14
    The picture isn't missing BuggyProgrammer's Avatar
    Join Date
    Oct 2000
    Location
    Vancouver, Canada
    Posts
    5,217
    I believe it loads the dll into your process when you call it. Try placing your open dialog functions into an activex dll so you can unload it. Mgiht be able to release it that way.
    Remember, if someone's post was not helpful, you can always rate their post negatively .

  15. #15

  16. #16

    Thread Starter
    Fanatic Member sridharavijay's Avatar
    Join Date
    Sep 2002
    Location
    http://www.vijaysridhara.in
    Posts
    589
    Oh Is it? Thanks woka for taking pain to run. However only in the EXE the memory consumption is exploding when I execute this code not while development.

    Thanks any ways...

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