all messages for Emacs-related lists mirrored at yhetil.org
 help / color / mirror / code / Atom feed
* bug#42482: 27.0.91; emacs modules memory leak
@ 2020-07-22 23:25 Milan Stanojević
  2020-07-23 12:06 ` Philipp Stephani
  0 siblings, 1 reply; 14+ messages in thread
From: Milan Stanojević @ 2020-07-22 23:25 UTC (permalink / raw)
  To: 42482

[-- Attachment #1: Type: text/plain, Size: 707 bytes --]

env-make_global_ref adds a reference to the underlying Lisp_Object
and allocates emacs_value from the global storage. env->free_global_ref
on the other hand will only remove a reference to the underlying
Lisp_Object and not free the emacs_value.

Here is a simple recipe to reproduce the problem (I only tested this
on linux). I'm attaching the necessary files.

$ gcc -shared -fpic -std=c99 -I <dir-with-emacs-module.h>
create_global_refs.c -o create_global_refs.so
$ emacs --no-splash -q -l create_global_refs.so -l create_global_refs_test.el

If you look at the memory usage of emacs (for example in htop) you'll
see that with emacs-26 it is constant but with emacs-27 the resident
memory quickly grows.

[-- Attachment #2: create_global_refs.c --]
[-- Type: text/x-csrc, Size: 1053 bytes --]

#include <string.h>
#include <emacs-module.h>

int plugin_is_GPL_compatible;

static emacs_value create_loop (emacs_env *env, ptrdiff_t _nargs, emacs_value _args[], void* _data)
{
  for (int i = 0; i < 1000000; i++)
    env->free_global_ref
      (env,
       env->make_global_ref
       (env,
        env->make_integer(env, 0)));

  char msg[] = "1M global references created and freed";
  emacs_value message = { env->make_string(env, msg, strlen(msg)) };
  env->funcall(env, env->intern(env, "message"), 1, &message);

  return env->intern(env, "nil");
}

int emacs_module_init (struct emacs_runtime *ert) {
  emacs_env* env = ert->get_environment(ert);

  emacs_value loopf = env->make_function(env, 0, 0, create_loop, "create and free 1M refs", NULL);

  emacs_value args[] = { env->intern(env, "create-refs-loop"), loopf };
  env->funcall(env, env->intern(env, "fset"), 2, args);

  emacs_value simple_loop_feature = env->intern(env, "create-global-refs");
  env->funcall(env, env->intern(env, "provide"), 1, &simple_loop_feature);

  return 0;
}

[-- Attachment #3: create_global_refs_test.el --]
[-- Type: text/x-emacs-lisp, Size: 211 bytes --]

(require 'create-global-refs)

(defun run-one-iteration-and-gc ()
  (create-refs-loop)
  (garbage-collect))

(switch-to-buffer (get-buffer-create "*Messages*"))

(run-with-timer 0.1 1 'run-one-iteration-and-gc)

^ permalink raw reply	[flat|nested] 14+ messages in thread

end of thread, other threads:[~2020-08-04  8:05 UTC | newest]

Thread overview: 14+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2020-07-22 23:25 bug#42482: 27.0.91; emacs modules memory leak Milan Stanojević
2020-07-23 12:06 ` Philipp Stephani
2020-07-23 14:29   ` Milan Stanojević
2020-07-23 14:33     ` Philipp Stephani
2020-07-23 17:45       ` Eli Zaretskii
2020-07-23 19:11         ` Milan Stanojević
2020-07-25 21:43         ` Philipp Stephani
2020-08-01 12:11           ` Philipp Stephani
2020-08-01 12:37             ` Eli Zaretskii
2020-08-01 12:55               ` Nicolas Petton
2020-08-01 14:47                 ` Philipp Stephani
2020-08-02 17:25                 ` Eli Zaretskii
2020-08-03 23:02                   ` Milan Stanojević
2020-08-04  8:05                   ` Nicolas Petton

Code repositories for project(s) associated with this external index

	https://git.savannah.gnu.org/cgit/emacs.git
	https://git.savannah.gnu.org/cgit/emacs/org-mode.git

This is an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.