English Amiga Board


Go Back   English Amiga Board > Coders > Coders. Language > Coders. C/C++

 
 
Thread Tools
Old 18 December 2022, 20:48   #1
Sim085
Registered User
 
Join Date: Apr 2009
Location: N/A
Posts: 962
Know how much memory is in use by program?

Is there a way how to know how much memory is currently being used by a program at some points during the execution of a program?

For example:
Code:
call_function_1();
print_memory_in_use();
call_function_2();
print_memory_in_use();
call_function_3();
print_memory_in_use();
Sim085 is offline  
Old 18 December 2022, 21:57   #2
Thomas Richter
Registered User
 
Join Date: Jan 2019
Location: Germany
Posts: 3,233
The short answer is no. The problem is really that "being used by a program" is not well-defined, mostly because AmigaOs has no such concept as "memory used by a program". There is only "memory used".

C programs may use the standard C library for memory allocation via malloc(), and potentially, there is a vendor-specific function to find out how large the heap is. Unfortunately, the C language does not specify such a function, but maybe there is a vendor-specific extension that provides that function.

For AmigaOs, there is, as said, no such function since it is not so uncommon that memory allocated by your program is released by some other program, or vice versa, so one cannot really account memory to a particular program.

As an approximation, you may call AvailMem() before your program runs, and then while it runs, and compute the difference. However, that is only an approximation as other programs may have allocated and released memory in between, and memory may have been allocated as side effect of your program operation that may be eventually released, though not by your program.

Consider, for example, your program prints something on the console. This "something" needs to be put somewhere in case the console window is reprinted or resized, and hence needs to be stored. Thus, just "printing" may allocate memory, and this memory is probably only released after the console window is closed, or cleared.
Thomas Richter is offline  
Old 18 December 2022, 22:21   #3
Karlos
Alien Bleed
 
Karlos's Avatar
 
Join Date: Aug 2022
Location: UK
Posts: 4,178
If it's your program, as in you are writing it, you could just declare your own allocation and deallocation wrappers that keeps track of the amount allocated. This only works with memory managed via your wrappers but implemented properly it's not a bad approximation.
Karlos is offline  
Old 18 December 2022, 22:58   #4
Sim085
Registered User
 
Join Date: Apr 2009
Location: N/A
Posts: 962
Quote:
Originally Posted by Thomas Richter View Post
The short answer is no...
Thought so as I could not find much when searching for a solution.


Quote:
Originally Posted by Karlos View Post
If it's your program, as in you are writing it, you could just declare your own allocation and deallocation wrappers that keeps track of the amount allocated. This only works with memory managed via your wrappers but implemented properly it's not a bad approximation.
I think in my current case this would work. I am only using malloc/free and that is the memory I want to keep track of, i.e - to ensure that all memory allocation is getting freed at specific points.

In summary, all looks fine, all is working fine, but trying to make sure there is no memory leak lurking somewhere.

Not sure if anyone has any other ideas?

Last edited by Sim085; 18 December 2022 at 23:23.
Sim085 is offline  
Old 18 December 2022, 23:53   #5
Thomas Richter
Registered User
 
Join Date: Jan 2019
Location: Germany
Posts: 3,233
For memory leak checking, you can try this program:

http://aminet.net/dev/debug/MemLog.lha

Note, however, that due to the above complications, it may not be perfectly accurate. There are some usage instructions in the archive.
Thomas Richter is offline  
Old 19 December 2022, 00:06   #6
Sim085
Registered User
 
Join Date: Apr 2009
Location: N/A
Posts: 962
Quote:
Originally Posted by Thomas Richter View Post
For memory leak checking, you can try this program:

http://aminet.net/dev/debug/MemLog.lha

Note, however, that due to the above complications, it may not be perfectly accurate. There are some usage instructions in the archive.

Thank you for this. Will go through it
Sim085 is offline  
Old 21 December 2022, 18:57   #7
tygre
Returning fan!
 
tygre's Avatar
 
Join Date: Jan 2011
Location: Montréal, QC, Canada
Posts: 1,434
Hi Sim085!

I highly recommend using fortify: it very simple to use and it will detect many memory issues (overwrite, underwrite, multiple frees, etc.) *and* it will tell you exactly how much memory was allocated, even the largest allocations, etc.

Cheers!
tygre is offline  
Old 22 December 2022, 08:51   #8
Sim085
Registered User
 
Join Date: Apr 2009
Location: N/A
Posts: 962
Quote:
Originally Posted by tygre View Post
Hi Sim085!

I highly recommend using fortify: it very simple to use and it will detect many memory issues (overwrite, underwrite, multiple frees, etc.) *and* it will tell you exactly how much memory was allocated, even the largest allocations, etc.

Cheers!
Thank you Will look into this.
Sim085 is offline  
 


Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools

Similar Threads
Thread Thread Starter Forum Replies Last Post
A program that snoops and creates full catalog / cd files from memory ancalimon request.Apps 0 13 August 2020 13:28
Finding start and end of my program's memory? Warty Coders. C/C++ 8 03 January 2019 11:24
Tracking a game/program's memory usage clebin Coders. General 5 01 December 2017 10:46
How to load program into fixed memory area ShK Coders. System 3 07 July 2016 09:46
Best program to test memory? Mustangjeff support.Hardware 5 05 October 2012 07:43

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump


All times are GMT +2. The time now is 08:59.

Top

Powered by vBulletin® Version 3.8.11
Copyright ©2000 - 2024, vBulletin Solutions Inc.
Page generated in 0.08015 seconds with 15 queries