Analysing memory in a live image

This page describes a small model for investigating memory in a running image, by people or by agents. It complements Analysing the size of the image, which explains what a saved image contains. Two questions are useful when memory grows: what kinds of objects take the space, and who holds them. A space tally answers the first question quickly. The memory-space model answers the second one: it walks the objects reachable from chosen roots and attributes their memory to the root that reaches them first. Neither answer is exact, and they do not have to be: the goal is to see quickly where space is spent.

A few objects, each with one job: - a GtMemorySpaceRootsDefinition GtMemorySpaceDefinition << #GtMemorySpaceRootsDefinition slots: { #rootGroups . #boundary . #expectedObjectsCount }; package: 'GToolkit-Utility-MemorySpace' says where to look: ordered groups of roots, where to stop, and which kinds of objects to tag. It is configured in a snippet, without writing a class; - a GtMemorySpaceRootGroup Object << #GtMemorySpaceRootGroup slots: { #label . #category . #rootsComputation . #attribution . #extraStopClasses }; package: 'GToolkit-Utility-MemorySpace' is a named group of roots (GtMemorySpaceRoot Object << #GtMemorySpaceRoot slots: { #label . #object . #categoryName }; package: 'GToolkit-Utility-MemorySpace' ), computed when the estimation runs. Its class side has ready-made groups for the class-side state of all classes (by package) and for the globals; - a GtMemorySpaceBoundary Object << #GtMemorySpaceBoundary slots: { #stopClasses . #tags }; package: 'GToolkit-Utility-MemorySpace' says at which classes a walk stops (by default code entities and processes) and which classes are tagged; - a GtMemorySpaceEstimator Object << #GtMemorySpaceEstimator slots: { #boundary . #visited . #objectsCountAtRelease . #stack . #classTraversals . #walkTagSizes . #hasWalkTags }; package: 'GToolkit-Utility-MemorySpace' walks the objects, counting each object once, by identity; - a GtMemorySpaceEstimate Object << #GtMemorySpaceEstimate slots: { #categoryEstimates . #rootEstimates . #totalBytes . #totalObjectsCount }; package: 'GToolkit-Utility-MemorySpace' holds the result: memory per category (where it comes from), per tag (what kind of objects), per root and per group of roots. Groups are estimated in order. An object reachable from several groups is counted in the first one, so put specific roots first and roots that reach almost everything last.

A space tally counts the instances of every class and their memory, per class, package and repository. It needs no knowledge of the object model and takes about a second, even with a heap of a few gigabytes. It does not say why the objects are alive.

GtSpaceTally new
	systemWideSpaceTally;
	yourself
  

Describe the roots in the order in which they should claim memory, then inspect the estimation. The Root groups and Roots views show who holds what; Categories split the class-side state by package. Walking a large heap takes minutes (about 150 thousand objects per second in a heap of 4 GB), so start with a few groups.

