@@ -87,7 +87,10 @@ func ReadGCStats(stats *GCStats) {
87
87
// SetGCPercent returns the previous setting.
88
88
// The initial setting is the value of the GOGC environment variable
89
89
// at startup, or 100 if the variable is not set.
90
- // A negative percentage disables garbage collection.
90
+ // A negative percentage disables triggering garbage collection
91
+ // based on the ratio of fresh allocation to previously live heap.
92
+ // However, GC can still be explicitly triggered by runtime.GC and
93
+ // similar functions, or by the maximum heap size set by SetMaxHeap.
91
94
func SetGCPercent (percent int ) int {
92
95
return int (setGCPercent (int32 (percent )))
93
96
}
@@ -166,3 +169,114 @@ func WriteHeapDump(fd uintptr)
166
169
// If SetTraceback is called with a level lower than that of the
167
170
// environment variable, the call is ignored.
168
171
func SetTraceback (level string )
172
+
173
+ // GCPolicy reports the garbage collector's policy for controlling the
174
+ // heap size and scheduling garbage collection work.
175
+ type GCPolicy struct {
176
+ // GCPercent is the current value of GOGC, as set by the GOGC
177
+ // environment variable or SetGCPercent.
178
+ //
179
+ // If triggering GC by relative heap growth is disabled, this
180
+ // will be -1.
181
+ GCPercent int
182
+
183
+ // MaxHeapBytes is the current soft heap limit set by
184
+ // SetMaxHeap, in bytes.
185
+ //
186
+ // If there is no heap limit set, this will be ^uintptr(0).
187
+ MaxHeapBytes uintptr
188
+
189
+ // AvailGCPercent is the heap space available for allocation
190
+ // before the next GC, as a percent of the heap used at the
191
+ // end of the previous garbage collection. It measures memory
192
+ // pressure and how hard the garbage collector must work to
193
+ // achieve the heap size goals set by GCPercent and
194
+ // MaxHeapBytes.
195
+ //
196
+ // For example, if AvailGCPercent is 100, then at the end of
197
+ // the previous garbage collection, the space available for
198
+ // allocation before the next GC was the same as the space
199
+ // used. If AvailGCPercent is 20, then the space available is
200
+ // only a 20% of the space used.
201
+ //
202
+ // AvailGCPercent is directly comparable with GCPercent.
203
+ //
204
+ // If AvailGCPercent >= GCPercent, the garbage collector is
205
+ // not under pressure and can amortize the cost of garbage
206
+ // collection by allowing the heap to grow in proportion to
207
+ // how much is used.
208
+ //
209
+ // If AvailGCPercent < GCPercent, the garbage collector is
210
+ // under pressure and must run more frequently to keep the
211
+ // heap size under MaxHeapBytes. Smaller values of
212
+ // AvailGCPercent indicate greater pressure. In this case, the
213
+ // application should shed load and reduce its live heap size
214
+ // to relieve memory pressure.
215
+ //
216
+ // AvailGCPercent is always >= 0.
217
+ AvailGCPercent int
218
+ }
219
+
220
+ // SetMaxHeap sets a soft limit on the size of the Go heap and returns
221
+ // the previous setting. By default, there is no limit.
222
+ //
223
+ // If a max heap is set, the garbage collector will endeavor to keep
224
+ // the heap size under the specified size, even if this is lower than
225
+ // would normally be determined by GOGC (see SetGCPercent).
226
+ //
227
+ // Whenever the garbage collector's scheduling policy changes as a
228
+ // result of this heap limit (that is, the result that would be
229
+ // returned by ReadGCPolicy changes), the garbage collector will send
230
+ // to the notify channel. This is a non-blocking send, so this should
231
+ // be a single-element buffered channel, though this is not required.
232
+ // Only a single channel may be registered for notifications at a
233
+ // time; SetMaxHeap replaces any previously registered channel.
234
+ //
235
+ // The application is strongly encouraged to respond to this
236
+ // notification by calling ReadGCPolicy and, if AvailGCPercent is less
237
+ // than GCPercent, shedding load to reduce its live heap size. Setting
238
+ // a maximum heap size limits the garbage collector's ability to
239
+ // amortize the cost of garbage collection when the heap reaches the
240
+ // heap size limit. This is particularly important in
241
+ // request-processing systems, where increasing pressure on the
242
+ // garbage collector reduces CPU time available to the application,
243
+ // making it less able to complete work, leading to even more pressure
244
+ // on the garbage collector. The application must shed load to avoid
245
+ // this "GC death spiral".
246
+ //
247
+ // The limit set by SetMaxHeap is soft. If the garbage collector would
248
+ // consume too much CPU to keep the heap under this limit (leading to
249
+ // "thrashing"), it will allow the heap to grow larger than the
250
+ // specified max heap.
251
+ //
252
+ // The heap size does not include everything in the process's memory
253
+ // footprint. Notably, it does not include stacks, C-allocated memory,
254
+ // or many runtime-internal structures.
255
+ //
256
+ // To disable the heap limit, pass ^uintptr(0) for the bytes argument.
257
+ // In this case, notify can be nil.
258
+ //
259
+ // To depend only on the heap limit to trigger garbage collection,
260
+ // call SetGCPercent(-1) after setting a heap limit.
261
+ func SetMaxHeap (bytes uintptr , notify chan <- struct {}) uintptr {
262
+ if bytes == ^ uintptr (0 ) {
263
+ return gcSetMaxHeap (bytes , nil )
264
+ }
265
+ if notify == nil {
266
+ panic ("SetMaxHeap requires a non-nil notify channel" )
267
+ }
268
+ return gcSetMaxHeap (bytes , notify )
269
+ }
270
+
271
+ // gcSetMaxHeap is provided by package runtime.
272
+ func gcSetMaxHeap (bytes uintptr , notify chan <- struct {}) uintptr
273
+
274
+ // ReadGCPolicy reads the garbage collector's current policy for
275
+ // managing the heap size. This includes static settings controlled by
276
+ // the application and dynamic policy determined by heap usage.
277
+ func ReadGCPolicy (gcp * GCPolicy ) {
278
+ gcp .GCPercent , gcp .MaxHeapBytes , gcp .AvailGCPercent = gcReadPolicy ()
279
+ }
280
+
281
+ // gcReadPolicy is provided by package runtime.
282
+ func gcReadPolicy () (gogc int , maxHeap uintptr , egogc int )
0 commit comments