|
I agree that it's basically useless, and a well-designed program isn't likely to be able to free up its own memory at such a time anyway. That is, there shouldn't be anything unimportant just lying around that can be freed that hasn't already been.
So unless you want to start tossing the user's information, about all you can do is warn them that you're running low, and tell them they better start closing tabs (or something).
“Time and space can be a bitch.”
–Gushie, Quantum Leap
{o,o}.oO( Check out my blog! )
|)””’) piHole.org
-”-”-
|
|
|
|
|
Really depends what I'm programming in. In C++, I'd typically check all the time. In higher-level languages, C#, Java, etc. I'd rely more on the runtime to catch such conditions.
|
|
|
|
|
Also depends on platform, on desktop i also use more catches, 'cause there is enough memory, but for ex. windows mobile you don't have so much resources to spare (on older phones mostly) and there is really much bigger chance to run out of memory.
|
|
|
|
|
Sometimes out of memory errors come as side-effects also. Examples are:
1) System.StackOverFlowException (because of infinite recursion)
2) Always True Condition in a while (true) block
3) Image API when loaded with a non-image also throws this exception. The third exception, I feel, however, is a misnomer. I have written a note on this point over here sometime back: http://www.dotnetspider.com/resources/3020-Quick-Effective-Tips-Image-API-Input-Validat.aspx[^]
Please share if you have similar observations.
P.S.: The examples I have given might be based on Managed Code (C#/.NET Framework 2.0)
Vasudevan Deepak Kumar
Personal Homepage Tech Gossips
All the world's a stage,
And all the men and women merely players.
They have their exits and their entrances;
And one man in his time plays many parts... --William Shakespeare
|
|
|
|
|
When I am doing 3D medical imaging applications (where a case can be over 2GB) that must not crash for any reason, the application must handle low memory conditions before the entire address space is exhausted. For me this is usually the application deciding on what needs to be in memory now and what can be reloaded at future time. In one application I made heavy usage of memory mapped files (that can be unmapped and remapped) to solve an address space problem. If I could use a 64 bit operating system (medical display driver only 32 bit) I would not have had to manage the address space so closely though.
John
modified on Monday, September 29, 2008 2:20 PM
|
|
|
|
|
Sure, you've detected the condition, now what? You usually can't report it in any way to the user, and even if you could, what are they going to do about it anyway?
In today's age of 2+GB virtual address spaces, and page files that can conveniently be >2GB, running out of virtual memory is nearly always an indication of a more fundamental problem -- a memory leak. Since you're already leaking memory until you leak it all, by definition, you have no hope. The best you could possibly do is to report to the user "I'm broke. Please get the developers to fix me". A crash says exactly the same thing to the users and is easier to code
Of course, if the user asked you to do that, then you'd probably be better off checking to see if they have enough memory before trying instead of letting the allocation fail as your way to find out. And that's not really the spirit of the question.
patbob
|
|
|
|
|
patbob wrote: A crash says exactly the same thing to the users and is easier to code
Grim
MCDBA, MCSD, MCP+SB
SELECT * FROM users WHERE clue IS NOT NULL
(0 row(s) affected)
|
|
|
|
|
i.e. when I don't want an InsufficientMemoryException or IndexOutOfRangeException to bubble up to the end user or caller of my code.
In virtually all cases, I do want the exception to make it to the top, although for GUI apps I wrap the exception in a user-friendly application exception.
/ravi
|
|
|
|
|
It is rather unlikely that one could consume all the memory in a modern PC with anything but a gross programming error* (or a user who's too cheap to upgrade their 128 Kb system).
HOWEVER:
One should check for memory leaks - if not for protection of the system's integrity (including silly stuff like other applications that may be running and want some memory know and then), then for old time's sake.
(VB Programmers need not apply)
* E.g.: MicroSoft Windows Vista
"The difference between genius and stupidity is that genius has its limits." - Albert Einstein
"How do you find out if you're unwanted if everyone you try to ask tells you to go away?" - Balboos HaGadol
|
|
|
|
|
You as programmer should well know, that Vista have far more sophisticated memory management routines then any other version of Windows, except Server 2008. So, this overexcited notes about how "Vista sucks in any way possible" is really annoying for everyone who have straight hands and a little real experience of using it. The one and only concern, that really apply to it, is the memory requirements of Vista, but, really, it's a little bit overrated. And anyway, don't like the product, don't use it. Want better? Make it!
|
|
|
|
|
13xforever wrote: annoying for everyone who have straight hands
Just my luck . . . nasty arthritis has left my index fingers both bent at ca. 20 degree angles and damaged some of the others, as well. No wonder Vista doesn't seem to do it for me. Hopefully, the next version of windows will have some help/enhancements for those of us who are finger-pointing-impaired.
.
"The difference between genius and stupidity is that genius has its limits." - Albert Einstein
"How do you find out if you're unwanted if everyone you try to ask tells you to go away?" - Balboos HaGadol
|
|
|
|
|
"It is rather unlikely that one could consume all the memory in a modern PC with anything but a gross programming error"
Memory doesn't just fail to allocate when virtual memory runs out, ask for enough memory in one chunk and you won't get it. Recently I had to deal with a situation where some<complete as="" desired=""> was trying to load a 200e6 sample waveform into an arbitrary waveform generator (50MHz - 1Hz signal at 200MHz) ARB's API needed a pointer to that number of doubles (i.e. ~1.5G) even with 3G memory couldn't get that in a contiguous chunk.
|
|
|
|
|
And, to add more to your answer - other responses from the (more common?) game programmers pointed out the need of a lot of video ram - a venue to which I've never ventured. I'll omit questionably relevant ancecdotes.
I do have one question: if you require such a large block of contigious memory - do you find yourself in trouble often with that application? Once upon a time, in my Fortran/Scientific programming days, when one allocated a data array, it was allocated in the executable image (not dynamically at run-time). Wasteful, for sure, but if the memory wasn't there, the application wouldn't load.
"The difference between genius and stupidity is that genius has its limits." - Albert Einstein
"How do you find out if you're unwanted if everyone you try to ask tells you to go away?" - Balboos HaGadol
|
|
|
|
|
That frequency selection was not the typical use. 1kHz resolution or higher is more usual(i.e. 1.5M or less), so allocating half my memory for a situation that occurs less than 1% of the time seemed like a bad choice. Have been trying to get "system" (read hardware) engineers to involve software engineers looking at APIs as part of the off the shelf device selection. Since the device has the memory to load such a wave it would be nice if the API had allowed me to feed it samples rather than having two copies of the data (one in the ARB, one for the API)
|
|
|
|
|
The majority of my applications aren't likely to accumulate sufficient memory to trigger an allocation failure. They don't construct large data structures or require large temporary objects.
That said, I do have one that could. It's a TCP/IP logging facility that can buffer log data over long periods. Therefore, it is designed to predict and prevent allocation failures.
Software Zen: delete this;
|
|
|
|
|
Gary Wheeler wrote: Only when necessary
Exactly. Or at least "Only when appropriate".
/ravi
|
|
|
|
|
Ravi Bhavnani wrote: Or
Shouldn't it be AND ?
Vasudevan Deepak Kumar
Personal Homepage Tech Gossips
All the world's a stage,
And all the men and women merely players.
They have their exits and their entrances;
And one man in his time plays many parts... --William Shakespeare
|
|
|
|
|
No, that would be incorrect grammar.
/ravi
|
|
|
|
|
Ravi Bhavnani wrote: would be incorrect grammar
I actually intended the AND/OR logical operator.
Vasudevan Deepak Kumar
Personal Homepage Tech Gossips
All the world's a stage,
And all the men and women merely players.
They have their exits and their entrances;
And one man in his time plays many parts... --William Shakespeare
|
|
|
|
|
By default, new throws an exception, so just put a try catch in the main function if you want to handle out of memory.
That is assuming you're doing embedded development.
Doing out of memory checking is fairly pointless for desktop apps, because of virtual memory.
|
|
|
|
|
I always implicitly test for (and handle) out-of-memory conditions, by getting __new_handler to throw a real c++ exception. The question was if I explicitly test for out of memory conditions, so the answer has to be 'no'.
In the few cases when I don't use new (e.g. in the boost pool allocator, or SysAllocString) I check for out-of-memory explicitly, so it never goes unhandled, which I think would have made a better question.
This is in a server app, where the client has been known to make requests so big they exceed the 2gb virtual memory space limit, so it is essential that we handle out-of-memory conditions properly.
|
|
|
|
|
I've worked on machines that nearly ran out of hard drive space thanks to virtual memory. I play it safe and test everything.
|
|
|
|
|
new only fails after it runs out of virtual memory.
|
|
|
|
|
"Doing out of memory checking is fairly pointless for desktop apps, because of virtual memory."
Well, well. There are no 2 systems which are the same. One can't expect an unlimited amount of memory, or an unlimited amount of virtual memory. And there are - many - types of apps where memory availability is a real issue, at least from where I'm standing (image and video processing, large scale, large resolution, large databases, and so on). My rule is always to expect the worst.
|
|
|
|
|
Yeah, now that I think about it I'd say it's worth doing, considering it's only 4 extra lines of code. Having said that those large scale apps you're talking about are pretty rare. You're average run of the mill app will never run out of memory.
|
|
|
|