garbage collection memory leak
garbage collection memory leak
- Subject: garbage collection memory leak
- From: Michael Link <email@hidden>
- Date: Wed, 17 Dec 2008 17:16:39 -0600
I'm trying to track down a memory leak even though I'm using garbage
collection. I was curious what the "rc:" field means when using "info
gc-roots" in gdb? It doesn't seem to correlate to the Retain Count
when inspecting an address in Instruments and it doesn't seem to
always correlate to using "info gc-references" in gdb. I would tend to
assume that it would make more sense if it meant the retain count
since that would explain why the root objects have something > 0.
After running about 40 NSURLConnections in an application and closing
out everything so that only the menu bar is left I noticed that there
are some objects not being collected that are definitely not being
used anymore and should have been collected.
Some of the objects I noticed right away are NSXMLDocuments which are
created right after a connection sends the -
connectionDidFinishLoading: delegate message. The method looks like:
- (void)connectionDidFinishLoading:(NSURLConnection*)connection
{
NSError* error = nil;
NSString* string = [[NSString alloc] initWithData:connection.data
encoding:NSUTF8StringEncoding];
NSXMLDocument* document = nil;
if (document = [[NSXMLDocument alloc] initWithXMLString:string
options:[self options] error:&error]) {
... extract some NSStrings using objectsForXQuery:@"xs:string(/
some[1]/path[1])
... assign strings to NSManagedObject
}
}
the -connectionDidFinishLoading: runs on a background thread that is
using NSRunLoop, so the stack should be cleared.
The root objects for 1 of these NSXMLDocuments is:
(gdb) info gc-roots 0x17815b0
Number of roots: 3
Root:
warning: can't find class named `NSCFType' given by ObjC class object
0 Kind: object rc: 2 Address: 0x014bab40 Offset: 0x00000044
Class: NSCFType
warning: can't find class named `a' given by ObjC class object
1 Kind: object rc: 0 Address: 0x014b2070 Offset: 0x00000020
Class: NSCFType
2 Kind: bytes rc: 0 Address: 0x014af730 Offset: 0x00000018
warning: can't find class named `NSXMLNamedNode' given by ObjC class
object
3 Kind: object rc: 0 Address: 0x0148fb10 Offset: 0x00000008
Class: NSXMLNamedNode
4 Kind: object rc: 0 Address: 0x0148fae0 Class: NSXMLElement
ivar: NSXMLNode._parent
5 Kind: object rc: 0 Address: 0x0148fab0 Class: NSXMLElement
ivar: NSXMLNode._parent
6 Kind: object rc: 0 Address: 0x01493010 Class: NSXMLElement
ivar: NSXMLNode._parent
7 Kind: object rc: 0 Address: 0x01492fe0 Class: NSXMLElement
ivar: NSXMLNode._parent
8 Kind: object rc: 0 Address: 0x014942a0 Class: NSXMLElement
ivar: NSXMLNode._parent
9 Kind: object rc: 0 Address: 0x014970e0 Class: NSXMLElement
ivar: NSXMLNode._parent
10 Kind: object rc: 0 Address: 0x01498570 Class: NSXMLElement
ivar: NSXMLNode._parent
11 Kind: object rc: 0 Address: 0x014a5280 Class: NSXMLElement
ivar: NSXMLNode._parent
12 Kind: object rc: 0 Address: 0x014b1710 Class: NSXMLElement
ivar: NSXMLNode._parent
13 Kind: object rc: 0 Address: 0x016a0c50 Class: NSXMLElement
ivar: NSXMLNode._parent
14 Kind: object rc: 0 Address: 0x016c6a80 Class: NSXMLElement
ivar: NSXMLNode._parent
15 Kind: object rc: 0 Address: 0x016c6980 Class: NSXMLElement
ivar: NSXMLNode._parent
16 Kind: object rc: 0 Address: 0x016c68a0 Class: NSXMLElement
ivar: NSXMLNode._parent
17 Kind: object rc: 0 Address: 0x01730560 Class: NSXMLElement
ivar: NSXMLNode._parent
18 Kind: object rc: 0 Address: 0x01735060 Class: NSXMLElement
ivar: NSXMLNode._parent
19 Kind: object rc: 0 Address: 0x01d66d50 Class: NSXMLElement
ivar: NSXMLNode._parent
20 Kind: object rc: 0 Address: 0x020948e0 Class: NSXMLElement
ivar: NSXMLNode._parent
21 Kind: object rc: 0 Address: 0x017815b0 Class: NSXMLDocument
Root:
warning: can't find class named `a' given by ObjC class object
0 Kind: object rc: 1 Address: 0x014badb0 Offset: 0x00000014
Class: NSCFType
warning: can't find class named `a' given by ObjC class object
1 Kind: object rc: 2 Address: 0x014bab40 Offset: 0x00000044
Class: NSCFType
warning: can't find class named `a' given by ObjC class object
2 Kind: object rc: 0 Address: 0x014b2070 Offset: 0x00000020
Class: NSCFType
3 Kind: bytes rc: 0 Address: 0x014af730 Offset: 0x00000018
warning: can't find class named `a' given by ObjC class object
4 Kind: object rc: 0 Address: 0x0148fb10 Offset: 0x00000008
Class: NSXMLNamedNode
5 Kind: object rc: 0 Address: 0x0148fae0 Class: NSXMLElement
ivar: NSXMLNode._parent
6 Kind: object rc: 0 Address: 0x0148fab0 Class: NSXMLElement
ivar: NSXMLNode._parent
7 Kind: object rc: 0 Address: 0x01493010 Class: NSXMLElement
ivar: NSXMLNode._parent
8 Kind: object rc: 0 Address: 0x01492fe0 Class: NSXMLElement
ivar: NSXMLNode._parent
9 Kind: object rc: 0 Address: 0x014942a0 Class: NSXMLElement
ivar: NSXMLNode._parent
10 Kind: object rc: 0 Address: 0x014970e0 Class: NSXMLElement
ivar: NSXMLNode._parent
11 Kind: object rc: 0 Address: 0x01498570 Class: NSXMLElement
ivar: NSXMLNode._parent
12 Kind: object rc: 0 Address: 0x014a5280 Class: NSXMLElement
ivar: NSXMLNode._parent
13 Kind: object rc: 0 Address: 0x014b1710 Class: NSXMLElement
ivar: NSXMLNode._parent
14 Kind: object rc: 0 Address: 0x016a0c50 Class: NSXMLElement
ivar: NSXMLNode._parent
15 Kind: object rc: 0 Address: 0x016c6a80 Class: NSXMLElement
ivar: NSXMLNode._parent
16 Kind: object rc: 0 Address: 0x016c6980 Class: NSXMLElement
ivar: NSXMLNode._parent
17 Kind: object rc: 0 Address: 0x016c68a0 Class: NSXMLElement
ivar: NSXMLNode._parent
18 Kind: object rc: 0 Address: 0x01730560 Class: NSXMLElement
ivar: NSXMLNode._parent
19 Kind: object rc: 0 Address: 0x01735060 Class: NSXMLElement
ivar: NSXMLNode._parent
20 Kind: object rc: 0 Address: 0x01d66d50 Class: NSXMLElement
ivar: NSXMLNode._parent
21 Kind: object rc: 0 Address: 0x020948e0 Class: NSXMLElement
ivar: NSXMLNode._parent
22 Kind: object rc: 0 Address: 0x017815b0 Class: NSXMLDocument
Root:
warning: can't find class named `a' given by ObjC class object
0 Kind: object rc: 1 Address: 0x014aeb90 Offset: 0x00000024
Class: NSCFType
warning: can't find class named `a' given by ObjC class object
1 Kind: object rc: 2 Address: 0x014bab40 Offset: 0x00000044
Class: NSCFType
warning: can't find class named `a' given by ObjC class object
2 Kind: object rc: 0 Address: 0x014b2070 Offset: 0x00000020
Class: NSCFType
3 Kind: bytes rc: 0 Address: 0x014af730 Offset: 0x00000018
warning: can't find class named `a' given by ObjC class object
4 Kind: object rc: 0 Address: 0x0148fb10 Offset: 0x00000008
Class: NSXMLNamedNode
5 Kind: object rc: 0 Address: 0x0148fae0 Class: NSXMLElement
ivar: NSXMLNode._parent
6 Kind: object rc: 0 Address: 0x0148fab0 Class: NSXMLElement
ivar: NSXMLNode._parent
7 Kind: object rc: 0 Address: 0x01493010 Class: NSXMLElement
ivar: NSXMLNode._parent
8 Kind: object rc: 0 Address: 0x01492fe0 Class: NSXMLElement
ivar: NSXMLNode._parent
9 Kind: object rc: 0 Address: 0x014942a0 Class: NSXMLElement
ivar: NSXMLNode._parent
10 Kind: object rc: 0 Address: 0x014970e0 Class: NSXMLElement
ivar: NSXMLNode._parent
11 Kind: object rc: 0 Address: 0x01498570 Class: NSXMLElement
ivar: NSXMLNode._parent
12 Kind: object rc: 0 Address: 0x014a5280 Class: NSXMLElement
ivar: NSXMLNode._parent
13 Kind: object rc: 0 Address: 0x014b1710 Class: NSXMLElement
ivar: NSXMLNode._parent
14 Kind: object rc: 0 Address: 0x016a0c50 Class: NSXMLElement
ivar: NSXMLNode._parent
15 Kind: object rc: 0 Address: 0x016c6a80 Class: NSXMLElement
ivar: NSXMLNode._parent
16 Kind: object rc: 0 Address: 0x016c6980 Class: NSXMLElement
ivar: NSXMLNode._parent
17 Kind: object rc: 0 Address: 0x016c68a0 Class: NSXMLElement
ivar: NSXMLNode._parent
18 Kind: object rc: 0 Address: 0x01730560 Class: NSXMLElement
ivar: NSXMLNode._parent
19 Kind: object rc: 0 Address: 0x01735060 Class: NSXMLElement
ivar: NSXMLNode._parent
20 Kind: object rc: 0 Address: 0x01d66d50 Class: NSXMLElement
ivar: NSXMLNode._parent
21 Kind: object rc: 0 Address: 0x020948e0 Class: NSXMLElement
ivar: NSXMLNode._parent
22 Kind: object rc: 0 Address: 0x017815b0 Class: NSXMLDocument
Root Object #1 is: 0x014bab40 CFHTTPMessage
Created at:
18 libSystem.B.dylib thread_start
17 libSystem.B.dylib _pthread_start
16 Foundation __NSThread__main__
15 Foundation -[NSThread main]
14 Foundation +[NSURLConnection(NSURLConnectionReallyInternal)
_resourceLoadLoop:]
13 CoreFoundation CFRunLoopRunInMode
12 CoreFoundation CFRunLoopRunSpecific
11 CFNetwork MultiplexerSource::perform()
10 CFNetwork URLConnectionLoader::processEvents()
9 CFNetwork
URLConnectionLoader
::LoaderConnectionEventQueue
::processAllEventsAndConsumePayload(XConnectionEventInfo<XLoaderEvent,
XLoaderEventParams>*, long)
8 CFNetwork
URLConnectionLoader::loaderScheduleOriginLoad(_CFURLRequest const*)
7 CFNetwork HTTPProtocol::createAndOpenStream()
6 CFNetwork HTTPProtocol::createStream()
5 CFNetwork CFURLRequestCopyHTTPRequest
4 CFNetwork CFHTTPMessageCreateCopy
3 CFNetwork HTTPMessage::operator new(unsigned long, __CFAllocator
const*)
2 CFNetwork CFObject::Allocate(unsigned long, CFClass const&,
__CFAllocator const*)
1 CoreFoundation _CFRuntimeCreateInstance
0 CoreFoundation CFAllocatorAllocate
Root Object #2 is: 0x014badb0 __HTTPNetStreamInfo
Created at:
19 libSystem.B.dylib thread_start
18 libSystem.B.dylib _pthread_start
17 Foundation __NSThread__main__
16 Foundation -[NSThread main]
15 Foundation +[NSURLConnection(NSURLConnectionReallyInternal)
_resourceLoadLoop:]
14 CoreFoundation CFRunLoopRunInMode
13 CoreFoundation CFRunLoopRunSpecific
12 CFNetwork MultiplexerSource::perform()
11 CFNetwork URLConnectionLoader::processEvents()
10 CFNetwork
URLConnectionLoader
::LoaderConnectionEventQueue
::processAllEventsAndConsumePayload(XConnectionEventInfo<XLoaderEvent,
XLoaderEventParams>*, long)
9 CFNetwork
URLConnectionLoader::loaderScheduleOriginLoad(_CFURLRequest const*)
8 CFNetwork HTTPProtocol::createAndOpenStream()
7 CFNetwork HTTPProtocol::createStream()
6 CFNetwork CFHTTPConnectionEnqueue
5 CoreFoundation CFReadStreamCreate
4 CFNetwork EnqueueInfo::constructStreamObject(__CFReadStream*)
3 CFNetwork HTTPNetStreamInfo::operator new(unsigned long,
__CFAllocator const*)
2 CFNetwork CFObject::Allocate(unsigned long, CFClass const&,
__CFAllocator const*)
1 CoreFoundation _CFRuntimeCreateInstance
0 CoreFoundation CFAllocatorAllocate
Root Object #3 is: 0x014aeb90 __HTTPFilter
Created at:
26 libSystem.B.dylib thread_start
25 libSystem.B.dylib _pthread_start
24 Foundation __NSThread__main__
23 Foundation -[NSThread main]
22 Foundation +[NSURLConnection(NSURLConnectionReallyInternal)
_resourceLoadLoop:]
21 CoreFoundation CFRunLoopRunInMode
20 CoreFoundation CFRunLoopRunSpecific
19 CFNetwork MultiplexerSource::perform()
18 CFNetwork URLConnectionLoader::processEvents()
17 CFNetwork
URLConnectionLoader
::LoaderConnectionEventQueue
::processAllEventsAndConsumePayload(XConnectionEventInfo<XLoaderEvent,
XLoaderEventParams>*, long)
16 CFNetwork
URLConnectionLoader::loaderScheduleOriginLoad(_CFURLRequest const*)
15 CFNetwork HTTPProtocol::createAndOpenStream()
14 CFNetwork HTTPProtocol::createStream()
13 CFNetwork
HTTPConnectionCache::CopyConnectionForProtocol(HTTPProtocol*,
__CFHTTPMessage*, unsigned char*)
12 CFNetwork
HTTPConnectionCacheEntry::copyConnectionForProtocol(HTTPProtocol*,
__CFHTTPMessage*, unsigned char*)
11 CFNetwork CFHTTPConnectionCreate
10 CFNetwork HTTPNetConnection_NoAuth::initialize(__CFString
const*, long, unsigned long, __CFDictionary const*,
NetConnection::Options&)
9 CFNetwork HTTPNetConnection::initialize(__CFString const*, long,
unsigned long, __CFDictionary const*, NetConnection::Options&)
8 CFNetwork NetConnection::initialize(NetConnection::Options&)
7 CFNetwork HTTPNetConnection::createStreams(__CFWriteStream**,
__CFReadStream**)
6 CFNetwork __CFWriteStreamCreateHTTPStreamWithConnInfo
5 CoreFoundation CFWriteStreamCreate
4 CFNetwork
WriteFilterConstructionKit::constructStreamObject(__CFWriteStream*)
3 CFNetwork HTTPFilter::operator new(unsigned long, __CFAllocator
const*)
2 CFNetwork CFObject::Allocate(unsigned long, CFClass const&,
__CFAllocator const*)
1 CoreFoundation _CFRuntimeCreateInstance
0 CoreFoundation CFAllocatorAllocate
Inspection of the threads in gdb shows that the thread that runs +
[NSURLConnection(NSURLConnectionReallyInternal) _resourceLoadLoop:] is
still running.
I would guess that something in that thread has some objects laying
around or possibly is falsely rooted on the stack? I'd be curious to
find out any solutions on how to get these objects collected. After a
few thousand NSURLConnections the leakage can be in the 100s of MBs.
Filed as bug #6452901
--
Michael
_______________________________________________
Cocoa-dev mailing list (email@hidden)
Please do not post admin requests or moderator comments to the list.
Contact the moderators at cocoa-dev-admins(at)lists.apple.com
Help/Unsubscribe/Update your Subscription:
This email sent to email@hidden