Skip to content

Commit

Permalink
Use malloc_trim when using glibc
Browse files Browse the repository at this point in the history
* use malloc_trim(0) to return memory to OS after free (after closing window)
* release the memory when clearing history and call malloc_trim()
  • Loading branch information
42wim committed Jan 17, 2015
1 parent df6488a commit 429fc7a
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 1 deletion.
17 changes: 16 additions & 1 deletion cmd-clear-history.c
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,9 @@
*/

#include <sys/types.h>
#ifdef __GLIBC__
# include <malloc.h>
#endif

#include "tmux.h"

Expand All @@ -40,12 +43,24 @@ cmd_clear_history_exec(struct cmd *self, struct cmd_q *cmdq)
struct args *args = self->args;
struct window_pane *wp;
struct grid *gd;
struct grid_line *gl;
u_int yy;

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

grid_move_lines(gd, 0, gd->hsize, gd->sy);
for (yy = 0; yy < gd->hsize+gd->sy; yy++) {
gl = &gd->linedata[yy];
free(gl->celldata);
}

free(gd->linedata);
# ifdef M_TRIM_THRESHOLD
malloc_trim(0);
# endif

gd->linedata = xcalloc(gd->sy, sizeof *gd->linedata);
gd->hsize = 0;

return (CMD_RETURN_NORMAL);
Expand Down
7 changes: 7 additions & 0 deletions grid.c
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,9 @@

#include <stdlib.h>
#include <string.h>
#ifdef __GLIBC__
# include <malloc.h>
#endif

#include "tmux.h"

Expand Down Expand Up @@ -113,6 +116,10 @@ grid_destroy(struct grid *gd)
free(gd->linedata);

free(gd);

# ifdef M_TRIM_THRESHOLD
malloc_trim(0);
# endif
}

/* Compare grids. */
Expand Down

0 comments on commit 429fc7a

Please sign in to comment.