For purpose of an academic project I want to create a simple program to hang a computer by consuming all available RAM. I tried creating a string and increase its length using a while loop like string=string+string2. I takes so much time when string2 is short, even though I could use 100% ram but at last I get a OutOfMemoryException and so much memory is freed. Is there any effective way of doing this?
Not entirely possible as the OS will deal with the memory management thus you wont be able to do what you are trying to do especially in managed code.
I also don't believe that an academic programme will have a course or instruct students to develop something like this. question sounds a little wierd
Related
I have just gotten into programming and i realized for every variable that you identify, you use ur computer memory as it is saved in it somewhere.
I wanted to know if I run a piece of code multiple times, would I lose more memory or somehow once you close terminal or program, system deletes it automatically.
THANK YOU
I've run a code several times and every time the address that a same variable is saved in is different.
I believe I'm wasting my computers memory so if I am how do I delete said variables from memory?
Yes, for all intents and purposes, it is gone the second the program has finished executing.
There are times that this isn't true, but they almost certainly don't apply to you. When on a normal computer or OS running device, the OS (operating system) will clean-up any resources used by your code when it is finished running. This includes all the memory used by declared variables (which is tiny amounts anyway, normally), files you have opened and forgotten to close, and pretty much everything else. OSs are very resilient!
I've run a code several times and every time the address that a same variable is saved in is different. I believe I'm wasting my computers memory so if I am how do I delete said variables from memory?
These are some pretty good investigative skills (a good sign for someone new to programming), but there are different reasons for this, don't worry. Memory addresses are a complex topic that is worth a look at later down the line, but the simplified story is that memory addresses are different every time you run the program for both security and performance reasons.
I'm new to programming, taking MIT's 6.00. While watching the Dynamic Programming lecture a simple question occurred to me: Is there any kind of built-in feature (for computers in general) to detect repetitive tasks and compensate?
I realize that's quite vague. I was working on my grandfather's computer because he had been complaining that it was slow. Indeed, it would lag for up to 15 seconds at a time, waiting for programs to open, etc. When I upgraded the RAM, the problem was gone. So if the computer was constantly having to write page ins and page outs to disk, why couldn't it have just popped up a little message suggesting a RAM upgrade? That would save quite a bit of time.
Computers are good at performing tasks quickly but slow code can be, well, slow. Can that be automated? Is this even a legitimate question?
In the example you describe the code isn't slow because it's reading/writing to disk. It's slow because it isn't actually doing anything but instead is waiting for the OS to page in and out to disk.
Also, a RAM upgrade isn't always the solution to frequent paging (say buggy program leaking memory or something).
It's not really possible in the general sense for the OS to detect what all the possible issues are and suggest a solution. That is in fact a variation of the Halting Problem.
It's impossible in general for a computer to know whether a slowness was because it's running an operation that fundamentally takes a long time to finish, or whether it's taking more time than it should really be.
Also, even if you've identified that an operation is slow, it's even more difficult to diagnose the precise reason why it is slow. Sometimes it's because you need more RAM, other times because slow network, or slow disk, or slow CPU. This is even more harder if the checker is running inside the same machine that it is running on since it's also experiencing the slowness itself.
However there are several things that can be done under certain limited situations. Many popular OSes (e.g. Windows, Linux, Android) can detect slow response to user input, and will offer to either give more time or force close applications (Android) or draw the not responding window in grayscale (Linux), or in bluish tint (Windows), if the application fails to respond to user input within certain period of time.
What I'm talking about is: Is it possible that under certain circumstances the CPU "buggs" and suddenly responses 1+1=2?
In which parts of the computer can that happen (HDD, RAM, Mainboard)?
What could be the causes? Bad quality? Overheating?
Does that even happen? When yes, how frequently?
If everything is okay with the CPU (not a single fault in production, good temperature), can that still happen sometimes?
What would be the results of, let's say one to three wrong computations?
This is programming related as it would be nice to know if you can even rely on the hardware to return the right results.
It can happen in all hardware; it happens quite often in RAM chips. There are mechanisms to detect and correct such errors, but in regards to RAM, only in the more expensive ECC chips. See Wikipedia's article on RAM and Error Correction
Also interesting is the article on Error Detection and Correction in general.
One example: http://en.wikipedia.org/wiki/Pentium_FDIV_bug
What I'm talking about is: Is it
possible that under certain
circumstances the CPU "buggs" and
suddenly responses 1+1=2?
Yes
In which parts of the computer can
that happen (HDD, RAM, Mainboard)?
All of them
What could be the causes? Bad quality?
Overheating?
The most common cause is overclocking. Less common causes include faulty hardware.
If everything is okay with the CPU
(not a single fault in production,
good temperature), can that still
happen sometimes?
It can be a ram problem like I said above, or really anything.
What would be the results of, let's
say one to three wrong computations?
I don't understand this question.. You mean what would happen to the program? It would probably segfault but impossible to say. You mean what would 1+1 result into? Impossible to say. You mean what would happen if 1 in 3 computations were to fail on average? The computer wouldn't even boot.
Well first you need to find an Computer Engineer who thinks that 1+1=2 is a bug and that its a hardware problem which needs to be fixed.
#Andreas Bonini, Midhat and Pekka: In such incidences it would be highly recommended to take a maths course on April Fool's day.
Andrew Appel had a great demo a few years ago where he started a lecture by lighting a 100W bulb under a PC running Java. Within 20 minutes there were enough memory errors that he could exploit one to crack the Java virtual machine and take it over.
Cool your hardware!
I'm trying to write a program that will detect signs of failure for portable flash memory devices (thumb drives, etc).
I have seen tools in the past that are able to detect failing sectors and other kinds of trouble on conventional mechanical hard drives, but I fear that flash memory does not have the same kind of predictable low-level access to the hardware due to the internal workings of the storage. Things like wear-leveling and other block-remapping techniques (to skip over 'dead' sectors?) lead me to believe that determining if a flash drive is failing will be difficult at best, if not impossible (short of having constant read failures and device unmounts).
Flash drives at their end-of-life should be easy to detect (constant CRC discrepancies during reads and all-out failure). But what about drives that might be failing early? Are there any tell-tale signs like slower throughput speeds that might indicate a flash drive is going to fail much sooner than normal?
Along the lines of detecting potentially bad blocks, I had considered attempting random reads/writes to a file close to or exactly the size of the entire volume, but even then is it possible that the drive might report sizes under its maximum capacity to account for 'dead' blocks?
In short, is there any way to circumvent or at least detect (algorithmically or otherwise) the use of block-remapping or other life extension techniques for flash memory?
Let me end this question by expressing my uncertainty as to whether or not this belongs on serverfault.com . This is definitely a hardware-related question, but I also desire a software solution - preferably one that I can program myself.
If this question is misplaced, I will be happy to migrate it to serverfault - but I do need a programming solution. Please let me know if you need clarification :)
Thanks!
It's interesting if badblocks can help in this case
AFAIK, Wear leveling happens at the firmware level. The hardware does not know about the bad block, till such time the firmware detects one.
And there is no known way to find this bad sectors before hand. BTW, I guess, it is not bad sectors, but bad blocks. Once a sector is bad, the whole block is marked as bad ...
Even the standard blank-window Cocoa app that gets built when you make a new Cocoa project in Xcode uses almost 6 MB of memory. What's the reason for this? Is it possible to make an app use less, or does OS X simply manage memory differently for Cocoa apps?
Not that I'm complaining. I know that performance "hardly matters anymore" (edit: what I mean is, it matters less than readability/maintainability/the programmer's time). I'm just curious.
OS X does a lot of magic with shared memory and copy-on-write pages, so chances are that it doesn't take that much physical RAM for every application.
You can check exactly how memory blocks are mapped by running:
sudo vmmap <PID of the process>
Depends on the all the framework (APIs) you use. Combine that with the VM allocations done by low level ops.
It's only worth trying to reduce the heap alloc (total), as well as the resident size of the code. Making sure your data structs are allocated efficiently and trying to compile with the ever-so-famous "-Os" optimization flag (size optimization). There isn't much you can do about the VM eaten by Cocoa. I wouldn't really worry about it.
This is clearly a 'WTF' moment for developers in general. The question is usually - why does my trivial application use up so much memory.
The answer is down to the underlying framework. You could argue that 6MB is too much, but really, it is nothing.
It's not rare to see computers come with 2GB of memory these days. The stock IMAC is 4GB. The whole point of the computer industry is to use up all the resources a machine has so that it continues to evolve.
Yes you should avoid ineffecincies where possible (Don't load up a 5million point array at start up for instance). But unless your beta demonstrates you fudged up just keep it on the list of todo's.
I'm a bit out on a limb here, but I guess it's because all the libraries that get added have to do quite a bit of setting up and there is no need to garbage collect, so they simply get to waste memory; plus, even if all memory got autoreleased, it would wait until the first idle event, which is after the creation of the window. Delete unneeded libraries/frameworks, or force a garbage collect somewhere after loading the window from the nib and see how much it goes down if you're so concerned.
I am not concerned about it. Some of the memory might be returned later, and the rest is the price you pay for a powerful framework.
A factor which is not directly linked to cocoa but is valid to frameworks in general is that the overhead is not linear. There is usually a fixed and a variable "price", in terms of overhead, to use the framework.
When you create a simple blank window, the fixed overhead is crushing, but when you create an application with tens of windows, dialogs, controls and all, the initial fixed overhead becomes negligible, against the size of the application itself.