|
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.
|
|
|
|
|
I am programing the 3D graphics applications which operate with large amount of data. So memory limit can be hit pretty fast.
|
|
|
|
|
Computer graphics always hunger for more and faster processors and yet more memory. There is always a higher resolution, a greater color depth, a need for a larger 3D buffer for a more detailed object and a good use for more and more 3D objects. This will never change.
A while ago he asked me what he should have printed on my business cards. I said 'Wizard'.
I read books which nobody else understand. Then I do something which nobody understands. After that the computer does something which nobody understands. When asked, I say things about the results which nobody understand. But everybody expects miracles from me on a regular basis. Looks to me like the classical definition of a wizard.
|
|
|
|
|
he he, neat
|
|
|
|
|
The consequences are not the same if you program a game or an securty system as a ABS or ESP !
|
|
|
|
|
No, actually it does not. A crashing game may not be a matter of life and death, but an angry gamer also is a dissatisfied customer.
A user interface should always show exactly, what the user's options are at this moment. Also, it should tell the user why an option is not available at the moment. I simply don't let the user do memory intensive stuff and simply wait for the exception to happen. Predicting trouble and telling the user what he can do about it leaves an entirely different impression.
Then there are sequences of operations which may become very complicated to undo if any serious error occurs somewhere in the middle. Checking the most probable causes of errors in advance, including memory limitations, may help by not letting the error occur in the first place.
A while ago he asked me what he should have printed on my business cards. I said 'Wizard'.
I read books which nobody else understand. Then I do something which nobody understands. After that the computer does something which nobody understands. When asked, I say things about the results which nobody understand. But everybody expects miracles from me on a regular basis. Looks to me like the classical definition of a wizard.
|
|
|
|
|
...64K. If the OS can't spare 64K, then my program will be the least of your worries.
- S
50 cups of coffee and you know it's on!
A post a day, keeps the white coats away!
|
|
|
|
|
Commodore 64 lives...!
|
|
|
|