Welcome, Guest. Please login or register.

Author Topic: Lost memory?  (Read 2117 times)

Description:

0 Members and 1 Guest are viewing this topic.

Offline Karlos

  • Sockologist
  • Global Moderator
  • Hero Member
  • *****
  • Join Date: Nov 2002
  • Posts: 16879
  • Country: gb
  • Thanked: 5 times
    • Show all replies
Re: Lost memory?
« on: June 17, 2009, 10:34:50 PM »
I take it "avail flush" doesn't work?

Memory isn't lost per se, but it can be leaked. A buggy bit of code may allocate some memory and then forget to release it. All current versions of the standard C/C++ library track all allocations through  the malloc() / new and will release it on exit. However, if an application is using exec AllocMem() directly and doesn't free it, it can be "lost" from the system after application exit
int p; // A
 

Offline Karlos

  • Sockologist
  • Global Moderator
  • Hero Member
  • *****
  • Join Date: Nov 2002
  • Posts: 16879
  • Country: gb
  • Thanked: 5 times
    • Show all replies
Re: Lost memory?
« Reply #1 on: June 18, 2009, 02:20:47 PM »
Quote from: Amiduffer;511935
I think I tried Avail Flush in shell, and just got an error.


That's definitely not supposed to happen. Was it a "Recoverable Alert" error?

If so, it's highly likely the free memory list has become corrupted. This can be down to a bad bit of code trying to free the same block of memory twice.
int p; // A
 

Offline Karlos

  • Sockologist
  • Global Moderator
  • Hero Member
  • *****
  • Join Date: Nov 2002
  • Posts: 16879
  • Country: gb
  • Thanked: 5 times
    • Show all replies
Re: Lost memory?
« Reply #2 on: June 18, 2009, 08:20:59 PM »
Quote from: Amiduffer;512118
That doesn't sound very nice. Is that all that is required? Just typing AVAIL FLUSH in a shell? I better try again just to be sure.

It's relatively easy to make sure memory gets freed when a program quits, even if the program was badly written and forgot to free it. If the program only uses the C standard malloc()/calloc()/realloc()/free() family of functions (or C++ new/new[]/delete/delete[]), since the standard library can keep track of all allocations. If the application uses AmigaOS memory pools, it is also possible to ensure memory is freed. Only direct calls to AllocMem() that are never freed are likely to cause genuine leaks on exit. This is compounded by the fact that under AmigaOS, you can free bits of an allocated block of memory. That's not easy to track.

Freeing the same memory twice isn't something that is easy to protect against for any system. My own code uses a tracking system that knows all the blocks that were allocated by it and will throw an exception when any code tries to free something not in that list. However, this doesn't come for free, you have to step through the list of all allocated blocks and check the pointer doesn't match any of them. I'm pretty sure the C standard library is capable of doing this (and in current versions of glibc I think it does), but the standard makes it clear that freeing memory at the same address twice is undefined and bad.

If you free memory using AmigaOS FreeMem() and give it an area that wasn't allocated with AllocMem() in the first place, you basically trash exec's free memory list. That tends to make the system unstable.

It isn't the Avail command that is to blame, it's whatever left the free memory list in a state.
« Last Edit: June 18, 2009, 08:24:42 PM by Karlos »
int p; // A