Skip to content

Commit 429fc7a

Browse files
committed
Use malloc_trim when using glibc
* use malloc_trim(0) to return memory to OS after free (after closing window) * release the memory when clearing history and call malloc_trim()
1 parent df6488a commit 429fc7a

2 files changed

Lines changed: 23 additions & 1 deletion

File tree

cmd-clear-history.c

Lines changed: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,9 @@
1717
*/
1818

1919
#include <sys/types.h>
20+
#ifdef __GLIBC__
21+
# include <malloc.h>
22+
#endif
2023

2124
#include "tmux.h"
2225

@@ -40,12 +43,24 @@ cmd_clear_history_exec(struct cmd *self, struct cmd_q *cmdq)
4043
struct args *args = self->args;
4144
struct window_pane *wp;
4245
struct grid *gd;
46+
struct grid_line *gl;
47+
u_int yy;
4348

4449
if (cmd_find_pane(cmdq, args_get(args, 't'), NULL, &wp) == NULL)
4550
return (CMD_RETURN_ERROR);
4651
gd = wp->base.grid;
4752

48-
grid_move_lines(gd, 0, gd->hsize, gd->sy);
53+
for (yy = 0; yy < gd->hsize+gd->sy; yy++) {
54+
gl = &gd->linedata[yy];
55+
free(gl->celldata);
56+
}
57+
58+
free(gd->linedata);
59+
# ifdef M_TRIM_THRESHOLD
60+
malloc_trim(0);
61+
# endif
62+
63+
gd->linedata = xcalloc(gd->sy, sizeof *gd->linedata);
4964
gd->hsize = 0;
5065

5166
return (CMD_RETURN_NORMAL);

grid.c

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,9 @@
2020

2121
#include <stdlib.h>
2222
#include <string.h>
23+
#ifdef __GLIBC__
24+
# include <malloc.h>
25+
#endif
2326

2427
#include "tmux.h"
2528

@@ -113,6 +116,10 @@ grid_destroy(struct grid *gd)
113116
free(gd->linedata);
114117

115118
free(gd);
119+
120+
# ifdef M_TRIM_THRESHOLD
121+
malloc_trim(0);
122+
# endif
116123
}
117124

118125
/* Compare grids. */

0 commit comments

Comments
 (0)