Skip to content

Commit 7528a31

Browse files
fix: calculate memoryUsage in one place
1 parent 84bbdad commit 7528a31

File tree

2 files changed

+14
-18
lines changed

2 files changed

+14
-18
lines changed

src/components/MemoryViewer/MemoryViewer.tsx

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -53,23 +53,23 @@ export function MemoryViewer({
5353
warningThreshold = 60,
5454
dangerThreshold = 80,
5555
}: MemoryProgressViewerProps) {
56-
const value = stats.AnonRss ?? calculateAllocatedMemory(stats);
56+
const memoryUsage = stats.AnonRss ?? calculateAllocatedMemory(stats);
5757

5858
const capacity = stats.HardLimit;
5959

6060
const theme = useTheme();
6161
let fillWidth =
62-
Math.round((parseFloat(String(value)) / parseFloat(String(capacity))) * 100) || 0;
62+
Math.round((parseFloat(String(memoryUsage)) / parseFloat(String(capacity))) * 100) || 0;
6363
fillWidth = fillWidth > 100 ? 100 : fillWidth;
64-
let valueText: number | string | undefined = value,
64+
let valueText: number | string | undefined = memoryUsage,
6565
capacityText: number | string | undefined = capacity,
6666
divider = '/';
6767
if (percents) {
6868
valueText = fillWidth + '%';
6969
capacityText = '';
7070
divider = '';
7171
} else if (formatValues) {
72-
[valueText, capacityText] = formatValues(Number(value), Number(capacity));
72+
[valueText, capacityText] = formatValues(Number(memoryUsage), Number(capacity));
7373
}
7474

7575
const renderContent = () => {
@@ -81,13 +81,13 @@ export function MemoryViewer({
8181
};
8282

8383
const calculateMemoryShare = (segmentSize: number) => {
84-
if (!value) {
84+
if (!memoryUsage) {
8585
return 0;
8686
}
8787
return (segmentSize / parseFloat(String(capacity))) * 100;
8888
};
8989

90-
const memorySegments = getMemorySegments(stats);
90+
const memorySegments = getMemorySegments(stats, Number(memoryUsage));
9191

9292
const status = calculateProgressStatus({
9393
fillWidth,

src/components/MemoryViewer/utils.ts

Lines changed: 8 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ interface MemorySegment {
2121
isInfo?: boolean;
2222
}
2323

24-
export function getMemorySegments(stats: TMemoryStats): MemorySegment[] {
24+
export function getMemorySegments(stats: TMemoryStats, memoryUsage: number): MemorySegment[] {
2525
const segments = [
2626
{
2727
label: i18n('text_shared-cache'),
@@ -57,18 +57,14 @@ export function getMemorySegments(stats: TMemoryStats): MemorySegment[] {
5757
) as MemorySegment[];
5858
const sumNonInfoSegments = nonInfoSegments.reduce((acc, segment) => acc + segment.value, 0);
5959

60-
const memoryUsage = getMaybeNumber(stats.AnonRss ?? calculateAllocatedMemory(stats));
60+
const otherMemory = Math.max(0, memoryUsage - sumNonInfoSegments);
6161

62-
if (memoryUsage) {
63-
const otherMemory = Math.max(0, memoryUsage - sumNonInfoSegments);
64-
65-
segments.push({
66-
label: i18n('text_other'),
67-
key: 'Other',
68-
value: otherMemory,
69-
isInfo: false,
70-
});
71-
}
62+
segments.push({
63+
label: i18n('text_other'),
64+
key: 'Other',
65+
value: otherMemory,
66+
isInfo: false,
67+
});
7268

7369
segments.push(
7470
{

0 commit comments

Comments
 (0)