geoschem / HEMCO

The Harmonized Emissions Component (HEMCO), developed by the GEOS-Chem Support Team.
https://hemco.readthedocs.io
Other
16 stars 32 forks source link

[WIP] Fix remaining memory leaks in HEMCO #255

Open yantosca opened 9 months ago

yantosca commented 9 months ago

Name and Institution (Required)

Name: Bob Yantosca Institution: Harvard + GCST

Confirm you have reviewed the following documentation

Describe the update

This is a placeholder PR and should not yet be merged. I have made the base branch main but will change it later.

This is a companion PR to geoschem/geos-chem#2102. Configuring GEOS-Chem with -DSANITIZE=y has revealed the following memory leaks in GEOS-Chem and HEMCO:

=================================================================
==1495473==ERROR: LeakSanitizer: detected memory leaks

Direct leak of 107464 byte(s) in 133 object(s) allocated from:
    #0 0x14815712993f in __interceptor_malloc ../../.././libsanitizer/asan/asan_malloc_linux.cpp:145
    #1 0x1bd566d in __hco_filedata_mod_MOD_filedata_init /n/holyscratch01/jacob_lab/ryantosca/tests/cloudj/test_memory/CodeDir/src/HEMCO/src/Core/hco_filedata_mod.F90:174
    #2 0x202020202020201f  (<unknown module>)

Direct leak of 29248 byte(s) in 8 object(s) allocated from:
    #0 0x14815712993f in __interceptor_malloc ../../.././libsanitizer/asan/asan_malloc_linux.cpp:145
    #1 0x10c438e in __histcontainer_mod_MOD_histcontainer_create /n/holyscratch01/jacob_lab/ryantosca/tests/cloudj/test_memory/CodeDir/src/GEOS-Chem/History/histcontainer_mod.F90:319
    #2 0x202020202020201f  (<unknown module>)

Direct leak of 3720 byte(s) in 1 object(s) allocated from:
    #0 0x14815712993f in __interceptor_malloc ../../.././libsanitizer/asan/asan_malloc_linux.cpp:145
    #1 0x18f6ae1 in __state_chm_mod_MOD_init_state_chm /n/holyscratch01/jacob_lab/ryantosca/tests/cloudj/test_memory/CodeDir/src/GEOS-Chem/Headers/state_chm_mod.F90:803

Direct leak of 3160 byte(s) in 1 object(s) allocated from:
    #0 0x14815712993f in __interceptor_malloc ../../.././libsanitizer/asan/asan_malloc_linux.cpp:145
    #1 0x18fe162 in __state_chm_mod_MOD_init_state_chm /n/holyscratch01/jacob_lab/ryantosca/tests/cloudj/test_memory/CodeDir/src/GEOS-Chem/Headers/state_chm_mod.F90:1027

SUMMARY: AddressSanitizer: 143592 byte(s) leaked in 143 allocation(s).

Leak 1 is in HEMCO. We are still investigating.

The other leaks are being addressed in https://github.com/geoschem/geos-chem/pull/2104.

Tagging @msulprizio @lizziel

Expected changes

This will be a zero-diff update that will remove memory leaks. It will not change results.

Reference(s)

N/A

Related Github Issue(s)

yantosca commented 8 months ago

@msulprizio @lizziel @christophkeller: I now know where the memory leak is being incurred. In HEMCO/src/Core/hco_config_mod.F90 there is a FileData object (Dta) that is a local variable that gets allocated, and pointed to by the linked list.

https://github.com/geoschem/HEMCO/blob/ca9b187aabd0b862aeeb094b7b56b214efc966e1/src/Core/hco_config_mod.F90#L945-L1050

The Dta object is also needed further down in the loop for those entries in HEMCO_Config.rc that use - as the file name (i.e. using the file that is immediately above it).

I tried to replace Dta with Lct%Dct%Dta, which would get rid of the memory leak, but then the Dta object would not be present for those entries that use -.

I just wanted to note this. It might be going deep into the rabbit hole to try to undo this memory leak, so I will probably put this off for the time being. If any of you have suggestions then let me know. Thanks!

stale[bot] commented 6 months ago

This issue has been automatically marked as stale because it has not had recent activity. If there are no updates within 7 days it will be closed. You can add the "never stale" tag to prevent the Stale bot from closing this issue.