简体   繁体   English

iPhone应用程序内存问题。 因内存不足而崩溃?

[英]iPhone app memory issues. Crashing due to Low Memory?

My iPhone app is crashing like crazy. 我的iPhone应用程序崩溃像疯了一样。 I need some help, or really direction. 我需要帮助,或者真的需要指导。

Quick background: I don't know how to program that well. 快速背景:我不知道如何编程。 I've only been doing it for a few months and I'm pretty sure I didn't make this app in the best possible way, or even close. 我只做了几个月,我很确定我没有以最好的方式制作这个应用程序,甚至没有关闭它。

The app is basically a menu screen with 10 buttons. 该应用程序基本上是带有10个按钮的菜单屏幕。 Each button leads to 3 pages and the navigation will take you back and forth, and back to the main menu. 每个按钮指向3页,导航将带您来回切换至主菜单。 It only has one view and all of the images load as you start the app. 它只有一个视图,并且在启动应用程序时会加载所有图像。 All of the images are hidden at first, and as needed they will be shown (hidden = NO). 首先将所有图像隐藏起来,并根据需要显示它们(隐藏= NO)。

The app loads up very slowly and then functions exactly as needed. 该应用程序加载速度非常慢,然后完全根据需要运行。 After clicking through about 15 of the images, it will crash. 单击大约15张图像后,它将崩溃。

I've used instruments (not fully sure how to yet) but it doesn't show any memory leaks. 我已经使用过仪器(还不确定如何操作),但是它没有显示任何内存泄漏。 It shows "low memory" a bunch of times before crashing. 在崩溃之前,它多次显示“内存不足”。

So time for my questions. 所以我的问题了。 I apologize that these are very amateur. 我很抱歉,这些都是非常业余的。 This is the first time I've seen any crashes in my apps, and this one seemed so simple to me. 这是我第一次看到应用程序崩溃,对我来说,这似乎很简单。 It has a lot more imagery than anything I've done before. 它的图像比我以前做的要多得多。

  1. Without starting the app over from scratch, is there anything I can do to fix the issue and eliminate the crashes? 如果不从头开始重新启动应用程序,我可以采取任何措施解决此问题并消除崩溃吗? I don't fully understand how to handle memory (obviously). 我不完全了解如何处理内存(显然)。 I know that anytime you alloc something you should release it, but I don't know that I ever do alloc it. 我知道,每当您分配某些内容时,您都应该释放它,但是我不知道我曾经分配过它。 All I'm doing is showing and hiding images. 我要做的就是显示和隐藏图像。

  2. If I have to start over from scratch, what is the best way to do an app like this (similar to a book. main window for navigation, and 10 buttons that lead to 3 full screen images each)? 如果我必须从头开始,那么做这样一个应用程序的最佳方法是什么(类似于书本,导航主窗口和10个按钮,每个按钮可产生3张全屏图像)? I've never worked in more views than the utility template. 除了实用程序模板,我从未在更多的视图中工作过。

  3. What other information can I provide you to help me? 我还能提供什么其他信息来帮助我?

I appreciate this! 我很欣赏这个! I created this app for a friend and I've been struggling for a few days trying to make it right. 我为一个朋友创建了这个应用程序,而几天来我一直在努力使其正确。

EDIT: 编辑:

(add your analyze details and code here). (在此处添加您的分析详细信息和代码)。 Put 4 spaces in front of your code and ensure formatting. 在代码前放置4个空格,并确保格式正确。

- (IBAction)pressNext:(id)sender
{
if (num == 11)
{
    peteNotes1.hidden = YES;
     //[peteNotes1 release], peteNotes1 = nil;
    peteBio.hidden = NO;
    num = 12;
    //[self playRandomSound];
}
else if (num == 12)
{
    peteBio.hidden = YES;
     //[peteBio release], peteBio = nil;
    petePic.hidden = NO;
    next.hidden = YES;
    num = 13;
    //[self playRandomSound];
}
else if (num == 21)
{
    eddieNotes1.hidden = YES;
     //[eddieNotes1 release], eddieNotes1 = nil;
    eddieBio.hidden = NO;
    num = 22;
    //[self playRandomSound];
}
else if (num == 22)
{
    eddieBio.hidden = YES;
     //[eddieBio release], eddieBio = nil;
    eddiePic.hidden = NO;
    next.hidden = YES;
    num = 23;
    //[self playRandomSound];
}

Please add some code and I'll add more details. 请添加一些代码,我将添加更多详细信息。

Two things you can do right now: 您现在可以做两件事:

  1. Run build with analyze. 运行带有分析的构建。 It will show you where to focus. 它将向您显示重点。
  2. Read and re-read the apple memory guide. 阅读并重新阅读苹果内存指南。

http://developer.apple.com/library/mac/#documentation/Cocoa/Conceptual/MemoryMgmt/Articles/MemoryMgmt.html http://developer.apple.com/library/mac/#documentation/Cocoa/Conceptual/MemoryMgmt/Articles/MemoryMgmt.html

Specifically, the four rules on the second page. 具体来说,第二页上的四个规则。

Don't start over - stick with it and understand what's going on. 不要重新开始-坚持下去,了解发生了什么。 That's the best way to learn. 那是最好的学习方式。 It's hard at first but you'll get it soon. 起初很难,但是很快就会得到。

[2 cents]Other advice - and this is my personal opinion. [2美分]其他建议-这是我的个人观点。 Learn how to code the apps from scratch and then use interface builder as a convenience (not a crutch). 了解如何从头开始编写应用程序代码,然后方便地使用界面生成器(而不是拐杖)。 I like the Zdziarski early iPhone SDK book for that reason. 因此,我喜欢Zdziarski早期的iPhone SDK书。 I realize others may not agree (and that's fine) but starting with code really helps you understand what's going on better.[/2 cents] 我意识到其他人可能不同意(这很好),但是从代码开始确实可以帮助您了解正在发生的事情。[/ 2分]

EDIT: 编辑:

After looking at the code, I realized fixing this is beyond the scope of the SO question. 看完代码后,我意识到解决此问题超出了SO问题的范围。 But, I do have a few pieces of advice where to focus. 但是,我确实有一些建议可以重点关注。

  1. Spend some more time going through books and tutorials. 花更多时间阅读书籍和教程。 It's obvious from the snippet above, you just need to work through books and structured guidance before going out on your own app. 从上面的代码段中可以明显看出,您只需要通读书籍和结构化指南,然后再使用自己的应用程序即可。
  2. Look into arrays. 查看数组。 The if else with nums and setting setting the next num is a really bad coding pattern. if num和设置下一个num的if else是一个非常糟糕的编码模式。 Click next should increment a member variable property and your data should be in arrays. 单击下一步将增加成员变量属性,并且您的数据应位于数组中。 If you're doing large switch blocks or large if else blocks, it's a sign you're doing something wrong. 如果您正在执行大型开关模块或大型其他模块,则表明您在做错事。
  3. Rethink the pattern. 重新思考模式。 Find a way to load the images on demand. 寻找一种按需加载图像的方法。 And, figure out how to unload/release the images when not needed. 并且,找出不需要时如何卸载/释放图像。 You have a slow startup and memory foot print by loading up front and just hiding. 通过预先加载并隐藏,您的启动和存储足迹会很慢。
  4. Look at the size of the images. 查看图像的尺寸。 Have you resized them down to be the optimal size and pixels per inch on an iphone? 您是否已将它们调整为iphone的最佳尺寸和每英寸像素?

Hopefully the initial pointers and these help you going a little closer to the right direction. 希望初始指针和这些指针能帮助您更接近正确的方向。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM