Path: utzoo!utgpu!news-server.csri.toronto.edu!cs.utexas.edu!swrinde!zaphod.mps.ohio-state.edu!wuarchive!decwrl!pa.dec.com!bacchus!mwm From: mwm@raven.relay.pa.dec.com (Mike (My Watch Has Windows) Meyer) Newsgroups: comp.sys.amiga.programmer Subject: Re: Two questions about memory allocation Message-ID: Date: 9 Jan 91 21:04:59 GMT References: <8135@ucdavis.ucdavis.edu> Sender: news@pa.dec.com (News) Organization: Missionaria Phonibalonica Lines: 51 In-Reply-To: zerkle@iris.ucdavis.edu's message of 9 Jan 91 18:57:11 GMT In article <8135@ucdavis.ucdavis.edu> zerkle@iris.ucdavis.edu (Dan Zerkle) writes: I recently wrote a vector graphics package that had to allocate and de-allocate huge amounts of small structures with malloc. I noticed that it seemed to be running awfully slowly for some reason. When I wrote my own memory manager instead, the program ran three times faster. People running similar stuff on Unix (for the same class) noticed that their programs didn't spend a lot of time on memory management (according to the profiler). So: Why does the Amiga spend so much time fooling around with memory management? Is the behavior I noted typical? The key is noticing that you're using Manx, not Lattice. Manx malloc/free goes to the system (AllocMem/FreeMem), which have to deal with linked lists of all the memory in the system. The Unix malloc/free (and presumably, your code) allocate large chunks of memory, and most malloc/free calls don't go to the exec. The libraries have a simpler problem than the Amiga exec, and so tend to run noticably faster. Also, this same program's memory manager would allocate a large chunk of memory at the beginning of the program (and in the middle, actually), but I never bothered to de-allocate the memory before exiting. I never noticed any problems with this, and the status line showed as much memory before execution as after. That doesn't mean that there wasn't a potential for problems. Does anybody know about this? Was I doing a bad thing? Should I explicitly de-allocate the memory before exiting, or just count on "the system" to take care of it? The system isn't taking care of it; the library is. The exit code from the compiler will make sure any hanging malloc's get dealt with. This is SOP, and you shouldn't worry about it. Some will argue that it's better style to deallocate the memory yourself, but either way is safe. If it matters: A3000 under 2.02, Aztec C 5.0d. Yes, it matter. Lattice does things the way Unix does - by allocating large chunks, and using those. While testing this, I saw as much as an order of magnitude more speed from Lattices's malloc/free than from AllocMem/FreeMem. Manx malloc/free adds a layer around the AllocMem/FreeMem, and so can be expected to run slower than AllocMem/FreeMem. The advantage of Manx's way of doing things is that free()'ed memory is given back to the system, whereas Lattice will hang on to it until the program terminates. This causes problems in some situations.