Output from MemUsed differs after Run(env or Eval(env, "(run)"

49 views
Skip to first unread message

Ryan Johnston

unread,
Dec 1, 2023, 11:00:32 PM12/1/23
to CLIPSESG
I'm writing a CLIPS program that asserts and retracts facts many times throughout the lifecycle of the environment. Here's a mock-up of what it does in which I've replicated the behavior:

   printf("before: %lld\n", MemUsed(mainEnv));

   Build(mainEnv, "(deftemplate foo (multislot bar))");
   Build(mainEnv, "(defrule foo-removal ?f <- (foo) => (retract ?f))");
   char name [10000];
   MultifieldBuilder *mb = CreateMultifieldBuilder(mainEnv, 10);
   FactBuilder *fb = CreateFactBuilder(mainEnv, "foo");
   for(long long int x1 = 0; x1 < 10; x1++)
   {
     for(long long int x2 = 0; x2 < 10; x2++)
     {
       for(long long int x3 = 0; x3 < 20; x3++)
       {
         sprintf(name, "name: %lld %lld %lld", x3, x1, x2);
         MBAppendString(mb, name);
       }

       FBPutSlotMultifield(fb, "bar", MBCreate(mb));
       FBAssert(fb);
     }

     /*
     Uncomment these two lines,
     and then comment out the
     Run on the line below it
     CLIPSValue cv;
     Eval(mainEnv, "(run)", &cv);
     */
     Run(mainEnv, -1);
   }

   FBDispose(fb);
   MBDispose(mb);

   ReleaseMem(mainEnv, -1);
   printf("after: %lld\n", MemUsed(mainEnv));

This replaces the content in the main.c file in the clips core source version 6.4.1. When I run the compiled program with make && ./clips, this is the output:

$ ./clips
before: 1708746
after: 1856464

When I switch to using Eval instead of Run, this is the output:

$ ./clips
before: 1708746
after: 1700728

I notice in `htop` that the memory used by this process increases over time steadily when I use Run, so I suspect this may demonstrate memory not being freed rather than an issue with the MemUsed function. Also note my usage of ReleaseMem just to make sure.

I discovered this in a program I'm writing that uses a long-running CLIPS environment in memory. When I use Eval in the manner described above, the program appears to take up less memory over the course of its lifetime. Am I missing something?

CLIPS Support

unread,
Dec 2, 2023, 3:03:33 PM12/2/23
to CLIPSESG
There's a missing call to clean up garbage when Run is called from an application embedding CLIPS. In engine.c in the Run function replace

   GCBlockEnd(theEnv,&gcb);

   CallPeriodicTasks(theEnv);


with

   GCBlockEnd(theEnv,&gcb);

   if (EvaluationData(theEnv)->CurrentExpression == NULL)

     { CleanCurrentGarbageFrame(theEnv,NULL); }

   CallPeriodicTasks(theEnv);


Ryan Johnston

unread,
Dec 2, 2023, 5:51:25 PM12/2/23
to clip...@googlegroups.com
That did it. Thanks, Gary.


--
You received this message because you are subscribed to the Google Groups "CLIPSESG" group.
To post to this group, send email to CLIP...@googlegroups.com
For more options, visit this group at http://groups.google.com/group/CLIPSESG?hl=en
 
--> IF YOU NO LONGER WANT TO RECEIVE EMAIL <--
Visit this group at http://groups.google.com/group/CLIPSESG?hl=en
Click on "Edit my membership" link.
Select the "No Email" radio button.
Click the "Save these settings" button.

--> IF YOU WANT TO UNSUBSCRIBE <--
Visit this group at http://groups.google.com/group/CLIPSESG?hl=en
Sign in
Click on "Edit my membership" link.
Click the "Unsubscribe" button.
Note: This appears to be the most reliable way to unsubscribe
 
Alternately, send email to CLIPSESG-u...@googlegroups.com. You will receive an email which you must respond to as well to unsubscribe. Clicking the link mentioned in the unsubscribe reply does not appear to work reliably.
---
You received this message because you are subscribed to the Google Groups "CLIPSESG" group.
To unsubscribe from this group and stop receiving emails from it, send an email to clipsesg+u...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/clipsesg/23cf716e-dc37-4f0f-9e86-a6bb81894b8bn%40googlegroups.com.
Reply all
Reply to author
Forward
0 new messages