12
12
*/
13
13
public class BufferRecyclers
14
14
{
15
- /**
16
- * System property that is checked to see if recycled buffers (see {@link BufferRecycler})
17
- * should be tracked, for purpose of forcing release of all such buffers, typically
18
- * during major classloading.
19
- */
20
- public final static String SYSTEM_PROPERTY_TRACK_REUSABLE_BUFFERS
21
- = "tools.jackson.core.util.BufferRecyclers.trackReusableBuffers" ;
22
-
23
- /*
24
- /**********************************************************
25
- /* Life-cycle
26
- /**********************************************************
27
- */
28
-
29
- /**
30
- * Flag that indicates whether {@link BufferRecycler} instances should be tracked.
31
- */
32
- private final static ThreadLocalBufferManager _bufferRecyclerTracker ;
33
- static {
34
- boolean trackReusableBuffers = false ;
35
- try {
36
- trackReusableBuffers = "true" .equals (System .getProperty (SYSTEM_PROPERTY_TRACK_REUSABLE_BUFFERS ));
37
- } catch (SecurityException e ) { }
38
-
39
- _bufferRecyclerTracker = trackReusableBuffers ? ThreadLocalBufferManager .instance () : null ;
40
- }
41
-
42
- /*
43
- /**********************************************************
44
- /* BufferRecyclers for parsers, generators
45
- /**********************************************************
46
- */
47
-
48
- /**
49
- * This <code>ThreadLocal</code> contains a {@link java.lang.ref.SoftReference}
50
- * to a {@link BufferRecycler} used to provide a low-cost
51
- * buffer recycling between reader and writer instances.
52
- */
53
- final protected static ThreadLocal <SoftReference <BufferRecycler >> _recyclerRef
54
- = new ThreadLocal <SoftReference <BufferRecycler >>();
55
-
56
- /**
57
- * Main accessor to call for accessing possibly recycled {@link BufferRecycler} instance.
58
- *
59
- * @return {@link BufferRecycler} to use
60
- *
61
- * @deprecated Since 2.16 should use {@link BufferRecyclerPool} abstraction instead
62
- * of calling static methods of this class
63
- */
64
- @ Deprecated // since 2.16
65
- public static BufferRecycler getBufferRecycler ()
66
- {
67
- SoftReference <BufferRecycler > ref = _recyclerRef .get ();
68
- BufferRecycler br = (ref == null ) ? null : ref .get ();
69
-
70
- if (br == null ) {
71
- br = new BufferRecycler ();
72
- if (_bufferRecyclerTracker != null ) {
73
- ref = _bufferRecyclerTracker .wrapAndTrack (br );
74
- } else {
75
- ref = new SoftReference <BufferRecycler >(br );
76
- }
77
- _recyclerRef .set (ref );
78
- }
79
- return br ;
80
- }
81
-
82
- /**
83
- * Specialized method that will release all recycled {@link BufferRecycler} if
84
- * (and only if) recycler tracking has been enabled
85
- * (see {@link #SYSTEM_PROPERTY_TRACK_REUSABLE_BUFFERS}).
86
- * This method is usually called on shutdown of the container like Application Server
87
- * to ensure that no references are reachable via {@link ThreadLocal}s as this may cause
88
- * unintentional retention of sizable amounts of memory. It may also be called regularly
89
- * if GC for some reason does not clear up {@link SoftReference}s aggressively enough.
90
- *
91
- * @return Number of buffers released, if tracking enabled (zero or more); -1 if tracking not enabled.
92
- *
93
- * @since 2.9.6
94
- */
95
- public static int releaseBuffers () {
96
- if (_bufferRecyclerTracker != null ) {
97
- return _bufferRecyclerTracker .releaseBuffers ();
98
- }
99
- return -1 ;
100
- }
101
-
102
15
/*
103
16
/**********************************************************************
104
- /* Default BufferRecyclerPool implementations
17
+ /* Accessor for default BufferRecyclerPool implementations
105
18
/**********************************************************************
106
19
*/
107
20
@@ -113,6 +26,12 @@ public static BufferRecyclerPool nopRecyclerPool() {
113
26
return NonRecyclingRecyclerPool .INSTANCE ;
114
27
}
115
28
29
+ /*
30
+ /**********************************************************************
31
+ /* Standard BufferRecyclerPool implementations
32
+ /**********************************************************************
33
+ */
34
+
116
35
/**
117
36
* Default {@link BufferRecyclerPool} implementation that uses
118
37
* {@link ThreadLocal} for recycling instances.
@@ -121,8 +40,34 @@ public static class ThreadLocalRecyclerPool
121
40
implements BufferRecyclerPool
122
41
{
123
42
private static final long serialVersionUID = 1L ;
43
+
44
+ /**
45
+ * This <code>ThreadLocal</code> contains a {@link java.lang.ref.SoftReference}
46
+ * to a {@link BufferRecycler} used to provide a low-cost
47
+ * buffer recycling between reader and writer instances.
48
+ */
49
+ protected final static ThreadLocal <SoftReference <BufferRecycler >> _recyclerRef
50
+ = new ThreadLocal <SoftReference <BufferRecycler >>();
51
+
124
52
public final static ThreadLocalRecyclerPool INSTANCE = new ThreadLocalRecyclerPool ();
125
53
54
+ /**
55
+ * Main accessor to call for accessing possibly recycled {@link BufferRecycler} instance.
56
+ *
57
+ * @return {@link BufferRecycler} to use
58
+ */
59
+ private BufferRecycler getBufferRecycler ()
60
+ {
61
+ SoftReference <BufferRecycler > ref = _recyclerRef .get ();
62
+ BufferRecycler br = (ref == null ) ? null : ref .get ();
63
+
64
+ if (br == null ) {
65
+ br = new BufferRecycler ();
66
+ _recyclerRef .set (ref );
67
+ }
68
+ return br ;
69
+ }
70
+
126
71
@ Override
127
72
public BufferRecycler acquireBufferRecycler (TokenStreamFactory forFactory ) {
128
73
return getBufferRecycler ();
0 commit comments