Heap analyze on IBM i

Any IBM i topic that does not fit in another forum
Post Reply
mat
Posts: 2
Joined: Mon Jun 17, 2024 11:28 am

Heap analyze on IBM i

Post by mat »

Hello,
I have to analyze a memory leak in our software running on IBM i. For that I use the following PEX commands:

Code: Select all

ADDPEXDFN DFN(HEAP) TYPE(*TRACE) JOB((123456/MAT/MAT0 *ALL)) TASK(*ALL) MAXSTG(4000000) TRCTYPE(*SLTEVT) SLTEVT(*YES) MCHINST(*NONE) STGEVT((*ACTGRPHEAP))

STRPEX SSNID(HEAP)

- Do something in the job -

ENDPEX SSNID(HEAP) DTALIB(MATTRC) RPLDTA(*YES)
Then I have some outfiles which can be joined together to get the heap address, allocated size and call stack:

Code: Select all

CREATE VIEW mattrc/heapv1 AS
    (SELECT a.QRECN,
            a.QHPASA,
            a.QHPASZ,
            a.QHPOPR,
            a.QHPRET,
--       c.QTITIMN,
            (SELECT QPRPNM
                FROM mattrc/QAYPEPROCI
                WHERE QHPCK1 = QPRKEY) cs1,
            QIASTMT1,
            (SELECT QPRPNM
                FROM mattrc/QAYPEPROCI
                WHERE QHPCK2 = QPRKEY) cs2,
            QIASTMT2,
            (SELECT QPRPNM
                FROM mattrc/QAYPEPROCI
                WHERE QHPCK3 = QPRKEY) cs3,
            QIASTMT3,
            (SELECT QPRPNM
                FROM mattrc/QAYPEPROCI
                WHERE QHPCK4 = QPRKEY) cs4,
            QIASTMT4,
            (SELECT QPRPNM
                FROM mattrc/QAYPEPROCI
                WHERE QHPCK5 = QPRKEY) cs5,
            QIASTMT5
      FROM mattrc/QAYPEHEAP a
           INNER JOIN mattrc/QAYPETIDX c
             ON a.QRECN = c.QRECN
           LEFT JOIN mattrc/QAYPEIAD d
             ON a.QRECN = d.QRECN);
             
 /* Check mallocs whithout free */
 SELECT *
  FROM mattrc/heapv1
  WHERE QHPOPR = 0
    AND QHPASA NOT IN(SELECT QHPASA FROM mattrc/heapv1
                       WHERE QHPOPR = 1);
My problem is, that the outfile QAYPEHEAP has only 5 fields to save 5 call stack entries. Is there a way to get more entries of the call stack?

How do you analyze memory (heap) leaks on IBM i?
On Windows we use WPA (Windows Performance Analyzer) for that.

Thanks and best regards,
Matthias
mat
Posts: 2
Joined: Mon Jun 17, 2024 11:28 am

Re: Heap analyze on IBM i

Post by mat »

We are using ILE C++. So tools like "IBM i Performance Data Investigator (PDI) charts named IBM Technology for Java Memory Overview" can not be used.
Scott Klement
Site Admin
Posts: 872
Joined: Sun Jul 04, 2021 5:12 am

Re: Heap analyze on IBM i

Post by Scott Klement »

Yeah... the way Java reserves memory is very different from C++. Java runs in a Java Virtual Machine that manages the storage for you and runs garbage collection to clean up unused memory. C++ (or C for that matter) pretty much requires you to manage the memory yourself, and doesn't go through a virtual environment... so expecting Java and C++ to work with the same tool makes little sense, they're radically different.

I'm not an expert at memory management in ILE C++ (I'm a lot more versed in ILE C, but.. still wouldn't consider myself an expert.)

You might consider looking at using the Debug Memory Manager or one of the other options under "Heap Memory Considerations" in the manual to see if that can help you:
https://www.ibm.com/docs/en/i/7.4?topic ... eap-memory

I tend to use Teraspace allocations for most of what I do in ILE C to eliiminate limits, and it also has additional functions for debugging memory usage which may be useful:
https://www.ibm.com/docs/en/i/7.4?topic ... rification

If you decide to use Teraspace, make sure you read and understand the TERASPACE option on CRTCPPMOD and the difference between using the TS functions vs the standard malloc/realloc/free, etc.

Hope that helps... it may not :-)

Of course, another approach might be to use PEX APIs to insert checkpoints at the start/end of your functions to at least let you narrow it down to a function that seems to be leaking. Personally, I'd be more inclined to use the teraspace debug routine for that, though... lots of info... PEX is more designed for debugging performance, its less detailed in memory usage.
Post Reply