You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: modules/ROOT/pages/extending-neo4j/procedures.adoc
+77
Original file line number
Diff line number
Diff line change
@@ -190,9 +190,86 @@ The classes that can be injected are:
190
190
* `Transaction`
191
191
//* `SecurityContext`
192
192
//* `ProcedureTransaction`
193
+
//* `ProcedureMemory` Candidate for public API but not stable yet
193
194
194
195
All of the above classes are considered safe and future-proof and do not compromise the security of the database.
195
196
Several unsupported (restricted) classes can also be injected and can be changed with little or no notice.
196
197
Procedures written to use these restricted APIs are not loaded by default, and you need to use the `dbms.security.procedures.unrestricted` to load unsafe procedures.
197
198
Read more about this config setting in link:{neo4j-docs-base-uri}/operations-manual/{page-version}/security/securing-extensions[Operations Manual -> Securing extensions].
198
199
200
+
[[memory-resource-tracking]]
201
+
== Memory Resource Tracking
202
+
203
+
[NOTE]
204
+
====
205
+
The memory resource tracking API for the procedure framework is available for preview.
206
+
Future versions of Neo4j might contain breaking changes to this API.
207
+
====
208
+
209
+
If your procedure or function allocates significant amounts of heap memory, you can register allocations to count towards the configured transaction limits, see link:{neo4j-docs-base-uri}/operations-manual/{page-version}/performance/memory-configuration/#memory-configuration-limit-transaction-memory[Operations Manual -> Limit transaction memory usage] for more information.
210
+
This allows you to avoid `OutOfMemory` errors that cause database restarts.
211
+
Memory allocations also show up in query profiles.
212
+
213
+
To do this you need to inject `org.neo4j.procedure.memory.ProcedureMemory` as a field in your procedure/function class.
214
+
`ProcedureMemory` has various methods to allow you to register allocations.
215
+
For example (see javadocs for a full reference):
216
+
217
+
* `ProcedureMemoryTracker newTracker()` creates a new memory resource tracker that is bound to the current transaction.
218
+
* `HeapEstimator heapEstimator()` estimates the heap size of classes and instances.
219
+
* `HeapTrackingCollectionFactory collections()` lets you create collections that have built-in memory tracking of their internal structure.
220
+
221
+
It's usually difficult and time-consuming to implement memory resource tracking.
222
+
These are a few considerations and caveats that are worth keeping in mind:
223
+
224
+
- Limit the scope of the memory management.
225
+
Focus only on parts that can grow significantly in memory and ignore minor underestimation.
226
+
- Beware of overestimation by registering allocations of the same instance multiple times.
227
+
You can add reference counting or other mechanisms to avoid overestimation if that is a concern.
228
+
- It's common not to know the size of an instance before it has been allocated, which may lead you to register allocations after they have already been made.
229
+
The memory tracker implementation tries to prevent this by always pre-registering a certain amount of memory in the internal memory pools.
230
+
- It's cumbersome in Java to know when an instance has been garbage-collected.
231
+
Typically, you register the release of memory at the point when it's possible for that memory to be garbage-collected.
232
+
To account for this, memory trackers may internally choose not to register the release of memory instantaneously.
233
+
- Testing memory resource tracking can be difficult.
234
+
One approach is to use a third-party library, like JAMM (Java Agent for Memory Measurements), and assert that the estimates are close enough for some given input.
235
+
236
+
237
+
.A basic example of memory resource tracking in user defined procedures.
0 commit comments