You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: blog/_posts/2020-12-04-measuring-memory-usage-in-rust.adoc
+7-7Lines changed: 7 additions & 7 deletions
Original file line number
Diff line number
Diff line change
@@ -26,8 +26,8 @@ _The second approach_ is based on instrumenting the calls to allocation and deal
26
26
The profiler captures backtraces when the program calls `malloc` and `free` and constructs a flamegraph displaying "`hot`" functions which allocate a lot.
27
27
This is how, for example, https://github.com/KDE/heaptrack[heaptrack] works (see also https://github.com/cuviper/alloc_geiger[alloc geiger]).
28
28
29
-
The two approaches are complimentary.
30
-
If the problem is that the application does to many short-lived allocations (instead of re-using the buffers), it would be invisible for the first approach, but very clear in the second one.
29
+
The two approaches are complementary.
30
+
If the problem is that the application does too many short-lived allocations (instead of re-using the buffers), it would be invisible for the first approach, but very clear in the second one.
31
31
If the problem is that, in a steady state, the application uses to much memory, the first approach would work better for pointing out which data structures need most attention.
32
32
33
33
In rust-analyzer, we are generally interested in keeping the overall memory usage small, and can make better use of heap parsing approach.
@@ -40,7 +40,7 @@ There is Servo's https://github.com/servo/servo/tree/2d3811c21bf1c02911d5002f967
40
40
41
41
Another alternative is running the program under valgrind to gain runtime introspectability.
42
42
https://www.valgrind.org/docs/manual/ms-manual.html[Massif] and and https://www.valgrind.org/docs/manual/dh-manual.html[DHAT] work that way.
43
-
Running with valgrind is pretty slow, and still doesn't give the Java-level fidelity.
43
+
Running with valgrind is pretty slow, and still doesn't give Java-level fidelity.
44
44
45
45
Instead, rust-analyzer mainly relies on a much simpler approach for figuring out which things are heavy.
46
46
This is the first trick of this article:
@@ -53,11 +53,11 @@ It's even possible to implement a https://doc.rust-lang.org/stable/std/alloc/tra
53
53
54
54
And, if you can measure total memory usage, you can measure memory usage of any specific data structure by:
55
55
56
-
. noting the current memory usage
56
+
. measuring the current memory usage
57
57
. dropping the data structure
58
-
. noting the current memory usage again
58
+
. measuring the current memory usage again
59
59
60
-
The difference between the two measurements is the size of the data structure.
60
+
The difference between the two values is the size of the data structure.
61
61
And this is exactly what rust-analyzer does to find the largest caches: https://github.com/rust-analyzer/rust-analyzer/blob/b988c6f84e06bdc5562c70f28586b9eeaae3a39c/crates/ide_db/src/apply_change.rs#L104-L238[source].
62
62
63
63
Two small notes about this method:
@@ -115,4 +115,4 @@ struct Name {
115
115
116
116
Now, if the new `Name` increases the overall memory consumption by `N`, we can estimate the total size of old ``Name``s as `N` as well, as they are twice as small.
117
117
118
-
Sometimes, quick and simple hacks works better than the finest instruments :)
118
+
Sometimes, quick and simple hacks works better than the finest instruments :).
0 commit comments