News for hackers. Stuff that matters.
Re:Dealing with Out-of-Memory Conditions in Rust by SigmundA on Saturday August 14, 2021 @08:20AM (#28180061)
I am not sure how you can make a user friendly application like that.

How about an image viewer that tries to open too large an image, should it just crash when it OOMs? I would much prefer an error dialog and the program continues to run.

Re:Dealing with Out-of-Memory Conditions in Rust by ectopod on Saturday August 14, 2021 @09:07AM (#28180313)
For big infrequent allocations (like your example of loading a huge image) it is easy to use a non-default allocator that returns an error rather than aborting.

As the article notes, this is all moot if you are running on Linux though. The allocation will always succeed but if it's too big the kernel will start killing processes, quite possibly including your image viewer.

Re:Dealing with Out-of-Memory Conditions in Rust by alerighi on Saturday August 14, 2021 @12:04PM (#28181704)
In Linux allocations can fail in some situation, for example in case it's set a resource limit on memory, because it was set a memory rlimit for the process, or the cgroup that contains the process runs out of memory (so if you run the program in a container with constrained memory).
Re:Dealing with Out-of-Memory Conditions in Rust by beached_whale on Saturday August 14, 2021 @09:52AM (#28180619)
oom handling on linux is a system option and there are also ulimits
Re:Dealing with Out-of-Memory Conditions in Rust by tooltower on Saturday August 14, 2021 @09:52AM (#28180614)
You can look at the SQLite codebase for good examples of this. Essentially, you will have to introduce a fixed upper bound for the file size you are willing to handle, given your minimum system requirements. This way, you can actually test that your program gracefully handles OOM conditions.

This page explains more on this: https://sqlite.org/limits.html

Re:Dealing with Out-of-Memory Conditions in Rust by SigmundA on Saturday August 14, 2021 @10:03AM (#28180715)
Fixed upper limits are helpful but do not prevent OOM. Perhaps the system has very little free memory due to other programs or limited hardware or other reasons but your program is intended to load large files on systems that have the memory so the upper limits are pretty high.

It's takes very little memory to show the user an error message it will almost always succeed even if the operation that triggered failed due to OOM.

Re:Dealing with Out-of-Memory Conditions in Rust by tooltower on Saturday August 14, 2021 @11:19AM (#28181314)
Of course, it's not a guarantee. But if your program is under heavy pressure from other programs, it won't continue to run in any reasonable sense either, even without Linux's OOM-killer. Yes, you can show an error message, but then the only safe thing to do is quit. You won't even be able to clean up any on-disk swap files properly, let alone handle user interactions.

Handling this is hard in desktop applications. For servers, you can have known workloads with better limits on each process.

Re:Dealing with Out-of-Memory Conditions in Rust by kragen on Saturday August 14, 2021 @05:31PM (#28184109)
That is what Linux's OOM killer does, yes. In Linux you can handle the problem by spawning off a subprocess and watching to see why it dies. Not an acceptable result for your antilock braking system firmware.
Re:Dealing with Out-of-Memory Conditions in Rust by lmilcin on Saturday August 14, 2021 @08:24AM (#28180095)
Users are aware things stop working when memory is exhausted.

As a user I am ok with this. I don't let my memory end and would prefer that the application worked reliably while there is still memory.

What I don't like is applications trying to pretend nothing happened but then doing some strange shit.

I have seen, for example, applications using 100% cpu after they hit OOM. Yes they survived. No, it is not user friendly.

Re:Dealing with Out-of-Memory Conditions in Rust by SigmundA on Saturday August 14, 2021 @08:38AM (#28180168)
Why would users be aware what happened? Did you tell them?

This is a very reliable and easy for the user and other developers to understand.

  try
  {
     image = LoadImage(path);
  }
  catch(OutOfMemoryException e)
  {
     Msgbox("Cannot load image it is too large for available memory");
  }
That is way more friendly than a program crash and allow the user to try again perhaps with a smaller version of the image because they accidentally picked the high res version or something.

Either way you may get 100% CPU as the program crashes and memory gets reclaimed, or the program continues to run and the garbage collector reclaims.

Re:Dealing with Out-of-Memory Conditions in Rust by XorNot on Saturday August 14, 2021 @08:41AM (#28180179)
Although you've encoded the whole problem right there: do you even have enough memory to setup a MsgBox widget or whatever that is?
Re:Dealing with Out-of-Memory Conditions in Rust by SigmundA on Saturday August 14, 2021 @08:47AM (#28180207)
In a GC'd language which is what I am used to, the GC will pause execution and reclaim on the next allocate if there isn't any available memory cleaning up whatever was allocated on the attempted image load.

After that if there is still not enough memory for the simple message box then you have an uncaught exception and the program crashes and you're just back to the no catch approach, nothing lost. Most likely though there will be enough memory for the message and you have a much more friendly result.

I have used this approach before and it is way way more friendly then a crash with users potentially losing work because they accidentally picked the wrong file.

Re:Dealing with Out-of-Memory Conditions in Rust by lmilcin on Saturday August 14, 2021 @09:11AM (#28180338)
> After that if there is still not enough memory for the simple message box (...)

That's exactly the point. Once memory is exhausted, you can't take any action reliably.

You don't build reliable applications by making mechanisms like: "ok, if memory ends lets design it to show a box to the user, we have 50% chance this succeeds".

It would be better to wrap your application in a script that detects when the application quit and only then shows a message to the user.

People designing things like you drive me crazy. They come up with a huge number of contingencies that just don't work in practice when push comes to shove.

Stuck in a loop trying to show a widget, using 100% of CPU and preventing me to do any action?

I prefer simpler mechanisms that work reliably.

Re:Dealing with Out-of-Memory Conditions in Rust by SigmundA on Saturday August 14, 2021 @09:40AM (#28180526)
>That's exactly the point. Once memory is exhausted, you can't take any action reliably.

Nothing is 100% reliable, thats not realistic, and in this kind of situation its is not a 50/50 shot, its more like 10000/1 that you will be able to show a message, that should be obvious.

>It would be better to wrap your application in a script that detects when the application quit and only then shows a message to the user.

Thats simpler then wrapping the specific function in a "script" that shows message to the user but allows the program to continue to function in almost all situations?

>People designing things like you drive me crazy. They come up with a huge number of contingencies that just don't work in practice when push comes to shove.

People like you who do not value you the user experience over pure code drive me crazy. These things actually do work in practice, I guarantee you any sufficiently complex GUI program will have code like this to try and gracefully handle as many contingencies as possible before simply crashing. Do you think your browser should just crash losing other tabs if a web site loads too much data? Does Photoshop just crash if it runs out of memory on an operation losing all your work?

>Stuck in a loop trying to show a widget, using 100% of CPU and preventing me to do any action?

Its no more stuck than your process crashing and the kernel is reclaiming memory, they both take similar cpu time, one however results in a message that informs you of what happens and leaves you with a running program, the other tells you nothing and your program is now gone.

Re:Dealing with Out-of-Memory Conditions in Rust by lmilcin on Saturday August 14, 2021 @06:40PM (#28184555)
> Nothing is 100% reliable, thats not realistic,

My comments were directed at people who are interested in building reliable systems.

If you straight assume it is not possible to build reliable systems, you are missing a lot.

Re:Dealing with Out-of-Memory Conditions in Rust by SigmundA on Saturday August 14, 2021 @09:12PM (#28185375)
A program that can handle an OOM and continue to function normally is more reliable to the user than one that crashes and must be restarted potentially losing work.

Memory allocation can fail, network connections are not reliable , opening a file may fail, writing a file may fail and so on. If your program simply crashed because some operation failed it would only be reliable at crashing.

Re:Dealing with Out-of-Memory Conditions in Rust by ElectricalUnion on Sunday August 15, 2021 @12:26AM (#28186266)
> continue to function normally

By definition, you are not functioning normally if you're OOM.

Re:Dealing with Out-of-Memory Conditions in Rust by SigmundA on Sunday August 15, 2021 @07:38AM (#28187944)
If you try an operation that causes an OOM by say allocating a large amount of memory as the example given above, then that memory is freed you are now functioning normally again.

If you write a large file to disk filling it to capacity, failing then delete the file, now you have free space again and everything is normal.

Re:Dealing with Out-of-Memory Conditions in Rust by kasabali on Monday August 16, 2021 @04:44AM (#28196118)
> If you write a large file to disk filling it to capacity, failing then delete the file, now you have free space again and everything is normal.

You've never used btrfs, have you? :D

Re:Dealing with Out-of-Memory Conditions in Rust by dnautics on Saturday August 14, 2021 @09:30AM (#28180465)
All critical paths, should be allocated on application start, and you never have to worry about that.
Re:Dealing with Out-of-Memory Conditions in Rust by lmilcin on Saturday August 14, 2021 @09:47AM (#28180582)
This is the kind of advice that I put in a category of "easier said than done".

The trouble is that you may not know beforehand what exactly is going to be needed. And you might need maybe a library call and the library does dynamic allocation in it and you either have no idea about it (until you find out the hard way) or no way to help it.

So in the end maybe you can take some extremely simple action like writing something to log or show a widget, but that's about it.

Re:Dealing with Out-of-Memory Conditions in Rust by dnautics on Saturday August 14, 2021 @10:07AM (#28180752)
Just code as normal and when you run into a problem, reassign the default allocator for the struct/object? Not possible for many PLs, hard for some but basically trivial (1 LOC per struct/object) for others.

Some PLs come with test suites that give you a failing allocator, so you can even easily test to make sure this looping condition resolves sanely.

Re:Dealing with Out-of-Memory Conditions in Rust by dnautics on Saturday August 14, 2021 @09:26AM (#28180430)
Preallocate that? If it's modal, you should never have more than one, and the allocator associated with the msgbox should know to use that scratch space instead of fetching new memory.

This is nontrivial in several PLs.