A
A
Artem Kislenko2021-03-03 09:43:39
Programming
Artem Kislenko, 2021-03-03 09:43:39

What happens to a program (Go, Python, JS, PHP) if it needs to allocate memory and the OS runs out of RAM?

Situation:
1. A file is read from the network (disk) in chunks
2. All this is added to a variable
3. The file is large - RAM is running out
4. What will happen to the program (if there are no additional processing)? Will the disk space be allocated (by the operating system)? What if this place is not available anywhere else?

Answer the question

In order to leave comments, you need to log in

7 answer(s)
V
Vasily Bannikov, 2021-03-03
@webwork

Swap first, then out of memory exception

A
antonwx, 2021-03-03
@antonwx

The swap file will be involved.
If there is no more space, an out of memory exception will occur and the program will crash.
In general, putting huge files completely in RAM is bad.

R
Romses Panagiotis, 2021-03-03
@romesses

It doesn't matter which programming language is used. OS, first of all, will try to serve the appetites of the program. And if the available RAM is not enough, it will get into the swap file / partition (swap).
In Linux, the program will be killed by the OOM protection mechanism as soon as it exceeds a certain threshold of load on system resources. On Windows - I don’t know, maybe it just won’t allocate a critical piece of memory for the application.
And if the program does not know how to handle out-of-memory errors, then it will simply hang or crash with a post-mortem dump.

1. The file is read from the network (disk) in chunks
Wow, wonderful!
2. It all adds up to a variable
What?! Why was it needed? It is as if in construction they put a brick house on the foundation as a whole, and not in parts.

H
HemulGM, 2021-03-03
@HemulGM

Why read it to memory? Why not read from the network directly to disk?
And if your program is 32bit, then you won’t be able to use all the RAM at all if you have more than 3GB of it.

N
Noble-Bastard, 2021-03-03
@Noble-Bastard

I don’t know how it is on other OSes, but Windows will most likely not highlight it. There is a limit

1
12rbah, 2021-03-03
@12rbah

Depends on how much is required, if a little, then it will be written to the swap, if a lot (I think when the amount of required memory exceeds the swap), then the system may crash. I once decided to test this (I don’t advise doing this), created an array that could take several tens of gigs and started filling it in a loop, as a result, the system crashed with an error (it had 4GB of RAM in total), I tested it on Windows 10 .

A
Artem Kislenko, 2021-03-03
@webwork

In general, thank you all.
In swarm mode in docker, as it turned out, everything crashes with code 137 - it is not allocated to swap by default, in order to stand out, you need to write additional. options. But this is, of course, a moot point - if the application is indomitably gluttonous, then it's pointless.
Read more here:
https://docs.docker.com/config/containers/resource...

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question