Page 1 of 1

Examining memory usage

Posted: Sat Mar 18, 2017 12:51 am
by smishra
I have a moderate size program that seems to be taking up more memory than I think it should even at start time.

Is there a tool that allows one to examine the statically allocated memory?

While doing C programming for embedded devices I use the map file generated to figure it out.

Re: Examining memory usage

Posted: Sat Mar 18, 2017 3:51 am
by Keya
I've got no idea if there's any tools for that, but you will generally know which of your variables can get large (its obviously not going to be integer variables, but ones like Arrays or Lists or dynamically allocated memory etc), so it's easy enough to check specific variables, here's a few examples:

Code: Select all

Structure mystruct
  blah1.l
  blah2.q
EndStructure


;### ALLOCATED MEMORY SIZE
*buf = AllocateMemory(512)
Debug "AllocateMemory size = " + Str(MemorySize(*buf)) + " bytes"


;### ARRAY SIZE
Global Dim MyArray.mystruct(10)
Debug "Size of 1 x mystruct = " + Str(SizeOf(mystruct))
Debug "Size of MyArray() = " + Str(ArraySize(MyArray())) + " elements, " +
      Str(ArraySize(MyArray()) * SizeOf(mystruct)) + " bytes"
;note - won't show size of Strings in a structure, as only the size of their pointer (4 or 8 bytes) is included


;### LIST SIZE
Global NewList MyList.mystruct()
For i = 1 To 10: AddElement(MyList()): Next i
pointerbytes = ListSize(MyList()) * (2 * SizeOf(Integer))  ;size of double-linked list pointers
Debug "Size of MyList() = " + Str(ListSize(MyList())) + " elements, " +
      Str((ListSize(MyList()) * SizeOf(mystruct)) + pointerbytes) + " bytes"

Re: Examining memory usage

Posted: Sat Mar 18, 2017 12:56 pm
by Lunasole
smishra wrote: Is there a tool that allows one to examine the statically allocated memory?
Use external tools for that, I'd suggest you "Process Hacker" for Windows. It has abilities even more cool than it's "cool name".

Re: Examining memory usage

Posted: Sat Mar 18, 2017 2:52 pm
by smishra
Thank you for your suggestions.

I figured out the arrays taking up more space than I thought they would take up. I changed allocation sizes for them and saw the difference in memory allocation of the program.

Not an ideal solution but it worked!

Re: Examining memory usage

Posted: Sat Mar 18, 2017 3:17 pm
by Keya
you can use Dim Array(0) initially, and then later use Redim Array(elementsneeded) later on when you know how much to allocate, as opposed to pre-allocating a wasteful amount

Re: Examining memory usage

Posted: Sat Mar 18, 2017 9:51 pm
by smishra
Yes, that is a good suggestion to use dynamic memory allocation through ReDim. It is particularly useful in desktop and server programs

My propensity to use static allocation follows from embedded programming experience

- Once the program starts (or the code fits in the chip for embedded work) I am sure it will run and not fail because of any memory allocation errors.

- The execution of code is more deterministic (important for embedded systems), there is no unknown wait for system memory allocation.

However in this judicious use of ReDim will probably be better. Thanks!