CFD Online Logo CFD Online URL
www.cfd-online.com
[Sponsors]
Home > Forums > Software User Forums > OpenFOAM > OpenFOAM Programming & Development

Memory leak In my code

Register Blogs Members List Search Today's Posts Mark Forums Read

Like Tree2Likes
  • 2 Post By frobaux

Reply
 
LinkBack Thread Tools Search this Thread Display Modes
Old   March 19, 2020, 03:28
Default Memory leak In my code
  #1
Member
 
Fabien Robaux
Join Date: Oct 2016
Posts: 51
Rep Power: 9
frobaux is on a distinguished road
Hey everyone, I written a pretty heavy code, which runs. The problem is that it accumulate memory usage with time and I have no clue to where it can be. Is there a debug option or something that I can use to check if something is getting constructed and not destructed.



I'm using a lot of(almost exculsively) autoPtr = new ... to store datas, maybe it is the origin. They are meant to be dynamically modified (ownership transferred with = or redefined completely with an other = new... )



Basically I have a mesh of 61777 cells, with all tests passing (except 4 faces that are severy non-orthogonal). And at the beggining, I use something like 0.72Gb of RAM. But after 40 000time step I'm at like 8Gb.



Thank you a lot for your help
Fabien
frobaux is offline   Reply With Quote

Old   March 20, 2020, 07:53
Default
  #2
Senior Member
 
Yogesh Bapat
Join Date: Oct 2010
Posts: 102
Rep Power: 15
ybapat is on a distinguished road
You can use tools like valgrind to detect memory leaks.
ybapat is offline   Reply With Quote

Old   March 20, 2020, 09:29
Default
  #3
Member
 
Fabien Robaux
Join Date: Oct 2016
Posts: 51
Rep Power: 9
frobaux is on a distinguished road
Thank you for your answer. That's what I've done (with debugging mode compilation)
The problem is that I do not understand the results to be honnest.

I tried both with the memcheck tool and massif tool.



In my code it seems that stuffs are allocated, and never removed. I have a lot of trouble finding the culprit(s). I would use massif for that kind of problem, but I have a nice graph but cannot identify when in my code this happens.
frobaux is offline   Reply With Quote

Old   March 23, 2020, 04:48
Default
  #4
Senior Member
 
Gerry Kan's Avatar
 
Gerry Kan
Join Date: May 2016
Posts: 347
Rep Power: 10
Gerry Kan is on a distinguished road
Dear Fabien:

Just a very naive question ... did you also call the delete function for each of the dynamically allocated memory for your variables once you don't need them anymore?

Gerry.

Last edited by Gerry Kan; March 23, 2020 at 06:28.
Gerry Kan is offline   Reply With Quote

Old   March 24, 2020, 00:37
Default
  #5
Senior Member
 
Yogesh Bapat
Join Date: Oct 2010
Posts: 102
Rep Power: 15
ybapat is on a distinguished road
I have used the tool some time back. I think you should find some document online to explain output. Also if you are using autoPtr then the are meant for memory handling and call delete in destructor.
ybapat is offline   Reply With Quote

Old   March 24, 2020, 07:39
Default
  #6
Member
 
Fabien Robaux
Join Date: Oct 2016
Posts: 51
Rep Power: 9
frobaux is on a distinguished road
Yes, this it the main reason why I use autoPtr.

Well, I think I found "the" (probably "a") problem:
I need to frequently use interpolation on a tmp<volScalarField>. I do It with

Code:
 autoPtr<interpolation<Type>> interp
                (
                    interpolation<Type>::New("cellPoint", potField)
                );
Where potField is a volScalarField. Which in turns call
Code:
template<class Type>
Foam::interpolationCellPoint<Type>::interpolationCellPoint
(
    const GeometricField<Type, fvPatchField, volMesh>& psi
)
:
    interpolation<Type>(psi),
    psip_
    (
        volPointInterpolation::New(psi.mesh()).interpolate
        (
            psi,
            "volPointInterpolate(" + psi.name() + ')',
            true       
        )
    )
    
{}
During this process, the registery (with debug activated) tells me that:
Code:
  

