Domanda

Che cosa fate quando ritorna malloc 0 o nuova tiri eccezione? Basta arrestare o cercare di sopravvivere condizione OOM / Salva il lavoro dell'utente?

È stato utile?

Soluzione

Eviterei l'OOM come evitare un incidente.

evitare di fare enorme pezzo di lavoro (e allocare enorme pezzo di memoria) in una sola volta. Mantenere i dati sul disco, la fiducia la cache del disco sistema operativo e fare uso di memory-mapped IO, per quanto possibile, e operare solo su una piccola parte di dati alla volta. Se grandi quantità di dati devono essere on-line (servito con bassa latenza) poi tenerli in memoria su più macchine, come tutte le grandi aziende dei motori di ricerca fanno. O acquistare uno SSD.

Altri suggerimenti

Most people answering this question have probably never worked on embedded systems, where malloc returning 0 is a very real possibility. On a system I'm currently working on, there is a total of 4.25K bytes of RAM (that's 4352 bytes). I'm allocating 64 bytes for the stack, and currently have a 1600 byte heap. Just yesterday I was debugging a heap walk routine so I can follow the allocation and freeing of memory. The heap walk uses a small (30 byte) statically allocated buffer to output to a serial port. It will be turned off for the release version.

Since this is a consumer product, it better not run out of memory once the product has been released. I'm sure it will during development. In any case, about all I can do is beep the speaker a couple of times, and force a reboot.

To be fairly honest, in all projects I've done (keep in mind I'm not working anywhere yet), I've never considered that it could happen, and thus I suppose my programs would die a very fast death.

Besides, handling an OOM requires you to have preallocated the resources to display the error message or to save everything, which can be kind of inconvenient.

I feel that these days, memory costing less than peanuts, it's not something that should happen frequently. On the dawn of protected memory and before, maybe that was a concern, but now? The only OOM errors I've ever seen were from bugged code.

Checking malloc return codes is usuall pointless anyway.

Modern operating systems overcommit memory: They give processes more memory than is actually available. The memory your process is granted is virtual, all mapped to a single zeroed-out page.

It's not until you write to the memory that a physical, unique, page is allocated for your processes. If this allocation fails the kernel will terminate a process (perhaps yours!) in an attempt to find memory. At that point there's nothing you can do any more.

Unless you're developing for embedded systems, real-time systems, or systems that are so critical that failures can cost lives, or billions of dollars... Then it's probably not financially worth it to worry about out of memory conditions.

In most cases, there is little that can be done when you are out out of memory anyways, since there is no memory to create any new objects or perform any tasks that may do something. You have to weigh the cost of the app handling OOM versus the benefit you get from doing it.

I would always check for error. If something returns an error condition, then it has to be handled by your program. Even if it is a message that says "Out of memory, gotta go!", it is better than "Access violation", "core dumped", or whatever. One is an error condition you handle, the other is a bug. And the user will perceive it as such as well.

For your specific case, you could try to roll back the operation, freeing resources that you've allocated until reaching the point of failure, reporting the error, and continuing execution (maybe when you are trying to quit the application, you can give the option to exit immediately). This way, the user can decide what to do, or try to free up some memory by fiddling around, closing files, etc. Of course, how you can handle the situation is highly dependent on your program - a program that is not supposed to be interactive probably just needs to log the error and either quit, or continue.

Autorizzato sotto: CC-BY-SA insieme a attribuzione
scroll top