Code Monkey home page Code Monkey logo

Comments (9)

iziz avatar iziz commented on August 29, 2024

Please show me your test code.

from libphonenumber-ios.

pmurphyjam avatar pmurphyjam commented on August 29, 2024

Iziz,
I noticed you had the ABContacts library, you can use this code below to create random contacts with phone numbers and email addresses, or use the library I’ve attached.
Then simply loop through the contacts once you’ve created 25K or so, and watch the memory in Xcode on the debug pane.
@autoreleasepool
{
ABAddressBookRef addressBookRef = ABAddressBookCreateWithOptions(NULL, NULL);
CFArrayRef arrayRef = ABAddressBookCopyArrayOfAllPeople(addressBookRef);
NSArray *contacts = (__bridge NSArray *)(arrayRef);
CFRelease(arrayRef);
int totalContacts = [contacts count];
for(int contactIndex = 0; contactIndex < totalContacts; contactIndex++)
{

    ABRecordRef person = (__bridge ABRecordRef)([contacts objectAtIndex:contactIndex]);
            CFTypeRef firstnameRef = ABRecordCopyValue(person, kABPersonFirstNameProperty);
            NSString *firstname = (__bridge NSString *)(firstnameRef);
            if(!(firstnameRef == NULL))
                CFRelease(firstnameRef);

            CFTypeRef lastnameRef = ABRecordCopyValue(person, kABPersonLastNameProperty);
            NSString *lastname = (__bridge NSString *)(lastnameRef);
            if(!(lastnameRef == NULL))
                CFRelease(lastnameRef);

            CFTypeRef modificationDateRef = ABRecordCopyValue(person, kABPersonModificationDateProperty);
         NSDate *modificationDate = (__bridge NSDate *)(modificationDateRef);
            if(!(modificationDateRef == NULL))
                CFRelease(modificationDateRef);
            NSLog(@"ContactModel : insertUserContacts : contact = %@ %@ : Date = %@",firstname,lastname,modificationDate);

    CFTypeRef phoneProperty = ABRecordCopyValue(person, kABPersonPhoneProperty);
            CFArrayRef arrayPhoneRef = ABMultiValueCopyArrayOfAllValues(phoneProperty);
            NSArray *phoneArray = (__bridge NSArray *)(arrayPhoneRef);
            CFRelease(phoneProperty);
            if(!(arrayPhoneRef == NULL))
                CFRelease(arrayPhoneRef);

    if([phoneArray count] > 0)
    {
        for(NSString * phone in phoneArray)
                {
            NSError * aError;
                        NSString *strippedPhone = [[NBPhoneNumberUtil sharedInstance] format:phone numberFormat:NBEPhoneNumberFormatE164 error:&aError];
            //Run this for 25K ABContacts, and you’ll see the memory go up to 450MBs, and never get free'd
        {
    }

    }
}//autoreleasepool

//Used by ABContacts put in ABContactsHelper
//Beware ABContacts has tons of leaks in it also, I’ve attached a version that actually works and has the below methods in it

+(NSString*)randomPhoneNumber
{
NSString *randomPhoneCountry = @"+1-";
NSString *randomAreaCodeStr = [[NSNumber numberWithInt:(int)(arc4random() % 800-1) + 200] stringValue];
NSString *randomPhonePrefixCodeStr = [[NSNumber numberWithInt:(int)(arc4random() % 800-1) + 200] stringValue];
NSString *randomPhonePostFixCodeStr = [[NSNumber numberWithInt:(int)(arc4random() % 8000-1) + 2000] stringValue];
NSString *randomPhone = [NSString stringWithFormat:@"%@(%@)-%@-%@",randomPhoneCountry,randomAreaCodeStr,randomPhonePrefixCodeStr,randomPhonePostFixCodeStr];
return randomPhone;
}

+(NSDictionary*)randomContact
{
NSDictionary *firstNameDic = [NSDictionary dictionaryWithObjectsAndKeys:@"Pat",@"FirstName_0",@"Paul",@"FirstName_1",@"James",@"FirstName_2",@"Karl",@"FirstName_3",@"Sheffield",@"FirstName_4",@"Ivan",@"FirstName_5",@"Craig",@"FirstName_6",@"Steve",@"FirstName_7",@"Jordon",@"FirstName_8",@"Victoria",@"FirstName_9",@"Alexandria",@"FirstName_10",@"Jacqueline",@"FirstName_11",@"Sally",@"FirstName_12",@"Milley",@"FirstName_13",@"Melissa",@"FirstName_14",@"Callie",@"FirstName_15",@"Mattey",@"FirstName_16",@"Rachael",@"FirstName_17",@"Sandy",@"FirstName_18",@"Monica",@"FirstName_19", nil];
NSDictionary *lastNameDic = [NSDictionary dictionaryWithObjectsAndKeys:@"urphy",@"LastName_0",@"aptista",@"LastName_1",@"ansom",@"LastName_2",@"anders",@"LastName_3",@"olan",@"LastName_4",@"onald",@"LastName_5",@"abama",@"LastName_6",@"antel",@"LastName_7",@"mith",@"LastName_8",@"ones",@"LastName_9",@"ishop",@"LastName_10",@"itchel",@"LastName_11",@"unn",@"LastName_12",@"alin",@"LastName_13",@"antica",@"LastName_14",@"olly",@"LastName_15",@"ansome",@"LastName_16",@"hipmore",@"LastName_17",@"ates",@"LastName_18",@"ackson",@"LastName_19", nil];
NSString *alphabet = @"ABCDEFGHIJKLMNOPQRSTUVWXYZ";
int letter = (int)(arc4random() % [alphabet length]);
NSRange range;
range.location = letter;
range.length = 1;
NSString *firstLetter = [alphabet substringWithRange:range];
int randomFirstNameNum = (int)(arc4random() % 19);
int randomLastNameNum = (int)(arc4random() % 19);
NSString *randomFirstNameKey = [NSString stringWithFormat:@"FirstName_%d",randomFirstNameNum];
NSString *randomLastNameKey = [NSString stringWithFormat:@"LastName_%d",randomLastNameNum];
NSString *lastNameStr = [NSString stringWithFormat:@"%@%@",firstLetter,[lastNameDic objectForKey:randomLastNameKey]];
NSString *randomPhone = [self randomPhoneNumber];
NSString *randomEmail = [NSString stringWithFormat:@"%@.%@@appredeem.com",[firstNameDic objectForKey:randomFirstNameKey],lastNameStr];
NSDictionary *randomNameDic = [NSDictionary dictionaryWithObjectsAndKeys:[firstNameDic objectForKey:randomFirstNameKey],@"FirstName",lastNameStr,@"LastName",randomPhone,@"Phone",randomEmail,@"Email",nil];
return randomNameDic;
}

+(void)createRandomContacts
{
NSArray *contacts = [ABContactsHelper contacts];
NSLog(@"ABContactsHelper : CreateRandomContacts[%d] ",[contacts count]);
int maxContacts = 1000;
for (int index = 0; index<maxContacts; index++)
{
ABContact *contact = [ABContact contact];
NSDictionary *randomContactDic = [self randomContact];
[contact setFirstname:[randomContactDic objectForKey:@"FirstName"]];
[contact setLastname:[randomContactDic objectForKey:@"LastName"]];
NSMutableArray *phoneDicArray = [[NSMutableArray alloc] init];
NSDictionary *phoneMobileDic = [NSDictionary dictionaryWithObjectsAndKeys:[randomContactDic objectForKey:@"Phone"],@"value",kABPersonPhoneIPhoneLabel,@"label",nil];
[phoneDicArray addObject:phoneMobileDic];
[contact setPhoneDictionaries:phoneDicArray];
ABMutableMultiValueRef multiEmail = ABMultiValueCreateMutable(kABMultiStringPropertyType);
NSMutableDictionary *emailDic = [NSMutableDictionary dictionaryWithObjectsAndKeys:
[randomContactDic objectForKey:@"Email"],@"value",
@"Email",@"label",
nil];
[contact setEmailDictionaries:emailDic andMulti:multiEmail];
CFRelease(multiEmail);
NSError *error = nil;
BOOL status = [ABContactsHelper addContact:contact withError:&error];
NSLog(@"ABContactsHelper : CreateRandomContacts[%d] : Contact : %@ : status = %@ ",index,randomContactDic,status?@"YES":@"NO");
}
}

+(void)deleteAllContacts
{
NSArray *contacts = [ABContactsHelper contacts];
NSError *error = nil;
for (ABContact *contact in contacts)
[contact removeSelfFromAddressBook:&error];
}

On Apr 8, 2014, at 4:55 PM, iziz [email protected] wrote:

Please show me your test code.


Reply to this email directly or view it on GitHub.

from libphonenumber-ios.

iziz avatar iziz commented on August 29, 2024

I was tested yours.

[AppDelegate createRandomContacts];
[self testIt];
[AppDelegate deleteAllContacts];

As you see, 'ABContactsHelper' has more...

screen shot 2014-04-09 at 10 20 11

- (void)testIt
{
    @autoreleasepool
    {
        ABAddressBookRef addressBookRef = ABAddressBookCreateWithOptions(NULL, NULL);
        CFArrayRef arrayRef = ABAddressBookCopyArrayOfAllPeople(addressBookRef);
        NSArray *contacts = (__bridge NSArray *)(arrayRef);
        CFRelease(arrayRef);
        int totalContacts = [contacts count];
        for(int contactIndex = 0; contactIndex < totalContacts; contactIndex++)
        {

            ABRecordRef person = (__bridge ABRecordRef)([contacts objectAtIndex:contactIndex]);
            CFTypeRef firstnameRef = ABRecordCopyValue(person, kABPersonFirstNameProperty);
            NSString *firstname = (__bridge NSString *)(firstnameRef);
            if(!(firstnameRef == NULL))
                CFRelease(firstnameRef);

            CFTypeRef lastnameRef = ABRecordCopyValue(person, kABPersonLastNameProperty);
            NSString *lastname = (__bridge NSString *)(lastnameRef);
            if(!(lastnameRef == NULL))
                CFRelease(lastnameRef);

            CFTypeRef modificationDateRef = ABRecordCopyValue(person, kABPersonModificationDateProperty);
            NSDate *modificationDate = (__bridge NSDate *)(modificationDateRef);
            if(!(modificationDateRef == NULL))
                CFRelease(modificationDateRef);
            NSLog(@"ContactModel : insertUserContacts : contact = %@ %@ : Date = %@",firstname,lastname,modificationDate);

            CFTypeRef phoneProperty = ABRecordCopyValue(person, kABPersonPhoneProperty);
            CFArrayRef arrayPhoneRef = ABMultiValueCopyArrayOfAllValues(phoneProperty);
            NSArray *phoneArray = (__bridge NSArray *)(arrayPhoneRef);
            CFRelease(phoneProperty);
            if(!(arrayPhoneRef == NULL))
                CFRelease(arrayPhoneRef);

            if ([phoneArray count] > 0)
            {
                for (NSString * phone in phoneArray)
                {
                    NSError *aError;
                    NBPhoneNumber *aPhoneNumber = [[NBPhoneNumberUtil sharedInstance] parse:phone defaultRegion:@"US" error:&aError];
                    NSString *strippedPhone = [[NBPhoneNumberUtil sharedInstance] format:aPhoneNumber
                                                                            numberFormat:NBEPhoneNumberFormatE164
                                                                                   error:&aError];
                    //Run this for 25K ABContacts, and you’ll see the memory go up to 450MBs, and never get free'd
                        NSLog(@"- %@", strippedPhone);
                }
            }

            // autoreleasepool

            // Used by ABContacts put in ABContactsHelper
            // Beware ABContacts has tons of leaks in it also,
            // I’ve attached a version that actually works and has the below methods in it
        }
    }
}

+ (NSString *)randomPhoneNumber
{
    NSString *randomPhoneCountry = @"+1-";
    NSString *randomAreaCodeStr = [[NSNumber numberWithInt:(int)(arc4random() % 800-1) + 200] stringValue];
    NSString *randomPhonePrefixCodeStr = [[NSNumber numberWithInt:(int)(arc4random() % 800-1) + 200] stringValue];
    NSString *randomPhonePostFixCodeStr = [[NSNumber numberWithInt:(int)(arc4random() % 8000-1) + 2000] stringValue];
    NSString *randomPhone = [NSString stringWithFormat:@"%@(%@)-%@-%@",randomPhoneCountry,randomAreaCodeStr,randomPhonePrefixCodeStr,randomPhonePostFixCodeStr];
    return randomPhone;
}

+ (NSDictionary *)randomContact
{
    NSDictionary *firstNameDic = [NSDictionary dictionaryWithObjectsAndKeys:@"Pat",@"FirstName_0",@"Paul",@"FirstName_1",@"James",@"FirstName_2",@"Karl",@"FirstName_3",@"Sheffield",@"FirstName_4",@"Ivan",@"FirstName_5",@"Craig",@"FirstName_6",@"Steve",@"FirstName_7",@"Jordon",@"FirstName_8",@"Victoria",@"FirstName_9",@"Alexandria",@"FirstName_10",@"Jacqueline",@"FirstName_11",@"Sally",@"FirstName_12",@"Milley",@"FirstName_13",@"Melissa",@"FirstName_14",@"Callie",@"FirstName_15",@"Mattey",@"FirstName_16",@"Rachael",@"FirstName_17",@"Sandy",@"FirstName_18",@"Monica",@"FirstName_19", nil];
    NSDictionary *lastNameDic = [NSDictionary dictionaryWithObjectsAndKeys:@"urphy",@"LastName_0",@"aptista",@"LastName_1",@"ansom",@"LastName_2",@"anders",@"LastName_3",@"olan",@"LastName_4",@"onald",@"LastName_5",@"abama",@"LastName_6",@"antel",@"LastName_7",@"mith",@"LastName_8",@"ones",@"LastName_9",@"ishop",@"LastName_10",@"itchel",@"LastName_11",@"unn",@"LastName_12",@"alin",@"LastName_13",@"antica",@"LastName_14",@"olly",@"LastName_15",@"ansome",@"LastName_16",@"hipmore",@"LastName_17",@"ates",@"LastName_18",@"ackson",@"LastName_19", nil];
    NSString *alphabet = @"ABCDEFGHIJKLMNOPQRSTUVWXYZ";
    int letter = (int)(arc4random() % [alphabet length]);
    NSRange range;
    range.location = letter;
    range.length = 1;
    NSString *firstLetter = [alphabet substringWithRange:range];
    int randomFirstNameNum = (int)(arc4random() % 19);
    int randomLastNameNum = (int)(arc4random() % 19);
    NSString *randomFirstNameKey = [NSString stringWithFormat:@"FirstName_%d",randomFirstNameNum];
    NSString *randomLastNameKey = [NSString stringWithFormat:@"LastName_%d",randomLastNameNum];
    NSString *lastNameStr = [NSString stringWithFormat:@"%@%@",firstLetter,[lastNameDic objectForKey:randomLastNameKey]];
    NSString *randomPhone = [self randomPhoneNumber];
    NSString *randomEmail = [NSString stringWithFormat:@"%@.%@@appredeem.com",[firstNameDic objectForKey:randomFirstNameKey],lastNameStr];
    NSDictionary *randomNameDic = [NSDictionary dictionaryWithObjectsAndKeys:[firstNameDic objectForKey:randomFirstNameKey],@"FirstName",lastNameStr,@"LastName",randomPhone,@"Phone",randomEmail,@"Email",nil];
    return randomNameDic;
}

+ (void)createRandomContacts
{
    NSArray *contacts = [ABContactsHelper contacts];
    NSLog(@"ABContactsHelper : CreateRandomContacts[%d] ",[contacts count]);
    int maxContacts = 1000;
    for (int index = 0; index<maxContacts; index++)
    {
        ABContact *contact = [ABContact contact];
        NSDictionary *randomContactDic = [self randomContact];
        [contact setFirstname:[randomContactDic objectForKey:@"FirstName"]];
        [contact setLastname:[randomContactDic objectForKey:@"LastName"]];
        NSMutableArray *phoneDicArray = [[NSMutableArray alloc] init];
        NSDictionary *phoneMobileDic = [NSDictionary dictionaryWithObjectsAndKeys:[randomContactDic objectForKey:@"Phone"],@"value",kABPersonPhoneIPhoneLabel,@"label",nil];
        [phoneDicArray addObject:phoneMobileDic];
        [contact setPhoneDictionaries:phoneDicArray];
        ABMutableMultiValueRef multiEmail = ABMultiValueCreateMutable(kABMultiStringPropertyType);
        NSMutableDictionary *emailDic = [NSMutableDictionary dictionaryWithObjectsAndKeys:
                                         [randomContactDic objectForKey:@"Email"],@"value",
                                         @"Email",@"label",
                                         nil];
        // [contact setEmailDictionaries:emailDic andMulti:multiEmail];

        CFRelease(multiEmail);
        NSError *error = nil;
        BOOL status = [ABContactsHelper addContact:contact withError:&error];
        NSLog(@"ABContactsHelper : CreateRandomContacts[%d] : Contact : %@ : status = %@ ",index,randomContactDic,status?@"YES":@"NO");
    }
}

+ (void)deleteAllContacts
{
    NSArray *contacts = [ABContactsHelper contacts];
    NSError *error = nil;
    for (ABContact *contact in contacts)
        [contact removeSelfFromAddressBook:&error];
}

So, please tell me more informations for libPhoneNumbe-iOS allocations.

ps. Using ARC project.

from libphonenumber-ios.

iziz avatar iziz commented on August 29, 2024

Added
"ARC only, or add the -fobjc-arc flag if your project is non-ARC"
in README

If you has same issue again, reopen it.

thanks.

from libphonenumber-ios.

pmurphyjam avatar pmurphyjam commented on August 29, 2024

iziz,
It’s still broke. All our code is using ARC.
I’ve put together an Xcode test project using your project adding only ABContacts and ContactsTestViewController which will show you the problem.
ContactsTestViewController has two buttons, the “Create More Contacts” will create 20,000 random contacts so you can see the problem.
Once this is done, you can then hit “Test Button”, this will then loop through all the random contact phone numbers using your NBPhoneNumberUtil library, and you will see it consume and hold onto the memory since your storing something you shouldn’t which never get’s free’d up. There is a #ifdef TEST_NBPHONE define, it is defined so it will run your library, and you can see the memory consumption problem. You can also comment out “#define TEST_NBPHONE”, and this will run the same test without your library, and you’ll see the memory consumption is fine. Without your library running, the App will consume around 37MB’s, and everything is free’d OK. With your library running on 29147 contacts you will see it consume around 293MBs which never get’s free'd.
The Start and Finished memory consumptions are displayed for you.
It would be most appreciated if you could fix this libraries memory consumption since for users with a large number of contacts it will not work and crash the application.
Thank you…
Pat…

The attached test Xcode project is attached below:

On Apr 8, 2014, at 6:54 PM, iziz [email protected] wrote:

Closed #6.


Reply to this email directly or view it on GitHub.

from libphonenumber-ios.

iziz avatar iziz commented on August 29, 2024

Hm... cant download your "attached test Xcode project"
Would you send me your test project to zen.isis ?

from libphonenumber-ios.

iziz avatar iziz commented on August 29, 2024

OK.

As you see...
screen shot 2014-04-11 at 7 43 12

I think that you should check memory status at right time

from libphonenumber-ios.

iziz avatar iziz commented on August 29, 2024

So. you can change like that

for(NSString * phone in phoneArray)
                {
                    @autoreleasepool {
    #define TEST_NBPHONE
    #ifdef TEST_NBPHONE
                        NBPhoneNumber *realphone = [self isValidPhoneNumber:phone];
    #else
                        NSString *realphone = @"";
    #endif
                        if(realphone!=nil)
                        {
                            foundPhoneForContact=YES;
    #ifdef TEST_NBPHONE
                            NSError * aError;
                            NSString *strippedPhone = [[NBPhoneNumberUtil sharedInstance] format:realphone numberFormat:NBEPhoneNumberFormatE164 error:&aError];
    #endif
                            if(contactIndex % 10 == 0)
                            {
                                //Update every 10th
                                [self performSelectorOnMainThread:@selector(dynamicMemoryLabels:) withObject:[NSNumber numberWithInt:contactIndex] waitUntilDone:NO];
                            }
                            NDLog(@"ContactModel : Name : PHONE : %@ %@ Phone: %@",firstname,lastname, phone);
                        }
                    }
                }

it will show steady memory status graph... but can not guarantee process performance.

from libphonenumber-ios.

david-hoze avatar david-hoze commented on August 29, 2024

Hi, the same issue happens to me..
I'm going through a lot of contacts, and instantiating NBPhoneNumberUtil for each of them results in a memory leak and crash. When I use the semi-deprecated (😄 ) sharedInstance singleton, the crash is prevented, but there are 17MB (for around 3000 phone numbers) extra memory to our app which do not get released..

Couldn't figure out what you meant by the change you proposed in the last comment, since I don't have the test project.

Your library is very useful, and meanwhile we're using the sharedInstance workaround, but we would like to avoid these extra MBs, could you please help us?

from libphonenumber-ios.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.