0

I am facing an issue regarding sort being killed due over usage of memory and can not understand why it surpasses the given limit by a significant margin.

Context: 
Available physical RAM: 8GB 
Memory in use by system: <500MB (FreeBSD 13) 
Free memory: ~7.5GB 
Swap: 2G
No system hard limit on mem usage ($ ulimit -> unlimited)
File being sorted: 300+ GB 
HDD space: 1Tb+ free both on target or temporary dir.

Even when I run sort specifying a resource limit whithin the available; or without any explicit argument (which should automatically be set to 90% of available) it eventually gets killed due over usage.

[mario@casa-server ~/pjy]$ sort -S 7G -T /filesWD-WMAY03648941/tmp/ -o /filesWD-WMAY03648941/pjA-sorted.txt pjyA.txt

Killed
[mario@casa-server ~/pjy]$
[mario@casa-server ~/pjy]$ sort -T /filesWD-WMAY03648941/tmp/ -o /filesWD-WMAY03648941/pjA-sorted.txt pjyA.txt

Killed
[mario@casa-server ~/pjy]$

Apparently it is indeed over using the resource limit. When run with -S 3G I can observe around 5G of usage sometimes.

# ~5G USAGE EVEN WITH EXPLICIT -S 3G LIMIT


  [mario@casa-server ~]$ sudo procstat -r 3183
  PID COMM             RESOURCE                          VALUE        
 3183 sort             user time                    00:27:51.849171   
 3183 sort             system time                  00:03:28.220262   
 3183 sort             maximum RSS                          5423072 KB
 3183 sort             integral shared memory              10505728 KB
 3183 sort             integral unshared data               1910064 KB
 3183 sort             integral unshared stack             30560128 KB
 3183 sort             page reclaims                        8172561   
 3183 sort             page faults                             1724   
 3183 sort             swaps                                      0   
 3183 sort             block reads                            24296   
 3183 sort             block writes                           17719   
 3183 sort             messages sent                              0   
 3183 sort             messages received                          0   
 3183 sort             signals received                           0   
 3183 sort             voluntary context switches              5868   
 3183 sort             involuntary context switches           30700   
[mario@casa-server ~]$ 


# TOP excerpt
 PID USERNAME    THR PRI NICE   SIZE    RES STATE    C   TIME    WCPU COMMAND
 3183 mario         1 103    0  4657M  4603M CPU3     3  38:23  99.88% sort -S 3G -T /filesWD-WMAY03648941/tmp/ -o /files



# NO SO LIMIT ON USAGE
[mario@casa-server ~]$ sudo procstat -l 3183
  PID COMM             RLIMIT                  SOFT             HARD     
 3183 sort             cputime             infinity         infinity     
 3183 sort             filesize            infinity         infinity     
 3183 sort             datasize               32768 MB         32768 MB  
 3183 sort             stacksize             524288 KB        524288 KB  
 3183 sort             coredumpsize        infinity         infinity     
 3183 sort             memoryuse           infinity         infinity     
 3183 sort             memorylocked           65536 B          65536 B   
 3183 sort             maxprocesses           12186            12186     
 3183 sort             openfiles             234576           234576     
 3183 sort             sbsize              infinity         infinity     
 3183 sort             vmemoryuse          infinity         infinity     
 3183 sort             pseudo-terminals    infinity         infinity     
 3183 sort             swapuse             infinity         infinity     
 3183 sort             kqueues             infinity         infinity     
 3183 sort             umtxp               infinity         infinity     
[mario@casa-server ~]$
[mario@casa-server ~]$ ulimit 
unlimited

That explain why it is being killed when ran with 7G limit (of 7.5 available), or when the limit was automatically set: it was indeed over using all the mem+swap (and being killed by OS as expected).

But what am I missing here? man sort:

 -S size, --buffer-size=size
         Use size for the maximum size of the memory buffer.  Size
         modifiers %,b,K,M,G,T,P,E,Z,Y can be used.  If a memory limit is
         not explicitly specified, sort takes up to about 90% of available
         memory.  If the file size is too big to fit into the memory
         buffer, the temporary disk files are used to perform the sorting.

If -S 3G is set, why is it over using RAM and not the HDD temporary folder?

PS: I tried to ask this question related to other 'sort being killed due out of memory' but it was mismanaged by some moderators. I do think it is a related question (and flagging it as a unrelated one just fragments the solution). Also, I read through many other possible solutions and they are not suited to this case.

Thanks all

4
  • How much free space is in the directory specified with -T? Commented Apr 2, 2020 at 6:05
  • Your question wasn't mismanaged - you posted it as an answer, so it was deleted. Commented Apr 2, 2020 at 8:14
  • For others, related - Unix sort out of memory Commented Apr 2, 2020 at 8:15
  • @NasirRiley ~ 1.5 Tb free (4+ times the file being sorted) Commented Apr 2, 2020 at 19:29

0

You must log in to answer this question.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.