Constructing IOobject called volPointInterpolation of type IOobject
objectRegistry::checkIn(regIOobject&) : GlobMesh : checking in volPointInterpolation of type regIOobject
And then that
Code:
Constructing IOobject called volPointInterpolate(potField) of type IOobject
objectRegistry::checkIn(regIOobject&) : GlobMesh : checking in volPointInterpolate(potField) of type regIOobject
The problem is that both of them are NEVER checkOut (I've written small scripts that compares for each variable how many time they were checkIn and checkOut)
I think this is were the memory fills! Each time this intep is called, It seems that I add and never delete the member psip_ (a pointField) of interpolationCellPoint.H

I tried to add

Code:
interp.clear();
But nothing changes...

Any advice on this??
frobaux is offline   Reply With Quote

Old   March 24, 2020, 10:37
Default
  #7
Senior Member
 
Gerry Kan's Avatar
 
Gerry Kan
Join Date: May 2016
Posts: 347
Rep Power: 10
Gerry Kan is on a distinguished road
Fabien:

You are right with the autoPtr. I am still stuck in old school, preferring to taking care of memory allocation by myself!

Code:
autoPtr<interpolation<Type>> interp
                (
                    interpolation<Type>::New("cellPoint", potField)
                );
and

Code:
        volPointInterpolation::New(psi.mesh()).interpolate
        (
            psi,
            "volPointInterpolate(" + psi.name() + ')',
            true       
        )
Without going through the code in detail, I am not sure if the destructor in interp auto pointer also calls the corresponding destructor of the new interpolation object that is used to initialize your object, and by extension the new volPointInterpolation. My hunch is it doesn't. So even if you handle the memory management yourself in the interpolation (instead of the autoPtr that you are currently having), the New'd objects will still be in memory.

Would it be possible to create the tmp<volScalarField> in question before hand, and use it as argument and then delete() them by yourself.

Gerry.
Gerry Kan is offline   Reply With Quote

Old   March 24, 2020, 11:23
Default
  #8
Member
 
Fabien Robaux
Join Date: Oct 2016
Posts: 51
Rep Power: 9
frobaux is on a distinguished road
Well, I didn't found a clean solution. that really erases the members of interp.
I had confirmation when I tried to print what is contained in the objectRegistry, that a lot of volScalarFields accumulate inside in (everyone named volPointInterpolate(blabla)) where blabla reprensents the operation done to compute "potField".


The (not clean workaround) is to use the same name, because "cache" is used (see bove). That way, the volPointInterpolate is cleared when renewed...


If someone wants, here is the small script to execute on your log to get everything written in the object registries.

Code:
CheckInAndOut.sh:
#!/bin/bash

### Compare variables that had been checked in and out from openFoam
### usage: ./CheckInAndOut.sh log.interFoam
### Care that you need to have regIOObject 1; as a debugSwitch enabled.
function esc_var()
{
    #escape the characters in  a given variable to be able to grep with this variable 
    a=$(printf %q $@)
    echo ${a//\+/\\+}
}


# list every val that are checkedIn. the name of the var is the seventh. last awk is to register variables only once
listVal=$(cat $1 | grep  "checkIn(reg" | awk  '{printf  $7"\n"}' | awk '!a[$0]++')


for var in $listVal; do

    i=$(esc_var $var)

    # how many time this variable is registered
    nIn=$(cat $1 | grep -E "checking in $i " | wc -l)

    # how many time this variable is checked out 
    nOut=$(cat $1 | grep -E  "checking out $i$" | wc -l)

    # compare, if it does not match, print what was found:
    if [ "$nIn" != "$nOut" ]; then
        echo 
        echo
        echo "variable name: $var"
        echo $i
        echo "nIn $nIn nOut $nOut diff:"$(calc $nIn-$nOut)
        cat $1 | grep --line-number -E -e "checking in $i " -e " checking out $i$" 
    fi
done
makaveli_lcf and Gerry Kan like this.
frobaux is offline   Reply With Quote

Old   March 26, 2020, 06:32
Default
  #9
Member
 
Fabien Robaux
Join Date: Oct 2016
Posts: 51
Rep Power: 9
frobaux is on a distinguished road
Quote:
Originally Posted by Gerry Kan View Post
Fabien:

You are right with the autoPtr. I am still stuck in old school, preferring to taking care of memory allocation by myself!

Code:
autoPtr<interpolation<Type>> interp
                (
                    interpolation<Type>::New("cellPoint", potField)
                );
and

Code:
        volPointInterpolation::New(psi.mesh()).interpolate
        (
            psi,
            "volPointInterpolate(" + psi.name() + ')',
            true       
        )
Without going through the code in detail, I am not sure if the destructor in interp auto pointer also calls the corresponding destructor of the new interpolation object that is used to initialize your object, and by extension the new volPointInterpolation. My hunch is it doesn't. So even if you handle the memory management yourself in the interpolation (instead of the autoPtr that you are currently having), the New'd objects will still be in memory.

Would it be possible to create the tmp<volScalarField> in question before hand, and use it as argument and then delete() them by yourself.

Gerry.

Well, this is what I tought, to be honest, I have tought about this method.
Creating a "localCellPoint" method that does not store it into "cache". Or takes as an entry the volPointInterpolate field.

But the problem of this is that in that case I could not replace "cellPoint" by another preexisting method of interpolation. As I said in my previous post (I didn't see your answer at that time), I ve managed to find a way to avoid the problem, even if I know it is not really clean nor elegant.
frobaux is offline   Reply With Quote

Reply

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

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
Trackbacks are Off
Pingbacks are On
Refbacks are On


Similar Threads
Thread Thread Starter Forum Replies Last Post
Error code 2 memory exceed no *. res files created Alejandro Garcia CFX 2 February 3, 2016 03:11
Memory leak in OpenFOAM? marupio OpenFOAM Bugs 8 October 14, 2010 13:49
How to optimize the memory usage when using FEM vasilis Main CFD Forum 11 August 25, 2009 00:57
Design Integration with CFD? John C. Chien Main CFD Forum 19 May 17, 2001 16:56
What is the Better Way to Do CFD? John C. Chien Main CFD Forum 54 April 23, 2001 09:10


All times are GMT -4. The time now is 17:59.