annotate OrthancServer/Resources/ImplementationNotes/memory_consumption.txt @ 5153:217863b09457 malloc-trim

malloc_trim doc
author Alain Mazy <am@osimis.io>
date Wed, 01 Feb 2023 19:23:58 +0100
parents
children 20911302c6e7
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
5153
217863b09457 malloc_trim doc
Alain Mazy <am@osimis.io>
parents:
diff changeset
1 In Orthanc 1.11.3, we have introduced a Housekeeper thread that
217863b09457 malloc_trim doc
Alain Mazy <am@osimis.io>
parents:
diff changeset
2 tries to give back unused memory back to the system. This is implemented
217863b09457 malloc_trim doc
Alain Mazy <am@osimis.io>
parents:
diff changeset
3 by calling malloc_trim every 100ms.
217863b09457 malloc_trim doc
Alain Mazy <am@osimis.io>
parents:
diff changeset
4
217863b09457 malloc_trim doc
Alain Mazy <am@osimis.io>
parents:
diff changeset
5 Here is how we validated the effect of this new feature:
217863b09457 malloc_trim doc
Alain Mazy <am@osimis.io>
parents:
diff changeset
6 -------------------------------------------------------
217863b09457 malloc_trim doc
Alain Mazy <am@osimis.io>
parents:
diff changeset
7
217863b09457 malloc_trim doc
Alain Mazy <am@osimis.io>
parents:
diff changeset
8 We compared the behaviour of 2 osimis/orthanc Docker images from the mainline
217863b09457 malloc_trim doc
Alain Mazy <am@osimis.io>
parents:
diff changeset
9 on Feb 1st 2023. One image without the call to malloc_trim and the other with
217863b09457 malloc_trim doc
Alain Mazy <am@osimis.io>
parents:
diff changeset
10 this call.
217863b09457 malloc_trim doc
Alain Mazy <am@osimis.io>
parents:
diff changeset
11
217863b09457 malloc_trim doc
Alain Mazy <am@osimis.io>
parents:
diff changeset
12 1st test: unconstrained Docker containers
217863b09457 malloc_trim doc
Alain Mazy <am@osimis.io>
parents:
diff changeset
13 .........................................
217863b09457 malloc_trim doc
Alain Mazy <am@osimis.io>
parents:
diff changeset
14
217863b09457 malloc_trim doc
Alain Mazy <am@osimis.io>
parents:
diff changeset
15 5 large studies are uploaded to each instance of Orthanc (around 1GB in total).
217863b09457 malloc_trim doc
Alain Mazy <am@osimis.io>
parents:
diff changeset
16 A script triggers anonymization of these studies as quick as possible.
217863b09457 malloc_trim doc
Alain Mazy <am@osimis.io>
parents:
diff changeset
17 We compare the memory used by the containers after 2 minutes of execution
217863b09457 malloc_trim doc
Alain Mazy <am@osimis.io>
parents:
diff changeset
18 (using `docker stats`):
217863b09457 malloc_trim doc
Alain Mazy <am@osimis.io>
parents:
diff changeset
19 - without malloc_trim: 1500 MB
217863b09457 malloc_trim doc
Alain Mazy <am@osimis.io>
parents:
diff changeset
20 - with malloc_trim: 410 MB
217863b09457 malloc_trim doc
Alain Mazy <am@osimis.io>
parents:
diff changeset
21
217863b09457 malloc_trim doc
Alain Mazy <am@osimis.io>
parents:
diff changeset
22 2nd test: memory constrained Docker containers
217863b09457 malloc_trim doc
Alain Mazy <am@osimis.io>
parents:
diff changeset
23 ..............................................
217863b09457 malloc_trim doc
Alain Mazy <am@osimis.io>
parents:
diff changeset
24
217863b09457 malloc_trim doc
Alain Mazy <am@osimis.io>
parents:
diff changeset
25 Each Orthanc container is limited to 400MB (through the docker-compose configuration
217863b09457 malloc_trim doc
Alain Mazy <am@osimis.io>
parents:
diff changeset
26 `mem_limit: 400m`)
217863b09457 malloc_trim doc
Alain Mazy <am@osimis.io>
parents:
diff changeset
27 5 large studies are uploaded to each instance of Orthanc (around 1GB in total).
217863b09457 malloc_trim doc
Alain Mazy <am@osimis.io>
parents:
diff changeset
28 Each study is anonymized manually, one by one and then, we repeat the operation.
217863b09457 malloc_trim doc
Alain Mazy <am@osimis.io>
parents:
diff changeset
29 We compare the memory used by the containers after 2 minutes of execution
217863b09457 malloc_trim doc
Alain Mazy <am@osimis.io>
parents:
diff changeset
30 (using `docker stats`):
217863b09457 malloc_trim doc
Alain Mazy <am@osimis.io>
parents:
diff changeset
31
217863b09457 malloc_trim doc
Alain Mazy <am@osimis.io>
parents:
diff changeset
32 # study without malloc_trim with_malloc_trim
217863b09457 malloc_trim doc
Alain Mazy <am@osimis.io>
parents:
diff changeset
33 0 ~ 50 MB ~ 50 MB
217863b09457 malloc_trim doc
Alain Mazy <am@osimis.io>
parents:
diff changeset
34 1 ~ 140 MB ~ 140 MB
217863b09457 malloc_trim doc
Alain Mazy <am@osimis.io>
parents:
diff changeset
35 2 ~ 390 MB ~ 340 MB
217863b09457 malloc_trim doc
Alain Mazy <am@osimis.io>
parents:
diff changeset
36 3 ~ 398 MB ~ 345 MB
217863b09457 malloc_trim doc
Alain Mazy <am@osimis.io>
parents:
diff changeset
37 4 out-of-memory crash ~ 345 MB
217863b09457 malloc_trim doc
Alain Mazy <am@osimis.io>
parents:
diff changeset
38 5..20 ~ 380 MB (stable)
217863b09457 malloc_trim doc
Alain Mazy <am@osimis.io>
parents:
diff changeset
39
217863b09457 malloc_trim doc
Alain Mazy <am@osimis.io>
parents:
diff changeset
40 Note: the use of malloc_trim does not guarantee that Orthanc will never reach a
217863b09457 malloc_trim doc
Alain Mazy <am@osimis.io>
parents:
diff changeset
41 out-of-memory error, especially on very constrained systems.
217863b09457 malloc_trim doc
Alain Mazy <am@osimis.io>
parents:
diff changeset
42 Depending on the allocation pattern, the Orthanc memory can get
217863b09457 malloc_trim doc
Alain Mazy <am@osimis.io>
parents:
diff changeset
43 very fragmented and increase since malloc_trim only releases memory
217863b09457 malloc_trim doc
Alain Mazy <am@osimis.io>
parents:
diff changeset
44 at the end of each of malloc arena. However, note that, even long before the
217863b09457 malloc_trim doc
Alain Mazy <am@osimis.io>
parents:
diff changeset
45 introduction of malloc_trim, we have observed Orthanc instances running for years
217863b09457 malloc_trim doc
Alain Mazy <am@osimis.io>
parents:
diff changeset
46 without ever reaching out-of-memory errors and Orthanc is usually considered as
217863b09457 malloc_trim doc
Alain Mazy <am@osimis.io>
parents:
diff changeset
47 very stable.
217863b09457 malloc_trim doc
Alain Mazy <am@osimis.io>
parents:
diff changeset
48
217863b09457 malloc_trim doc
Alain Mazy <am@osimis.io>
parents:
diff changeset
49
217863b09457 malloc_trim doc
Alain Mazy <am@osimis.io>
parents:
diff changeset
50
217863b09457 malloc_trim doc
Alain Mazy <am@osimis.io>
parents:
diff changeset
51
217863b09457 malloc_trim doc
Alain Mazy <am@osimis.io>
parents:
diff changeset
52
217863b09457 malloc_trim doc
Alain Mazy <am@osimis.io>
parents:
diff changeset
53
217863b09457 malloc_trim doc
Alain Mazy <am@osimis.io>
parents:
diff changeset
54 malloc_trim documentation
217863b09457 malloc_trim doc
Alain Mazy <am@osimis.io>
parents:
diff changeset
55 -------------------------
217863b09457 malloc_trim doc
Alain Mazy <am@osimis.io>
parents:
diff changeset
56
217863b09457 malloc_trim doc
Alain Mazy <am@osimis.io>
parents:
diff changeset
57 from (https://stackoverflow.com/questions/40513716/malloc-trim0-releases-fastbins-of-thread-arenas)
217863b09457 malloc_trim doc
Alain Mazy <am@osimis.io>
parents:
diff changeset
58
217863b09457 malloc_trim doc
Alain Mazy <am@osimis.io>
parents:
diff changeset
59 If possible, gives memory back to the system (via negative
217863b09457 malloc_trim doc
Alain Mazy <am@osimis.io>
parents:
diff changeset
60 arguments to sbrk) if there is unused memory at the `high' end of
217863b09457 malloc_trim doc
Alain Mazy <am@osimis.io>
parents:
diff changeset
61 the malloc pool. You can call this after freeing large blocks of
217863b09457 malloc_trim doc
Alain Mazy <am@osimis.io>
parents:
diff changeset
62 memory to potentially reduce the system-level memory requirements
217863b09457 malloc_trim doc
Alain Mazy <am@osimis.io>
parents:
diff changeset
63 of a program. However, it cannot guarantee to reduce memory. Under
217863b09457 malloc_trim doc
Alain Mazy <am@osimis.io>
parents:
diff changeset
64 some allocation patterns, some large free blocks of memory will be
217863b09457 malloc_trim doc
Alain Mazy <am@osimis.io>
parents:
diff changeset
65 locked between two used chunks, so they cannot be given back to
217863b09457 malloc_trim doc
Alain Mazy <am@osimis.io>
parents:
diff changeset
66 the system.
217863b09457 malloc_trim doc
Alain Mazy <am@osimis.io>
parents:
diff changeset
67
217863b09457 malloc_trim doc
Alain Mazy <am@osimis.io>
parents:
diff changeset
68 The `pad' argument to malloc_trim represents the amount of free
217863b09457 malloc_trim doc
Alain Mazy <am@osimis.io>
parents:
diff changeset
69 trailing space to leave untrimmed. If this argument is zero,
217863b09457 malloc_trim doc
Alain Mazy <am@osimis.io>
parents:
diff changeset
70 only the minimum amount of memory to maintain internal data
217863b09457 malloc_trim doc
Alain Mazy <am@osimis.io>
parents:
diff changeset
71 structures will be left (one page or less). Non-zero arguments
217863b09457 malloc_trim doc
Alain Mazy <am@osimis.io>
parents:
diff changeset
72 can be supplied to maintain enough trailing space to service
217863b09457 malloc_trim doc
Alain Mazy <am@osimis.io>
parents:
diff changeset
73 future expected allocations without having to re-obtain memory
217863b09457 malloc_trim doc
Alain Mazy <am@osimis.io>
parents:
diff changeset
74 from the system.
217863b09457 malloc_trim doc
Alain Mazy <am@osimis.io>
parents:
diff changeset
75
217863b09457 malloc_trim doc
Alain Mazy <am@osimis.io>
parents:
diff changeset
76 Malloc_trim returns 1 if it actually released any memory, else 0.
217863b09457 malloc_trim doc
Alain Mazy <am@osimis.io>
parents:
diff changeset
77 On systems that do not support "negative sbrks", it will always
217863b09457 malloc_trim doc
Alain Mazy <am@osimis.io>
parents:
diff changeset
78 return 0.