Results 1 to 9 of 9

Thread: How do I improve (reduce) my app memory and cpu usage

  1. #1

    Thread Starter
    Fanatic Member coolcurrent4u's Avatar
    Join Date
    Apr 2008
    Location
    *****
    Posts
    993

    How do I improve (reduce) my app memory and cpu usage

    Hello guys,

    I am developing an application that manipulates big strings in memory, basically the app filter, sort, extract some strings from plain text files.

    The text files am currently working of is 4MB, but am putting a maximum files size of 10 MB in mind as i work.

    First i have an open feature, so my app loads the text file to a textbox, and when i click on a button, it starts to do its work.

    Observing a compiled version of my app, its cpu usage goes up as high as 99%, and the memory usage jumps high to 100MB.

    My system has 767MB memory 2.4GH processor power.

    In order to make the GUI responsive i put the code in a dll, and before starting the process here is the code i use

    vb Code:
    1. 'create buffer
    2.  sTemp = Space$(Len(txtOut.Text)) ' this line take a minute of more for 3MB text
    3. 'copy from textbox to buffer
    4.  retval = lstrcpy(sTemp, txtOut.Text) ' this take about 15 seconds for the same 3mb text

    where i need your help is to improve this routines so as to take less cpu and memory usage

    Is there a way i can assign max memory my app can use?

    Please any ideas are welcomed
    Programming is all about good logic. Spend more time here


    (Generate pronounceable password) (Generate random number c#) (Filter array with another array)

  2. #2
    PowerPoster dilettante's Avatar
    Join Date
    Feb 2006
    Posts
    24,487

    Re: How do I improve (reduce) my app memory and cpu usage

    Quote Originally Posted by coolcurrent4u View Post
    In order to make the GUI responsive i put the code in a dll, ...
    Since a typical DLL runs in-process and in VB6 on the same thread this isn't going to accomplish what you're hoping for.


    Another thing to consider is that approaches that work with smaller amounts of data don't always scale well to large sets of data. "Sucking it all into RAM" is one of these, though as RAM has gotten plentiful the threshold of "large" has moved quite high.

    Anything you can do to reduce the amount of data you work with early on should help. For example any filtering or extracting you can do before sorting will make your sorting take a lot fewer resources.

    Anything you can do with your algorithms that reduces the number of times you have to "touch" (test, modify, copy, or move) the same data item should help.

    There is also the issue of memory management and "chunk sizes" that can come into the picture. Asking for too many or too large arrays (and a String is essentially an array in most ways that matter) can mean a performance hit. Working with a huge number of tiny chunks of data can be a performance problem too though.


    Those guidelines are so vague as to be almost useless I admit. However without knowing more specifics it is hard to make specific suggestions.

    Some of the factors must be given different amounts of weight depending on the ecosystem your application lives in. "Suck it all into RAM" can fail miserably for server applications, where multiple copies must co-exist in RAM (and with other applications). Such an approach might work fine for systems that are otherwise idle though - like a desktop PC.


    I think you know where some of your trouble spots are. But you might also consider running a profiler against your application to see where more subtle bottlenecks are.

  3. #3

  4. #4

    Thread Starter
    Fanatic Member coolcurrent4u's Avatar
    Join Date
    Apr 2008
    Location
    *****
    Posts
    993

    Re: How do I improve (reduce) my app memory and cpu usage

    Quote Originally Posted by dilettante View Post
    Since a typical DLL runs in-process and in VB6 on the same thread this isn't going to accomplish what you're hoping for.


    Another thing to consider is that approaches that work with smaller amounts of data don't always scale well to large sets of data. "Sucking it all into RAM" is one of these, though as RAM has gotten plentiful the threshold of "large" has moved quite high.

    Anything you can do to reduce the amount of data you work with early on should help. For example any filtering or extracting you can do before sorting will make your sorting take a lot fewer resources.

    Anything you can do with your algorithms that reduces the number of times you have to "touch" (test, modify, copy, or move) the same data item should help.

    There is also the issue of memory management and "chunk sizes" that can come into the picture. Asking for too many or too large arrays (and a String is essentially an array in most ways that matter) can mean a performance hit. Working with a huge number of tiny chunks of data can be a performance problem too though.


    Those guidelines are so vague as to be almost useless I admit. However without knowing more specifics it is hard to make specific suggestions.

    Some of the factors must be given different amounts of weight depending on the ecosystem your application lives in. "Suck it all into RAM" can fail miserably for server applications, where multiple copies must co-exist in RAM (and with other applications). Such an approach might work fine for systems that are otherwise idle though - like a desktop PC.


    I think you know where some of your trouble spots are. But you might also consider running a profiler against your application to see where more subtle bottlenecks are.
    What profiler do you recommend? The Profiler i found does not help
    Last edited by coolcurrent4u; Aug 11th, 2010 at 05:10 AM. Reason: typo
    Programming is all about good logic. Spend more time here


    (Generate pronounceable password) (Generate random number c#) (Filter array with another array)

  5. #5
    PowerPoster dilettante's Avatar
    Join Date
    Feb 2006
    Posts
    24,487

    Re: How do I improve (reduce) my app memory and cpu usage

    I like VB Watch myself, and they have an evaluation version that can handle small projects (10 modules I believe). If you have VB/VS 6.0 Enterprise there is a profiler included too (APE is its name as I recall).

    I'm not sure what other commercial products are still available though.

  6. #6

    Thread Starter
    Fanatic Member coolcurrent4u's Avatar
    Join Date
    Apr 2008
    Location
    *****
    Posts
    993

    Re: How do I improve (reduce) my app memory and cpu usage

    Thank you dilettante,

    I'll look at vbwatch trail. I am using visual basic professional, so it doesn't have APE. An i dont think APE is for testing individual application stress/problems see here.

    any other suggestions would be welcomed
    Programming is all about good logic. Spend more time here


    (Generate pronounceable password) (Generate random number c#) (Filter array with another array)

  7. #7

    Thread Starter
    Fanatic Member coolcurrent4u's Avatar
    Join Date
    Apr 2008
    Location
    *****
    Posts
    993

    Re: How do I improve (reduce) my app memory and cpu usage

    Quote Originally Posted by dilettante View Post
    Since a typical DLL runs in-process and in VB6 on the same thread this isn't going to accomplish what you're hoping for.
    so do i use activex exe instead of dll?

    Quote Originally Posted by dilettante View Post
    Anything you can do to reduce the amount of data you work with early on should help. For example any filtering or extracting you can do before sorting will make your sorting take a lot fewer resources.
    Usually i follow this step when working with strings

    Functions
    Code:
    Extract > sort > remove duplicates > filter > group
    what would be your advice here?

    Quote Originally Posted by dilettante View Post
    Anything you can do with your algorithms that reduces the number of times you have to "touch" (test, modify, copy, or move) the same data item should help.
    do you have better algorithms for the above named functions that would be faster?
    Programming is all about good logic. Spend more time here


    (Generate pronounceable password) (Generate random number c#) (Filter array with another array)

  8. #8
    PowerPoster dilettante's Avatar
    Join Date
    Feb 2006
    Posts
    24,487

    Re: How do I improve (reduce) my app memory and cpu usage

    Allowing Users to Interrupt Tasks is the classic approach to background processing. There is also the DoEvents hack, but of course it has its risks.

    Other people offload background processing to another program, either a console program "connected" via anonymous pipes or an ActiveX EXE rigged for background processing on its own thread.


    As far as data reduction goes, you do what you can. Not having duplicates is best. If you do have them though you may be forced to sort first, and take the performance hit. Prevention is usually cheaper than "cure" when possible.


    "Better" algorithms are entirely situational. An example though might be to used hash-based keyed access for locating items in a large list instead of sequentially searching every time you need an item. Binary searches work well if your lists are ordered appropriately. Linked-lists can be better than moving long chunks of data when adjusting the sequence of a list, etc. Sorting is a performance killer when you don't truly require a fully sorted list. Not that any of that is news.


    It is hard to make specific suggestions without considering specific cases. I suspect that's why others are holding back their comments so far.

  9. #9

    Thread Starter
    Fanatic Member coolcurrent4u's Avatar
    Join Date
    Apr 2008
    Location
    *****
    Posts
    993

    Re: How do I improve (reduce) my app memory and cpu usage

    I'll past the code am using so far, maybe you'll see some bugs. They are all in a class module

    Here is the extract function

    '
    vb Code:
    1. extract Items from text
    2. Public Sub Extract(ByRef Value As String)
    3.     Dim inGroup      As Integer
    4.     If sSeperator = vbNullString Then
    5.         RaiseEvent Error("No Pattern defined", vbObjectError + 500)
    6.         Exit Sub
    7.     End If
    8.     RaiseEvent Status("Extracting", 1)
    9.     'Set the pattern by using the Pattern property.
    10.     objRegExp.Pattern = sPattern
    11.     ' Set Case Insensitivity.
    12.     objRegExp.IgnoreCase = True
    13.     'Set global applicability.
    14.     objRegExp.Global = True
    15.     'Test whether the String can be compared.
    16.     If (objRegExp.Test(Value) = True) Then
    17.         'Get the matches.
    18.         Set colMatches = objRegExp.Execute(Value)
    19.        
    20.         '  Execute search.
    21.         For Each objMatch In colMatches
    22.             Do_Events
    23.             ' Iterate Matches collection.
    24.             Append objMatch.Value & sSeperator
    25.         Next
    26.         lCount = colMatches.Count
    27.     End If
    28.     Set colMatches = Nothing
    29. End Sub

    Here is the append function

    vb Code:
    1. Private Sub Append(s As String)
    2.  
    3.     Dim L      As Long
    4.    
    5.     L = StrLen + Len(s)
    6.    
    7.     While L > Len(Buffer)
    8.         Buffer = Buffer & Space$(BufferSize)
    9.     Wend
    10.    
    11.     CopyMemory ByVal StrPtr(Buffer) + (StrLen * 2), ByVal StrPtr(s), LenB(s)
    12.     StrLen = StrLen + Len(s)
    13.    
    14. End Sub

    please let me know what you find.

    Thank you for you response so far
    Programming is all about good logic. Spend more time here


    (Generate pronounceable password) (Generate random number c#) (Filter array with another array)

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