https://kotlinlang.org logo
#ktor
Title
# ktor
d

Dmitriy Tarasevich

12/11/2023, 1:44 AM
I'm experiencing some very large memory leaks on iOS Ktor: 2.3.7 kotlin: 1.9.20 Seemingly every response leaks objects After a couple requests, profiling memory usage on iOS I see thousands of the following all pointing to ktor
Copy code
kotlin::std_support::aligned_malloc(unsigned long, unsigned long)	
kfun:kotlinx.cinterop.nativeMemUtils#allocRaw(kotlin.Long;kotlin.Int){}kotlin.native.internal.NativePtr	
kfun:kotlinx.cinterop.nativeMemUtils#alloc(kotlin.Long;kotlin.Int){}kotlinx.cinterop.NativePointed	
kfun:kotlinx.cinterop.nativeHeap#alloc(kotlin.Long;kotlin.Int){}kotlinx.cinterop.NativePointed	
kfun:kotlinx.cinterop.NativePlacement#alloc(kotlin.Long;kotlin.Int){}kotlinx.cinterop.NativePointed-trampoline	
<inlined-out:allocArray>	
Worker::processQueueElement(bool)	
thread_start
Searching this slack, i have seen a few people with similar experiences as well as a youtube track issue here with the exact same trace. Any suggestions?
Copy code
kotlin::std_support::aligned_malloc(unsigned long, unsigned long)	
Kotlin_interop_malloc	
kfun:kotlinx.cinterop.nativeMemUtils#allocRaw(kotlin.Long;kotlin.Int){}kotlin.native.internal.NativePtr	
kfun:kotlinx.cinterop.nativeMemUtils#alloc(kotlin.Long;kotlin.Int){}kotlinx.cinterop.NativePointed	
kfun:kotlinx.cinterop.nativeHeap#alloc(kotlin.Long;kotlin.Int){}kotlinx.cinterop.NativePointed	
kfun:kotlinx.cinterop.NativePlacement#alloc(kotlin.Long;kotlin.Int){}kotlinx.cinterop.NativePointed-trampoline	
<inlined-out:allocArray>	
kfun:io.ktor.utils.io.bits.PlacementAllocator#alloc(kotlin.Long){}io.ktor.utils.io.bits.Memory	
kfun:io.ktor.utils.io.bits.PlacementAllocator#alloc(<http://kotlin.Int|kotlin.Int>){}io.ktor.utils.io.bits.Memory	
kfun:io.ktor.utils.io.bits.DefaultAllocator#alloc(<http://kotlin.Int|kotlin.Int>){}io.ktor.utils.io.bits.Memory	
kfun:io.ktor.utils.io.bits.Allocator#alloc(<http://kotlin.Int|kotlin.Int>){}io.ktor.utils.io.bits.Memory-trampoline	
kfun:io.ktor.utils.io.core.DefaultBufferPool#produceInstance(){}io.ktor.utils.io.core.internal.ChunkBuffer	
kfun:io.ktor.utils.io.pool.DefaultPool#produceInstance(){}1:0-trampoline	
<inlined-out:<anonymous>>	
<inlined-out:synchronized>	
kfun:io.ktor.utils.io.pool.DefaultPool#borrow(){}1:0	
kfun:io.ktor.utils.io.pool.ObjectPool#borrow(){}1:0-trampoline	
kfun:io.ktor.utils.io.core.Output.appendNewChunk#internal	
kfun:io.ktor.utils.io.core.Output#prepareWriteHead(<http://kotlin.Int|kotlin.Int>){}io.ktor.utils.io.core.internal.ChunkBuffer	
kfun:io.ktor.utils.io.core.internal#prepareWriteHead__at__io.ktor.utils.io.core.Output(<http://kotlin.Int;io.ktor.utils.io.core.internal.ChunkBuffer?|kotlin.Int;io.ktor.utils.io.core.internal.ChunkBuffer?>){}io.ktor.utils.io.core.internal.ChunkBuffer	
<inlined-out:writeWhile>	
<inlined-out:writeFullyBytesTemplate>	
kfun:io.ktor.utils.io.core#writeFully__at__io.ktor.utils.io.core.Output(kotlin.ByteArray;kotlin.Int;kotlin.Int){}	
kfun:io.ktor.utils.io.ByteChannelSequentialBase.$writeFullyCOROUTINE$10.invokeSuspend#internal	
kfun:kotlin.coroutines.native.internal.BaseContinuationImpl#invokeSuspend(kotlin.Result<kotlin.Any?>){}kotlin.Any?-trampoline	
<inlined-out:<anonymous>>	
kfun:kotlin#with(0:0;kotlin.Function1<0:0,0:1>){0§<kotlin.Any?>;1§<kotlin.Any?>}0:1	
kfun:kotlin.coroutines.native.internal.BaseContinuationImpl#resumeWith(kotlin.Result<kotlin.Any?>){}	
kfun:kotlin.coroutines.Continuation#resumeWith(kotlin.Result<1:0>){}-trampoline	
kfun:kotlin.coroutines#resume__at__kotlin.coroutines.Continuation<0:0>(0:0){0§<kotlin.Any?>}	
<inlined-out:<anonymous>>	
<inlined-out:withContinuationContext>	
kfun:kotlinx.coroutines.DispatchedTask#run(){}	
kfun:kotlinx.coroutines.EventLoop#processUnconfinedEvent(){}kotlin.Boolean	
<inlined-out:runUnconfinedEventLoop>	
kfun:kotlinx.coroutines.resumeUnconfined#internal	
kfun:kotlinx.coroutines#dispatch__at__kotlinx.coroutines.DispatchedTask<0:0>(<http://kotlin.Int|kotlin.Int>){0§<kotlin.Any?>}	
kfun:kotlinx.coroutines.CancellableContinuationImpl.dispatchResume#internal	
kfun:kotlinx.coroutines.CancellableContinuationImpl#completeResume(kotlin.Any){}	
kfun:kotlinx.coroutines.CancellableContinuation#completeResume(kotlin.Any){}-trampoline	
<inlined-out:<anonymous>>	
kfun:kotlin#let__at__0:0(kotlin.Function1<0:0,0:1>){0§<kotlin.Any?>;1§<kotlin.Any?>}0:1	
kfun:kotlinx.coroutines.channels.tryResume0#internal	
kfun:kotlinx.coroutines.channels.BufferedChannel.BufferedChannelIterator.tryResumeHasNext#internal	
kfun:kotlinx.coroutines.channels.BufferedChannel.tryResumeReceiver#internal	
kfun:kotlinx.coroutines.channels.BufferedChannel.updateCellSend#internal	
<inlined-out:sendImpl>	
kfun:kotlinx.coroutines.channels.BufferedChannel#trySend(1:0){}kotlinx.coroutines.channels.ChannelResult<kotlin.Unit>	
kfun:kotlinx.coroutines.channels.Channel#trySend(1:0){}kotlinx.coroutines.channels.ChannelResult<kotlin.Unit>-trampoline	
kfun:io.ktor.client.engine.darwin.internal.DarwinTaskHandler#receiveData(platform.Foundation.NSURLSessionDataTask;platform.Foundation.NSData){}	
kfun:io.ktor.client.engine.darwin.KtorNSURLSessionDelegate#objc:URLSession:dataTask:didReceiveData:	
_696f2e6b746f723a6b746f722d636c69656e742d64617277696e2f6f70742f6275696c644167656e742f776f726b2f386435343762393734613762653231662f6b746f722d636c69656e742f6b746f722d636c69656e742d64617277696e2f64617277696e2f7372632f696f2f6b746f722f636c69656e742f656e67696e652f64617277696e2f4b746f724e5355524c53657373696f6e44656c65676174652e6b74_knbridge4	
0x183d37ff0	
_dispatch_call_block_and_release	
_dispatch_client_callout	
_dispatch_lane_serial_drain	
_dispatch_lane_invoke	
_dispatch_workloop_worker_thread	
_pthread_wqthread	
start_wqthread