How to detect memory leaks
  • Order of the Butterfly
    Order of the Butterfly
    asrael22
    Posts: 273 from 2014/6/11
    From: Germany
    Ta.

    I've developed a small tool that connects to a server socket someplace and sends some data via HTTP.
    That is done every 30 seconds.
    After a day or so the memory of the system has decreased quite a bit. 1/3 of 512 MB.
    I'm assuming it's my tool that's eating up the memory, although I've tripple checked that each AllocVec, strdup, etc. is freed.
    For each of those HTTP requests there is a sequence of things:
    - memory allocated for request and response
    - socket opened
    - data send over
    - socket closed
    - memory freed

    How could I check where the memory is used?
    Would it be better to allocate some memory upfront for the request/response and also keep the socket open until the application is closed?
    What is the suggested approach for a long-running application?


    Manfred
  • »02.04.17 - 15:09
    Profile
  • Acolyte of the Butterfly
    Acolyte of the Butterfly
    deka
    Posts: 123 from 2013/2/12
    From: Hungary, Kecsk...
    Quote:

    asrael22 wrote:
    Ta.

    I've developed a small tool that connects to a server socket someplace and sends some data via HTTP.
    That is done every 30 seconds.
    After a day or so the memory of the system has decreased quite a bit. 1/3 of 512 MB.
    I'm assuming it's my tool that's eating up the memory, although I've tripple checked that each AllocVec, strdup, etc. is freed.
    For each of those HTTP requests there is a sequence of things:
    - memory allocated for request and response
    - socket opened
    - data send over
    - socket closed
    - memory freed

    How could I check where the memory is used?
    Would it be better to allocate some memory upfront for the request/response and also keep the socket open until the application is closed?
    What is the suggested approach for a long-running application?


    Manfred



    I had similar problem while developing a project. I have written a simple library, which traces the memory allocations/freeings and usage too. Maybe, you could do something similar.
  • »03.04.17 - 09:18
    Profile
  • MorphOS Developer
    jacadcaps
    Posts: 1497 from 2003/3/5
    From: Canada
    Increase the frequency of the 'send network update' 'loop' and see if this really is your app, for starters. Try using AllocPooled rather than AllocMem. Try not using strdup :)
  • »03.04.17 - 11:48
    Profile Visit Website
  • Order of the Butterfly
    Order of the Butterfly
    polluks
    Posts: 311 from 2007/10/23
    From: Gelsenkirchen,...
    Try SDK's Wipeout...
    Pegasos II G4: MorphOS 3.9, Zalman M220W
    Power Mac G3: OSX 10.3 · PowerBook 5,8: OSX 10.5, MorphOS 3.9
  • »03.04.17 - 19:44
    Profile
  • Order of the Butterfly
    Order of the Butterfly
    asrael22
    Posts: 273 from 2014/6/11
    From: Germany
    I've replaced strdup with my own implementation that uses AllocVec as strdup probably uses malloc.
    FreeVec probably doesn't work on malloc allocated memory?

    Since when was this AllocPooled added the AmigaOS SDK?
    Completely new to me.


    Manfred
  • »04.04.17 - 06:56
    Profile
  • MorphOS Developer
    CISC
    Posts: 607 from 2005/8/27
    From: the land with ...
    Quote:

    asrael22 wrote:
    Since when was this AllocPooled added the AmigaOS SDK?
    Completely new to me.


    V39, so 3.0.


    - CISC
  • »04.04.17 - 10:26
    Profile
  • MorphOS Developer
    geit
    Posts: 888 from 2004/9/23
    I usually gate all alloc/free functions through app internal functions, where i track all memory blocks and perform wall boundary checks.

    This way the only way loosing resources is when dealing with other os functions which require a free on their own like Read/FreeArgs() .
  • »04.04.17 - 10:58
    Profile
  • MorphOS Developer
    jacadcaps
    Posts: 1497 from 2003/3/5
    From: Canada
    Quote:

    asrael22 wrote:
    I've replaced strdup with my own implementation that uses AllocVec as strdup probably uses malloc.
    FreeVec probably doesn't work on malloc allocated memory?



    Assuming you're on libnix (-noixemul): strdup -> malloc -> AllocPooled internally into your app's libnix pool.

    You are right that FreeVed does not work on malloc allocated memory - you've got to use free. A mixup like that will also cause a leak, if you are lucky, or a total system crash.
  • »04.04.17 - 15:01
    Profile Visit Website
  • Order of the Butterfly
    Order of the Butterfly
    asrael22
    Posts: 273 from 2014/6/11
    From: Germany
    Now I've added some test code that fakes that part of the normal operation which sends data via socket.
    I can say that there is no memory leaking.

    The data provider is the serial device where data comes in every 30 seconds.
    I'll now try to isolate this operation and see if there is a memory leak.
    This code operates on a static memory buffer of just some bytes, unlikely there is a leak, but who knows.


    Manfred
  • »08.04.17 - 10:02
    Profile
  • Order of the Butterfly
    Order of the Butterfly
    asrael22
    Posts: 273 from 2014/6/11
    From: Germany
    Hmm, weird stuff.
    It turns out, after making a bunch of tests, also with another laptop, that the one which leaked memory leaks by just running MorphOS alone.
    Installed MorphOS 3.9 and while it just runs, without any program started, it's leaking memory so that after 3 or 4 days none is left of the 512MB.

    Could that be a problem of the RAM itself?


    Manfred
  • »12.04.17 - 18:03
    Profile
  • Order of the Butterfly
    Order of the Butterfly
    Tcheko
    Posts: 417 from 2003/2/25
    From: France
    @asrael22 : try running from ISO. I doubt the default MorphOS leaks memory that fast. My own setup is running for weeks long without leaking ram that much if any. There must be something running that leaks ram which is not part of the default OS install.
    Quelque soit le chemin que tu prendras dans la vie, sache que tu auras des ampoules aux pieds.
    -------
    I need to practice my Kung Fu.
  • »12.04.17 - 18:07
    Profile Visit Website
  • Order of the Butterfly
    Order of the Butterfly
    asrael22
    Posts: 273 from 2014/6/11
    From: Germany
    Quote:

    Tcheko wrote:
    @asrael22 : try running from ISO. I doubt the default MorphOS leaks memory that fast. My own setup is running for weeks long without leaking ram that much if any. There must be something running that leaks ram which is not part of the default OS install.



    It was a clean install. Nothing installed additionally.
    I'll try running from DVD.

    My dev system (PowerBook) runs nicely, no leaks at all.
    So I was wondering if it's the hardware, or the RAM in particular.


    Manfred
  • »12.04.17 - 18:48
    Profile
  • Order of the Butterfly
    Order of the Butterfly
    asrael22
    Posts: 273 from 2014/6/11
    From: Germany
    Quote:

    asrael22 wrote:
    Quote:

    Tcheko wrote:
    @asrael22 : try running from ISO. I doubt the default MorphOS leaks memory that fast. My own setup is running for weeks long without leaking ram that much if any. There must be something running that leaks ram which is not part of the default OS install.



    It was a clean install. Nothing installed additionally.
    I'll try running from DVD.



    Same problem when booting from DVD.
    Free memory decreases slowly but constantly.


    Manfred
  • »13.04.17 - 07:55
    Profile
  • MorphOS Developer
    jacadcaps
    Posts: 1497 from 2003/3/5
    From: Canada
    Quote:

    asrael22 wrote:
    Same problem when booting from DVD.
    Free memory decreases slowly but constantly.



    So, you've booted from DVD and then what? Just left the machine alone? Please try to provide as much detail as possible - we need to get to the bottom of this.
  • »13.04.17 - 11:29
    Profile Visit Website
  • Order of the Butterfly
    Order of the Butterfly
    asrael22
    Posts: 273 from 2014/6/11
    From: Germany
    Quote:

    jacadcaps wrote:
    Quote:

    asrael22 wrote:
    Same problem when booting from DVD.
    Free memory decreases slowly but constantly.



    So, you've booted from DVD and then what? Just left the machine alone? Please try to provide as much detail as possible - we need to get to the bottom of this.

    Yeah, exactly.
    Booted from DVD, closed the installer window, and just let it alone.
    After a while the screenblanker gets active and from time to time I hit <space> to deactivate it.

    That's on a iBook G4 14" with 512MB.
    I'm currently trying on another iBook G4 12". It seems to have the same effect. After booting it had 5.3% used memory.
    By now (after 2 hours) it has 6.2% used memory. I'll let it run for a day to see if it's really increasing.


    Manfred
  • »13.04.17 - 12:33
    Profile
  • Order of the Butterfly
    Order of the Butterfly
    asrael22
    Posts: 273 from 2014/6/11
    From: Germany
    Quote:

    asrael22 wrote:
    Quote:

    jacadcaps wrote:
    Quote:

    asrael22 wrote:
    Same problem when booting from DVD.
    Free memory decreases slowly but constantly.



    So, you've booted from DVD and then what? Just left the machine alone? Please try to provide as much detail as possible - we need to get to the bottom of this.

    Yeah, exactly.
    Booted from DVD, closed the installer window, and just let it alone.
    After a while the screenblanker gets active and from time to time I hit <space> to deactivate it.

    That's on a iBook G4 14" with 512MB.
    I'm currently trying on another iBook G4 12". It seems to have the same effect. After booting it had 5.3% used memory.
    By now (after 2 hours) it has 6.2% used memory. I'll let it run for a day to see if it's really increasing.



    After a couple more hours we're at 8.7% memory usage.
  • »13.04.17 - 19:53
    Profile
  • Order of the Butterfly
    Order of the Butterfly
    asrael22
    Posts: 273 from 2014/6/11
    From: Germany
    There seems to be something that eats up memory on those iBooks I have.
    Both, with a default MorphOS 3.9 install, which just runs without interference, seem to leak memory.
    Also it seems the fans are permanently running. Which is not on OS X.

    On my PowerBook all is fine. It is now running for days with constant memory consumtion including my program I had developed.

    That's a bit unfortunate because I don't want to use my PB for the purpose I had iBook prepared for.


    Manfred
  • »14.04.17 - 14:57
    Profile
  • Order of the Butterfly
    Order of the Butterfly
    asrael22
    Posts: 273 from 2014/6/11
    From: Germany
    @jacadcaps:

    Were you guys able to replicate the issues?:
    - possible memory leak on iBook G4
    - constant fan on iBook G4


    Manfred
  • »30.04.17 - 14:17
    Profile