GeezerVison is showing github.com — Enforce io.netty.maxDirectMemory accounting on all Java versions · netty/netty@ce115a9
File treeExpand file treeCollapse file tree Open diff view settings buffer/src/main/java/io/netty/buffer main/java/io/netty/util/internal test/java/io/netty/util/internal
Expand file treeCollapse file tree Open diff view settings Collapse file Copy file name to clipboardExpand all lines: buffer/src/main/java/io/netty/buffer/UnpooledByteBufAllocator.java +6-5Lines changed: 6 additions & 5 deletions | Original file line number | Diff line number | Diff line change |
|---|
@@ -68,7 +68,7 @@ public UnpooledByteBufAllocator(boolean preferDirect, boolean disableLeakDetecto | 68 | 68 | * @param disableLeakDetector {@code true} if the leak-detection should be disabled completely for this | 69 | 69 | * allocator. This can be useful if the user just want to depend on the GC to handle | 70 | 70 | * direct buffers when not explicit released. | 71 | | - * @param tryNoCleaner {@code true} if we should try to use {@link PlatformDependent#allocateDirectNoCleaner(int)} | | 71 | + * @param tryNoCleaner {@code true} if we should try to use {@link PlatformDependent#allocateDirect(int)} | 72 | 72 | * to allocate direct memory. | 73 | 73 | */ | 74 | 74 | public UnpooledByteBufAllocator(boolean preferDirect, boolean disableLeakDetector, boolean tryNoCleaner) { | @@ -195,10 +195,11 @@ protected CleanableDirectBuffer allocateDirectBuffer(int capacity, boolean permi | 195 | 195 | } | 196 | 196 |
| 197 | 197 | @Override | 198 | | - CleanableDirectBuffer reallocateDirect(CleanableDirectBuffer oldBuffer, int initialCapacity) { | 199 | | - int capacity = oldBuffer.buffer().capacity(); | 200 | | - CleanableDirectBuffer buffer = super.reallocateDirect(oldBuffer, initialCapacity); | 201 | | - return new DecrementingCleanableDirectBuffer(alloc(), buffer, buffer.buffer().capacity() - capacity); | | 198 | + CleanableDirectBuffer reallocateDirect(CleanableDirectBuffer oldBuffer, int newCapacity) { | | 199 | + int oldCapacity = oldBuffer.buffer().capacity(); | | 200 | + CleanableDirectBuffer buffer = super.reallocateDirect(oldBuffer, newCapacity); | | 201 | + return new DecrementingCleanableDirectBuffer( | | 202 | + alloc(), buffer, buffer.buffer().capacity() - oldCapacity); | 202 | 203 | } | 203 | 204 | } | 204 | 205 |
| |
Collapse file Copy file name to clipboardExpand all lines: buffer/src/main/java/io/netty/buffer/UnpooledUnsafeNoCleanerDirectByteBuf.java +2-12Lines changed: 2 additions & 12 deletions | Original file line number | Diff line number | Diff line change |
|---|
@@ -25,23 +25,13 @@ class UnpooledUnsafeNoCleanerDirectByteBuf extends UnpooledUnsafeDirectByteBuf { | 25 | 25 | super(alloc, initialCapacity, maxCapacity); | 26 | 26 | } | 27 | 27 |
| 28 | | - @Override | 29 | | - protected CleanableDirectBuffer allocateDirectBuffer(int capacity) { | 30 | | - return PlatformDependent.allocateDirectBufferNoCleaner(capacity); | 31 | | - } | 32 | | - | 33 | | - @Override | 34 | | - protected CleanableDirectBuffer allocateDirectBuffer(int capacity, boolean ignorePermitExpensiveClean) { | 35 | | - return PlatformDependent.allocateDirectBufferNoCleaner(capacity); | 36 | | - } | 37 | | - | 38 | 28 | @Override | 39 | 29 | protected ByteBuffer allocateDirect(int initialCapacity) { | 40 | 30 | throw new UnsupportedOperationException(); | 41 | 31 | } | 42 | 32 |
| 43 | | - CleanableDirectBuffer reallocateDirect(CleanableDirectBuffer oldBuffer, int initialCapacity) { | 44 | | - return PlatformDependent.reallocateDirectBufferNoCleaner(oldBuffer, initialCapacity); | | 33 | + CleanableDirectBuffer reallocateDirect(CleanableDirectBuffer oldBuffer, int newCapacity) { | | 34 | + return PlatformDependent.reallocateDirect(oldBuffer, newCapacity); | 45 | 35 | } | 46 | 36 |
| 47 | 37 | @Override | |
Collapse file Copy file name to clipboardExpand all lines: common/src/main/java/io/netty/util/internal/Cleaner.java +20Lines changed: 20 additions & 0 deletions | Original file line number | Diff line number | Diff line change |
|---|
@@ -30,6 +30,26 @@ interface Cleaner { | 30 | 30 | */ | 31 | 31 | CleanableDirectBuffer allocate(int capacity); | 32 | 32 |
| | 33 | + /** | | 34 | + * Reallocate a direct buffer with a new capacity. The old buffer is consumed and | | 35 | + * must not be used after this call. | | 36 | + * <p> | | 37 | + * The default implementation allocates a new buffer, copies the data, and frees the old one. | | 38 | + * Implementations may override this to provide more efficient reallocation (e.g. via | | 39 | + * {@code Unsafe.reallocateMemory}). | | 40 | + */ | | 41 | + default CleanableDirectBuffer reallocate(CleanableDirectBuffer old, int newCapacity) { | | 42 | + CleanableDirectBuffer newBuf = allocate(newCapacity); | | 43 | + ByteBuffer oldBB = old.buffer(); | | 44 | + ByteBuffer newBB = newBuf.buffer(); | | 45 | + int bytesToCopy = Math.min(oldBB.capacity(), newCapacity); | | 46 | + oldBB.position(0).limit(bytesToCopy); | | 47 | + newBB.position(0).limit(bytesToCopy); | | 48 | + newBB.put(oldBB).clear(); | | 49 | + old.clean(); | | 50 | + return newBuf; | | 51 | + } | | 52 | + | 33 | 53 | /** | 34 | 54 | * Free a direct {@link ByteBuffer} if possible | 35 | 55 | * | |
Collapse file Copy file name to clipboardExpand all lines: common/src/main/java/io/netty/util/internal/CleanerJava24Linker.java +11-1Lines changed: 11 additions & 1 deletion | Original file line number | Diff line number | Diff line change |
|---|
@@ -236,11 +236,19 @@ private static final class CleanableDirectBufferImpl implements CleanableDirectB | 236 | 236 | private final long memoryAddress; | 237 | 237 |
| 238 | 238 | private CleanableDirectBufferImpl(int capacity) { | 239 | | - long addr = malloc(capacity); | | 239 | + PlatformDependent.incrementMemoryCounter(capacity); | | 240 | + long addr; | | 241 | + try { | | 242 | + addr = malloc(capacity); | | 243 | + } catch (Throwable e) { | | 244 | + PlatformDependent.decrementMemoryCounter(capacity); | | 245 | + throw e; | | 246 | + } | 240 | 247 | try { | 241 | 248 | memoryAddress = addr; | 242 | 249 | buffer = (ByteBuffer) INVOKE_CREATE_BYTEBUFFER.invokeExact(addr, (long) capacity); | 243 | 250 | } catch (Throwable throwable) { | | 251 | + PlatformDependent.decrementMemoryCounter(capacity); | 244 | 252 | Error error = new Error(throwable); | 245 | 253 | try { | 246 | 254 | free(addr); | @@ -258,7 +266,9 @@ public ByteBuffer buffer() { | 258 | 266 |
| 259 | 267 | @Override | 260 | 268 | public void clean() { | | 269 | + int capacity = buffer.capacity(); | 261 | 270 | free(memoryAddress); | | 271 | + PlatformDependent.decrementMemoryCounter(capacity); | 262 | 272 | } | 263 | 273 |
| 264 | 274 | @Override | |
Collapse file Copy file name to clipboardExpand all lines: common/src/main/java/io/netty/util/internal/CleanerJava25.java +6Lines changed: 6 additions & 0 deletions | Original file line number | Diff line number | Diff line change |
|---|
@@ -169,11 +169,14 @@ static boolean isSupported() { | 169 | 169 | @SuppressWarnings("OverlyStrongTypeCast") // The cast is needed for 'invokeExact' semantics. | 170 | 170 | @Override | 171 | 171 | public CleanableDirectBuffer allocate(int capacity) { | | 172 | + PlatformDependent.incrementMemoryCounter(capacity); | 172 | 173 | try { | 173 | 174 | return (CleanableDirectBufferImpl) INVOKE_ALLOCATOR.invokeExact(capacity); | 174 | 175 | } catch (RuntimeException e) { | | 176 | + PlatformDependent.decrementMemoryCounter(capacity); | 175 | 177 | throw e; // Propagate the runtime exceptions that the Arena would normally throw. | 176 | 178 | } catch (Throwable e) { | | 179 | + PlatformDependent.decrementMemoryCounter(capacity); | 177 | 180 | throw new IllegalStateException("Unexpected allocation exception", e); | 178 | 181 | } | 179 | 182 | } | @@ -209,12 +212,15 @@ public ByteBuffer buffer() { | 209 | 212 |
| 210 | 213 | @Override | 211 | 214 | public void clean() { | | 215 | + int capacity = buffer.capacity(); | 212 | 216 | try { | 213 | 217 | closeable.close(); | 214 | 218 | } catch (RuntimeException e) { | 215 | 219 | throw e; // Propagate the runtime exceptions that Arena would normally throw. | 216 | 220 | } catch (Exception e) { | 217 | 221 | throw new IllegalStateException("Unexpected close exception", e); | | 222 | + } finally { | | 223 | + PlatformDependent.decrementMemoryCounter(capacity); | 218 | 224 | } | 219 | 225 | } | 220 | 226 |
| |
Collapse file Copy file name to clipboardExpand all lines: common/src/main/java/io/netty/util/internal/CleanerJava6.java +3Lines changed: 3 additions & 0 deletions | Original file line number | Diff line number | Diff line change |
|---|
@@ -156,6 +156,7 @@ private static final class CleanableDirectBufferImpl implements CleanableDirectB | 156 | 156 |
| 157 | 157 | private CleanableDirectBufferImpl(ByteBuffer buffer) { | 158 | 158 | this.buffer = buffer; | | 159 | + PlatformDependent.incrementMemoryCounter(buffer.capacity()); | 159 | 160 | } | 160 | 161 |
| 161 | 162 | @Override | @@ -165,7 +166,9 @@ public ByteBuffer buffer() { | 165 | 166 |
| 166 | 167 | @Override | 167 | 168 | public void clean() { | | 169 | + int capacity = buffer.capacity(); | 168 | 170 | freeDirectBufferStatic(buffer); | | 171 | + PlatformDependent.decrementMemoryCounter(capacity); | 169 | 172 | } | 170 | 173 | } | 171 | 174 | } |
Collapse file Copy file name to clipboardExpand all lines: common/src/main/java/io/netty/util/internal/CleanerJava9.java +3Lines changed: 3 additions & 0 deletions | Original file line number | Diff line number | Diff line change |
|---|
@@ -133,6 +133,7 @@ private static final class CleanableDirectBufferImpl implements CleanableDirectB | 133 | 133 |
| 134 | 134 | private CleanableDirectBufferImpl(ByteBuffer buffer) { | 135 | 135 | this.buffer = buffer; | | 136 | + PlatformDependent.incrementMemoryCounter(buffer.capacity()); | 136 | 137 | } | 137 | 138 |
| 138 | 139 | @Override | @@ -142,7 +143,9 @@ public ByteBuffer buffer() { | 142 | 143 |
| 143 | 144 | @Override | 144 | 145 | public void clean() { | | 146 | + int capacity = buffer.capacity(); | 145 | 147 | freeDirectBufferStatic(buffer); | | 148 | + PlatformDependent.decrementMemoryCounter(capacity); | 146 | 149 | } | 147 | 150 | } | 148 | 151 | } |
Collapse file Copy file name to clipboardExpand all lines: common/src/main/java/io/netty/util/internal/DirectCleaner.java +35-9Lines changed: 35 additions & 9 deletions | Original file line number | Diff line number | Diff line change |
|---|
| 20 | 20 | final class DirectCleaner implements Cleaner { | 21 | 21 | @Override | 22 | 22 | public CleanableDirectBuffer allocate(int capacity) { | 23 | | - return new CleanableDirectBufferImpl(PlatformDependent.allocateDirectNoCleaner(capacity)); | | 23 | + return new CleanableDirectBufferImpl(capacity); | | 24 | + } | | 25 | + | | 26 | + @Override | | 27 | + public CleanableDirectBuffer reallocate(CleanableDirectBuffer old, int newCapacity) { | | 28 | + int oldCapacity = old.buffer().capacity(); | | 29 | + int delta = newCapacity - oldCapacity; | | 30 | + PlatformDependent.incrementMemoryCounter(delta); | | 31 | + try { | | 32 | + ByteBuffer newBuffer = PlatformDependent0.reallocateDirectNoCleaner( | | 33 | + old.buffer(), newCapacity); | | 34 | + return new CleanableDirectBufferImpl(newBuffer); | | 35 | + } catch (Throwable e) { | | 36 | + PlatformDependent.decrementMemoryCounter(delta); | | 37 | + PlatformDependent0.throwException(e); | | 38 | + return null; | | 39 | + } | 24 | 40 | } | 25 | 41 |
| 26 | 42 | @Override | 27 | 43 | public void freeDirectBuffer(ByteBuffer buffer) { | 28 | | - PlatformDependent.freeDirectNoCleaner(buffer); | | 44 | + PlatformDependent0.freeMemory(PlatformDependent0.directBufferAddress(buffer)); | 29 | 45 | } | 30 | 46 |
| 31 | 47 | @Override | 32 | 48 | public boolean hasExpensiveClean() { | 33 | 49 | return false; | 34 | 50 | } | 35 | 51 |
| 36 | | - CleanableDirectBuffer reallocate(CleanableDirectBuffer buffer, int capacity) { | 37 | | - ByteBuffer newByteBuffer = PlatformDependent.reallocateDirectNoCleaner(buffer.buffer(), capacity); | 38 | | - return new CleanableDirectBufferImpl(newByteBuffer); | 39 | | - } | 40 | | - | 41 | 52 | private static final class CleanableDirectBufferImpl implements CleanableDirectBuffer { | 42 | 53 | private final ByteBuffer buffer; | 43 | 54 |
| 44 | | - private CleanableDirectBufferImpl(ByteBuffer buffer) { | | 55 | + // Used for normal allocation — allocates memory and increments counter | | 56 | + CleanableDirectBufferImpl(int capacity) { | | 57 | + PlatformDependent.incrementMemoryCounter(capacity); | | 58 | + try { | | 59 | + this.buffer = PlatformDependent0.allocateDirectNoCleaner(capacity); | | 60 | + } catch (Throwable e) { | | 61 | + PlatformDependent.decrementMemoryCounter(capacity); | | 62 | + PlatformDependent0.throwException(e); | | 63 | + throw new AssertionError(); // unreachable | | 64 | + } | | 65 | + } | | 66 | + | | 67 | + // Used for reallocation — memory already allocated, counter already adjusted | | 68 | + CleanableDirectBufferImpl(ByteBuffer buffer) { | 45 | 69 | this.buffer = buffer; | 46 | 70 | } | 47 | 71 |
| @@ -52,7 +76,9 @@ public ByteBuffer buffer() { | 52 | 76 |
| 53 | 77 | @Override | 54 | 78 | public void clean() { | 55 | | - PlatformDependent.freeDirectNoCleaner(buffer); | | 79 | + int capacity = buffer.capacity(); | | 80 | + PlatformDependent0.freeMemory(PlatformDependent0.directBufferAddress(buffer)); | | 81 | + PlatformDependent.decrementMemoryCounter(capacity); | 56 | 82 | } | 57 | 83 | } | 58 | 84 | } |
Collapse file Copy file name to clipboardExpand all lines: common/src/main/java/io/netty/util/internal/OutOfDirectMemoryError.java +1-1Lines changed: 1 addition & 1 deletion | Original file line number | Diff line number | Diff line change |
|---|
| 18 | 18 | import java.nio.ByteBuffer; | 19 | 19 |
| 20 | 20 | /** | 21 | | - * {@link OutOfMemoryError} that is throws if {@link PlatformDependent#allocateDirectNoCleaner(int)} can not allocate | | 21 | + * {@link OutOfMemoryError} that is throws if {@link PlatformDependent#allocateDirect(int)} can not allocate | 22 | 22 | * a new {@link ByteBuffer} due memory restrictions. | 23 | 23 | */ | 24 | 24 | public final class OutOfDirectMemoryError extends OutOfMemoryError { | |
|