|
-
May 14th, 2004, 04:04 AM
#1
Thread Starter
Fanatic Member
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
-
May 14th, 2004, 04:29 AM
#2
So Unbanned
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.
-
May 14th, 2004, 04:38 AM
#3
Thread Starter
Fanatic Member
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...
-
May 14th, 2004, 04:43 AM
#4
Member
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.
-
May 14th, 2004, 04:51 AM
#5
Thread Starter
Fanatic Member
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! :-)
-
May 14th, 2004, 05:27 AM
#6
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
-
May 27th, 2004, 05:28 AM
#7
Thread Starter
Fanatic Member
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
-
May 27th, 2004, 05:39 AM
#8
What's your code.
Post all the code concerned to show and close this dialog box.
Woka
-
May 27th, 2004, 06:05 AM
#9
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.
-
May 27th, 2004, 06:20 AM
#10
Thread Starter
Fanatic Member
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...
:-)
-
May 27th, 2004, 06:32 AM
#11
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
-
May 27th, 2004, 07:20 AM
#12
Thread Starter
Fanatic Member
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.
-
May 27th, 2004, 11:17 PM
#13
Thread Starter
Fanatic Member
Woka.... here is the code....for ShowOpen...
VB Code:
Public Sub mnuFileOpen_Click()
Dim filebox As OPENFILENAME ' open file dialog structure
Dim fname As String ' filename the user selected
Dim result As Long ' result of opening the dialog
On Error GoTo DisplayError
' Configure how the dialog box will look
With filebox
' Size of the structure.
.lStructSize = Len(filebox)
' Handle to window opening the dialog.
.hwndOwner = Me.hwnd
' Handle to calling instance (not needed).
.hInstance = 0
' File filters to make available: Text Files and All Files
.lpstrFilter = "Text Files (*.txt)" & vbNullChar & "*.txt" & vbNullChar & _
"All Files (*.*)" & vbNullChar & "*.*" & vbNullChar & vbNullChar
'.lpstrCustomFilter is ignored -- unused string
.nMaxCustomFilter = 0
' Default filter is the first one (Text Files, in this case).
.nFilterIndex = 2
' No default filename. Also make room for received
' path and filename of the user's selection.
.lpstrFile = Space(256) & vbNullChar
.nMaxFile = Len(.lpstrFile)
' Make room for filename of the user's selection.
.lpstrFileTitle = Space(256) & vbNullChar
.nMaxFileTitle = Len(.lpstrFileTitle)
' Initial directory is C:\.
.lpstrInitialDir = tempPathStri & vbNullChar
' Title of file dialog.
.lpstrTitle = "Select a File" & vbNullChar
' The path and file must exist; hide the read-only box.
.flags = OFN_PATHMUSTEXIST Or OFN_FILEMUSTEXIST Or OFN_HIDEREADONLY
' The rest of the options aren't needed.
.nFileOffset = 0
.nFileExtension = 0
'.lpstrDefExt is ignored -- unused string
.lCustData = 0
.lpfnHook = 0
'.lpTemplateName is ignored -- unused string
End With
' Display the dialog box.
result = GetOpenFileName(filebox)
If result <> 0 Then
' Remove null space from the file name.
fname = Left(filebox.lpstrFile, InStr(filebox.lpstrFile, vbNullChar) - 1)
OpenThisFile fname
tempPathStri = Mid(fname, 1, InStrRev(fname, "\", , vbTextCompare) - 1)
End If
Exit Sub
DisplayError:
DisplayError
End Sub
-
May 27th, 2004, 11:46 PM
#14
The picture isn't missing
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  .
-
May 28th, 2004, 02:32 AM
#15
Works fine on my pc 
WOka
-
May 28th, 2004, 03:08 AM
#16
Thread Starter
Fanatic Member
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
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|