"Who holds memory: the Lepiter databases first, then the class-side state of all classes and the globals.
The walk stops at code entities, processes and Bloc spaces."
| definition |
definition := GtMemorySpaceRootsDefinition new
	roots: 'Lepiter databases'
		are: [ LeDatabasesRegistry default currentLoadedDefaultLogicalDatabase
				ifNil: [ #() ]
				ifNotNil: [ :aLogicalDatabase | aLogicalDatabase databases ] ];
	rootGroup: GtMemorySpaceRootGroup classSideState;
	rootGroup: GtMemorySpaceRootGroup globals;
	stopAtInstancesOf: { BlSpace };
	tag: #string instancesOf: { ByteString. WideString };
	expectedObjects: 5000000;
	yourself.
definition estimate
  

Combine both: take the largest classes of a space tally and tag their instances. The Tags view of the estimate then shows, for each class, the memory of its instances in each category. What the space tally counts and no root reaches is held elsewhere, for example by processes or by the virtual machine. The default boundary stops at processes, so their stacks (Context objects) are not walked; to include them, use boundary: (GtMemorySpaceBoundary new stopAtCodeEntities; yourself).

| largestClasses definition |
largestClasses := ((GtSpaceTally new systemWideSpaceTally asArray
	sort: [ :a :b | a spaceForInstances > b spaceForInstances ])
		first: 5)
		collect: [ :each | Smalltalk globals at: each analyzedClassName ].
definition := GtMemorySpaceRootsDefinition new
	rootGroup: GtMemorySpaceRootGroup classSideState;
	rootGroup: GtMemorySpaceRootGroup globals;
	tagEachOf: largestClasses;
	expectedObjects: 5000000;
	yourself.
definition estimate
  

To look inside one object, make it the only root. A group can also split its roots with an attribution, for example a GtMemorySpaceSlotsAttribution GtMemorySpaceAttribution << #GtMemorySpaceSlotsAttribution slots: { #objectCategory . #objectAttribution . #slotRules . #otherSlotsAttribution }; package: 'GToolkit-Utility-MemorySpace' that sends each slot to its own category, when the object model is stable enough for slot names.

- Order matters. In a running image, the Bloc universes reach almost every object through the UI. Estimate specific roots first, or stop at UI classes such as BlSpace and BlElement. - Results are estimates. Memory reachable from several roots goes to the first one; weak references and the keys of ephemerons are not followed; the walk stops at code entities and, by default, at processes. - Cost. The time grows with the number of objects reached, and the set of visited objects needs memory too. Use expectedObjects: for large walks. - A long-running image is not a saved image. Its heap also holds the objects of the UI, of tools and of the user, and can be many times larger than the image file. - For agents. Run long estimations in a background process at a low priority (for example forkAt: 20), and write a summary to a file or keep the estimation in a variable. GtMemorySpaceViewsCheck Object << #GtMemorySpaceViewsCheck slots: { #object . #viewChecks }; package: 'GToolkit-Utility-MemorySpace' checks that the views of a result can be computed.

GtImageStateMemoryMetrics Object << #GtImageStateMemoryMetrics slots: { #definition . #estimation . #codeEntitiesSize . #imageSizeInMemory . #imageFileSize . #focusGroups }; package: 'Lepiter-MemoryMetrics' applies the model to the whole image: the objects that are not code entities, attributed to the GT Book, the code index, the other Lepiter databases, the class-side state of all classes and the globals. Code entities are measured by GtMemoryMetrics Object << #GtMemoryMetrics slots: { #cache }; package: 'GToolkit-Releaser' , and what no root reaches is reported as not reached. To explain the size of the image on disk, compute it in a freshly started image, or in the build before saving.

GtImageStateMemoryMetrics default
  

In a running image almost everything reaches almost everything, through the UI, processes and announcers, so a tool's memory usually goes to whatever root reaches it first. Two questions help: - What does it hold? Add a focus group: it is estimated before all other groups and stops at the shared hubs of the image (processes, Bloc elements and spaces, the async thread pool), so that it gets its own objects, for example the history of the MCP server with its results. - What does it keep alive? A GtMemorySpaceRetention Object << #GtMemorySpaceRetention slots: { #estimation . #retainedClasses . #stoppingEstimate . #retainedEstimate . #reachedEstimate . #classRetentions . #sharingEstimate }; package: 'GToolkit-Utility-MemorySpace' splits the memory reached through the instances of some classes in two: what is reached only through them (retained, freed without them) and what is also reached from other places (shared, it would stay). Its Classes view shows the instances of each class and the memory retained through them. The retained part is measured in a single walk, so it does not depend on changes of the heap between walks.

"What the MCP server holds, and what only it keeps alive. Each walk takes about a minute."
| metrics |
metrics := GtImageStateMemoryMetrics focusedOn: (GtMemorySpaceRootGroup
	label: 'MCP server'
	roots: [ { GtLMcpServer classPool at: #Current } ]).
metrics retentionOf: { GtLMcpServer. GtLToolMessage. GtLMcpChatAdapter }