home *** CD-ROM | disk | FTP | other *** search
/ NetNews Usenet Archive 1992 #16 / NN_1992_16.iso / spool / comp / lang / c / 11527 < prev    next >
Encoding:
Text File  |  1992-07-23  |  4.0 KB  |  83 lines

  1. Newsgroups: comp.lang.c
  2. Path: sparky!uunet!uunet.ca!hyper.hyper.com!hurst
  3. From: hurst@hyper.hyper.com (Graham Hurst)
  4. Subject: Re: good memory allocation strategies?
  5. Message-ID: <1992Jul23.171014.9158@hyper.hyper.com>
  6. Keywords: SGI kernel
  7. Reply-To: hurst@hyper.com (Graham Hurst,,)
  8. Organization: HyperCube Inc.
  9. References: <1992Jul21.214115.28569@tamsun.tamu.edu> <1992Jul22.172111.2646@hyper.hyper.com> <boehm.711834865@siria>
  10. Date: Thu, 23 Jul 1992 17:10:14 GMT
  11. Lines: 70
  12.  
  13. In article <boehm.711834865@siria> boehm@parc.xerox.com (Hans Boehm) writes:
  14. >hurst@hyper.hyper.com (Graham Hurst) writes:
  15. >>We recently discovered a "feature" of the default SGI kernel that makes
  16. >>dynamic memory allocation unreliable - it never turns down a request for
  17. >>memory!
  18. >
  19. >>Since IRIX 3.3.2, the default SGI kernel will cause malloc/realloc
  20. >>(via sbrk) to return a non-zero pointer even if the requested memory size is
  21. >>greater than the sum of physical memory and swap space! If your app tries
  22. >>to use enough of this memory, the kernel sends a SIGKILL to kill your app
  23. >>and puts a message on the console about running out of swap space.
  24. >
  25. >This does increase the probability of crashes due to running out of
  26. >swap space.  But the traditional way of checking malloc results is
  27. >already somewhat unreliable under most circumstances.  Consider
  28. >the following scenario:
  29. >
  30. >malloc(enough space to barely fit in swap space);
  31. >call deeply recursive procedure;
  32.  
  33. This example shows that a program using a "reliable" malloc can still
  34. crash (via blowing the stack). It doesn't show that "the traditional way
  35. of checking malloc results is already somewhat unreliable under most
  36. circumstances".
  37.  
  38. I agree that there are circumstances where there is no other choice but
  39. to have the operating system kill your application, particularly if the
  40. swap space is full.  However I still don't think the OS should give you
  41. requested memory (i.e. a successful malloc) that you cannot then use.
  42. I also think this is at odds with the ANSI standard.
  43.  
  44. >On most systems that I know of, your process now gets sent a bus error due
  45. >to the inability of the kernel to grow your process stack sufficiently.
  46. >You can try to avoid this by growing the stack sufficiently in the beginning,
  47. >and catching any bus errors that occur; but if it weren't for the
  48. >particularly poor choice of SIGKILL, you could do the analogous thing
  49. >for malloc.  (AIX gives you a warning signal first, before it sends
  50. >SIGKILL, I believe.  This initially struck me as silly, but I'm begginning
  51. >to believe that it's more useful than any of the other alternatives,
  52. >including the one in which malloc preallocates swap space.)
  53.  
  54. The concept of "preallocation" implies that you may be asking for memory
  55. before you need it or without knowing how much of it you'll need.  This is
  56. not the case for my applications (large scale number crunching for molecular
  57. modelling), where I know exactly how much space I need and will use all
  58. of it - i.e. if I can't be certain that I can use all of it, I want to know
  59. when I call malloc. Again this is how I interpret (via K&R II) the
  60. ANSI C standard library definition of malloc.  (I don't care if the OS
  61. procrastinates about physical allocation, just that I am guaranteed as
  62. much as I have asked for.)
  63.  
  64. I realize not all apps can or should be written this way, and that the
  65. other consumers of swap generally can't, so I think the solution has
  66. to be a richer API for memory allocation, where the programmer decides
  67. whether he wants reliable memory or not.  Microsoft adopted this
  68. approach with NT, and I've advocated this to SGI as well.
  69.  
  70. Of course any function called "malloc" should work the way I expect! ;-)
  71. >
  72. >Note that similar problems occur if there are other crucial processes
  73. >allocating swap space on the same machine.  Copy-on-write pages for
  74. >shared libararies are yet another problem.  Malloc is only one consumer
  75. >of swap space.
  76. >
  77. >Hans-J. Boehm
  78. >(boehm@parc.xerox.com)
  79.  
  80. Graham Hurst
  81. Hypercube Inc, 7-419 Phillip St, Waterloo, Ont, Canada N2L 3X2 (519)725-4040
  82. internet: hurst@hyper.com
  83.