diff --git a/doc/ref/debug.xml b/doc/ref/debug.xml
index a13a747892c..5d61e419e49 100644
--- a/doc/ref/debug.xml
+++ b/doc/ref/debug.xml
@@ -423,11 +423,14 @@ should be treated as an estimate.
-
+
+
In the read-eval-print loop,
- stores the number of milliseconds the last command took.
+ stores the number of milliseconds the last command
+took. and stored the number of bytes of
+memory it allocated.
@@ -444,6 +447,27 @@ in nanoseconds.
+
+
+ Tracking Memory Usage
+
+
+
+ returns the total amount of memory
+ in bytes allocated by the ⪆ memory manager since ⪆ started.
+
+
+
+
+
+
+In the read-eval-print loop,
+ stores the number of bytes of
+memoryallocated by the last completed statement.
+
+
+
+
diff --git a/doc/ref/mloop.xml b/doc/ref/mloop.xml
index 4eab302b96e..c694421b27d 100644
--- a/doc/ref/mloop.xml
+++ b/doc/ref/mloop.xml
@@ -27,6 +27,8 @@ interactive environment in which you use ⪆.
last
last2
last3
+time
+memory_allocated
previous result
The normal interaction with ⪆ happens in the so-called
read-eval-print loop.
@@ -154,8 +156,10 @@ gap> last3 + last2 * last;
]]>
Also in each statement the time spent by the last statement, whether it
-produced a value or not, is available in the variable .
-This is an integer that holds the number of milliseconds.
+produced a value or not, is available in the variable . This is an integer that holds the number of
+milliseconds. Similarly the amount of memory allocated during that
+statement (in bytes) is stored in the variable .
diff --git a/src/gap.c b/src/gap.c
index 8d8e29bce40..842c2b67495 100644
--- a/src/gap.c
+++ b/src/gap.c
@@ -148,6 +148,16 @@ UInt Last3;
*/
UInt Time;
+/****************************************************************************
+**
+*V MemoryAllocated. . . . . . . . . . . global variable 'memory_allocated'
+**
+** 'MemoryAllocated' is the global variable 'memory_allocated',
+** which is automatically assigned the amount of memory allocated while
+** executing the last command.
+*/
+UInt MemoryAllocated;
+
/****************************************************************************
**
@@ -226,6 +236,7 @@ Obj Shell ( Obj context,
Char *outFile)
{
UInt time = 0;
+ UInt mem = 0;
UInt status;
Obj evalResult;
UInt dualSemicolon;
@@ -261,8 +272,10 @@ Obj Shell ( Obj context,
while ( 1 ) {
/* start the stopwatch */
- if (setTime)
- time = SyTime();
+ if (setTime) {
+ time = SyTime();
+ mem = SizeAllBags;
+ }
/* read and evaluate one command */
STATE(Prompt) = prompt;
@@ -339,8 +352,14 @@ Obj Shell ( Obj context,
}
/* stop the stopwatch */
- if (setTime)
+ if (setTime) {
AssGVar( Time, INTOBJ_INT( SyTime() - time ) );
+ /* This should be correct for small allocated totals at least,
+ even on 32 bits, but this functionality will never be very useful
+ on 32 bits */
+ AssGVar(MemoryAllocated,
+ INTOBJ_INT((SizeAllBags - mem) % (1L << NR_SMALL_INT_BITS)));
+ }
if (STATE(UserHasQuit))
{
@@ -1944,6 +1963,14 @@ Obj FuncSHALLOW_SIZE (
return ObjInt_UInt( SIZE_BAG( obj ) );
}
+/****************************************************************************
+**
+*F FuncTotalMemoryAllocated( ) .expert function 'TotalMemoryAllocated'
+*/
+
+Obj FuncTotalMemoryAllocated( Obj self ) {
+ return INTOBJ_INT(SizeAllBags);
+}
/****************************************************************************
**
@@ -2871,6 +2898,7 @@ static StructGVarFunc GVarFuncs [] = {
GVAR_FUNC(GASMAN_MESSAGE_STATUS, 0, ""),
GVAR_FUNC(GASMAN_LIMITS, 0, ""),
GVAR_FUNC(SHALLOW_SIZE, 1, "object"),
+ GVAR_FUNC(TotalMemoryAllocated, 0, ""),
GVAR_FUNC(TNUM_OBJ, 1, "object"),
GVAR_FUNC(TNUM_OBJ_INT, 1, "object"),
GVAR_FUNC(OBJ_HANDLE, 1, "object"),
@@ -2969,7 +2997,9 @@ static Int PostRestore (
Last2 = GVarName( "last2" );
Last3 = GVarName( "last3" );
Time = GVarName( "time" );
+ MemoryAllocated = GVarName( "memory_allocated" );
AssGVar(Time, INTOBJ_INT(0));
+ AssGVar(MemoryAllocated, INTOBJ_INT(0));
QUITTINGGVar = GVarName( "QUITTING" );
/* return success */