Examining memory usage

Just starting out? Need help? Post your questions and find answers here.
smishra
User
User
Posts: 70
Joined: Tue Jun 06, 2006 3:39 pm
Location: Maryland US

Examining memory usage

Post 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.
User avatar
Keya
Addict
Addict
Posts: 1891
Joined: Thu Jun 04, 2015 7:10 am

Re: Examining memory usage

Post 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"
User avatar
Lunasole
Addict
Addict
Posts: 1091
Joined: Mon Oct 26, 2015 2:55 am
Location: UA
Contact:

Re: Examining memory usage

Post 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".
"W̷i̷s̷h̷i̷n̷g o̷n a s̷t̷a̷r"
smishra
User
User
Posts: 70
Joined: Tue Jun 06, 2006 3:39 pm
Location: Maryland US

Re: Examining memory usage

Post 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!
User avatar
Keya
Addict
Addict
Posts: 1891
Joined: Thu Jun 04, 2015 7:10 am

Re: Examining memory usage

Post 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
smishra
User
User
Posts: 70
Joined: Tue Jun 06, 2006 3:39 pm
Location: Maryland US

Re: Examining memory usage

Post 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!
Post Reply