- 浏览: 546509 次
- 性别:
- 来自: 北京
-
文章分类
最新评论
-
landerson:
明显就有要求的嘛
ANDROID轻量级JSON序列化和反序列化[转] -
jimode2013:
很不错,就是需要这个方法
多个UIViewController使用addSubView,第二个 UIViewController 不响应旋转[转] -
w11h22j33:
...
[转]NSMutableArray中的自动释放对象让我郁闷了一整天 -
w11h22j33:
UILabel* label = [[UILabel a ...
Iphone开发 -
w11h22j33:
http://mobile.51cto.com/iphone- ...
获得通讯录中联系人的所有属性[转]
Finding iPhone Memory Leaks: A “Leaks” Tool Tutorial
There are plenty of different places to get a mobile application designed. The problem is that they’re quite expensive. You might be able to figure out how to create your own, but it will probably look very basic. Instead, a good mobile application development software can make it even easier, so that you can build great looking apps all by yourself.
The Mobile design Starter Kit includes all the themes and scenarios you need to build whatever app you want. There are customizable and standard files that allow you to sell or offer anything you want through the kit. Everything is there, so once you spend the money, you can create as many mobile apps as you want – and even sell the apps if this is your thing.
I’ve been using Instruments a lot lately as I approach the end of my game development cycle. I’ve found it especially useful for tracking down memory leaks in my game. I figured that since I found Instruments so helpful, it might be helpful for other people to get a quick introduction on how to use it to track mem leaks.
What is a memory leak and why should I care?
A memory leak is when your program loses track of a piece of memory that was allocated. The consequence is that the “leaked” memory will never be freed by the program. This usually happens when a piece of code does a “new,” “malloc,” or “alloc” but never does a corresponding “delete”, “free” or “release” respectively.
When you new, malloc, or alloc, what the Operating System is doing is giving your program a chunk of memory on the heap. The OS is saying, “here, have this block of memory at this memory address.” It expects you to hold a reference to that memory address (usually in the form of a pointer) and it’s relying on you to tell the OS when you’re done with it (by calling free, delete, or release).
Memory leaks happen when you throw away your pointer to that memory. If your program no longer knows where on the heap your memory is allocated, how can you ever free it?
So why should you care? In the most minor case, you’re wasting memory that will be freed when the user quits your app. In the worst case, you could have a memory leak that happens every frame. That’s a good way to end up crashing your program, especially if a user lets it run for a long time.
For more general information on memory leaks, have a look at Wikipedia for a start:
http://en.wikipedia.org/wiki/Memory_leak
How do I know I’ve got a memory leak?
Some memory leaks are easy to see by looking at your code. Some are much more difficult. This is where Instruments comes in. Instruments has a “Leaks” tool that will tell you exactly where you’re leaking memory so that you can get in there and fix it!
An Example App
I’ve created an example application that leaks memory in two places: in an Objective-C view controller, and in a C++ class I’m using in the app. The code is available from Github here. Below are excerpts from the code below that contain the leaks we’ll track down:
InstrumentsTestViewController.mm Excerpts
// Leaky excerpts - see GitHub for complete source - (void)viewDidLoad { [super viewDidLoad]; LeakyClass* myLeakyInstance = new LeakyClass(); delete myLeakyInstance; mMyLeakyString = [[NSString alloc] initWithUTF8String:"I'm a leaky string."]; [self doSomethingNow]; } - (void) doSomethingNow { mMyLeakyString = [[NSString alloc] initWithUTF8String: "Look, another alloc, but no release for first one!"]; }
LeakyClass.mm Excerpts
// Leaky excerpts - see GitHub for complete source LeakyClass::LeakyClass() { mLeakedObject = new LeakedObject(); } LeakyClass::~LeakyClass() { }
I’m going to go ahead a build my InstrumentsTest iPhone app in Debug and get it running on my iPhone. (You’ll have to set up the code signing to work on your device.) Once I’ve done that, I’ll boot up Instruments (typing “Instruments” into Spotlight should find it).
Instruments
When you launch Instruments, you should be given the chance to select from a variety of different Instrument tools to use. On the left-hand side, choose iPhone. On the right-hand menu, double-click the “Leaks” tool:
Once you’ve done that, you should see a window that looks like this:
Make sure your iPhone is still connected to your computer. In the top-left corner of the window you’ll see a drop-down menu that says “Launch Executable”. Click on that and make sure that your iPhone (not your computer) is selected as the active device. Then scroll down to “Launch Executable” and you should see a list of all the apps that are installed on your iPhone. Find the app that you want to run “Leaks” on (in this case, InstrumentsTest) and click it.
You’re now ready to go. Click on the red “Record” button and it will launch your application for you and start recording every memory allocation you do. It will also automatically check for leaks every 10 seconds.
You can change how often the automatic leak check runs, or you can set it to run only when you tell it too (when it checks for leaks, the entire app will freeze for about 3-5 seconds, so it can be annoying if you’re trying to play test and check for leaks at the same time). What I usually do is set it to manual control, and then hit the “Check for leaks” button whenever I need to (e.g. after loading a new game mode, after quitting the game back to the main menu, etc). Click on the Leaks row and use the View -> Detail button in the top right corned of the window to set/view options. For this example, I’m going to leave it on auto.
After the app has run for a few seconds, the auto leak check will run and lo and behold, it has found two memory leaks! Fantastic! What do we do now?
Extended Detail View
Instruments is very sneaky: it doesn’t make it obvious what to do next. What you need to notice is that row of buttons along the bottom of the window. See that little one made up of two rectangles? Hover your mouse over it and it will say “Extended Detail View”. (Note: You can also open this via View -> Extended Detail)
Click that button and a window opens up on the right-hand side of the screen that provides all kinds of handy details about your leaks!
Click on one of the memory leaks. The Extended Detail View will show you a complete stack trace to the point where your leaked memory was allocated. In our example above, clicking on the first leak reveals that a leak occurred inside [NSString initWithUTF8String]. If you look one step higher in the stack trace, you’ll see the last call inside my app was to [InstrumentsTestViewController viewDidLoad].
Here’s the really cool part, double-click on that line in the Extend Detail View and it opens an XCode window right to the culprit!
In this case we see that it was the first NSString allocation that leaked. Here’s where you need to do a bit of detective work. This is an extremely simple case, but it can get more tricky to find out why something’s leaky. Let’s take a closer look at our example.
In viewDidLoad we allocate some memory for the string, like this:
mMyLeakyString = [[NSString alloc] initWithUTF8String:"I'm a leaky string."];
And in dealloc, we release it like this:
[mMyLeakyString release];
So your immediate reaction might be that there shouldn’t be a leak. However, let’s search the code for all references to mMyLeakyString. That turns up this line inside doSomethingNow:
mMyLeakyString = [[NSString alloc] initWithUTF8String: "Look, another alloc, but no release for first one!"];
Notice that we’ve allocated a new string and assigned the pointer to mMyLeakyString. The problem is that we never released mMyLeakyString before it started pointing to something else. So the original string is now floating around on the heap and we have no way of freeing that memory. What the release call inside dealloc is actually doing is freeing the 2nd string that we allocated in doSomethingNow, because that’s where the pointer is pointing.
So, to fix this, we might change doSomethingNow to something like this:
- (void) doSomethingNow { [mMyLeakyString release]; mMyLeakyString = [[NSString alloc] initWithUTF8String: "Look, another alloc, but released first one!"]; }
What this does is release the first string we allocated before we point mMyLeakyString to our new string. When you build and run your app in Instruments again, you’ll see there’s one fewer memory leak. Of course, there are probably some better ways to handle NSStrings in your project, but if you had to do it this way, this would fix it.
Let’s take a look at that second leak. Clicking on it again reveals the callstack of what led to the leak. Finding the last call inside our app shows that the leak came from inside the LeakyClass::LeakyClass() constructor:
Double-click that in the stack and it opens up the culprit in XCode again:
Here we see the constructor does a new of a LeakedObject. But what’s this? The destructor never deletes the pointer? Well that’s no good! For every new, there needs to be a corresponding delete! So let’s change the destructor to this:
LeakyClass::~LeakyClass() { if (mLeakedObject != NULL) { delete mLeakedObject; mLeakedObject = NULL; } }
Build and run your app through Instruments again and you should be memory leak free!
I’ve chosen these two examples, even though they’re both very simple, because they show that Instruments can be used to track down memory leaks both in your Objective-C objects, as well as your C++ classes that are integrated into your app.
So go forth and fix your memory leaks! Because remember, a mem leak-free app is a happy app!
Similar articles:
发表评论
-
iOS App性能优化
2014-01-03 11:23 1733http://www.hrchen.com/2013/05/ ... -
iOS多线程编程Part 3/3 - GCD
2014-01-03 11:21 1683http://www.hrchen.com/2013/07/ ... -
iOS多线程编程Part 2/3 - NSOperation
2014-01-03 11:20 4589http://www.hrchen.com/2013/06/ ... -
iOS多线程编程Part 1/3 - NSThread & Run Loop
2014-01-03 11:17 7161http://www.hrchen.com/2013/06/ ... -
iOS移动网络环境调优那些事[转]
2014-01-02 17:10 2976http://xiangwangfeng.com/201 ... -
生成APNS Service证书的步骤[转]
2013-05-23 09:19 5721要进行推送服务的第一件事就是获取推送证书。它用来对你通过SS ... -
xcode 环境,多工程联编设置【转】
2013-02-28 21:59 9268http://blog.csdn.net/vienna_zj ... -
干掉你程序中的僵尸代码【转】
2012-12-22 11:05 1005随着万圣节越来越流行,我感觉有必要跟大家讨论一下一个 ... -
一个文本框搞定信用卡相关信息的输入[转]
2012-12-22 11:03 1178http://beforweb.com/node/134 ... -
【转】深度技术分析“为什么ios比android流畅”
2012-09-23 19:41 1456原文 Andorid更新了一个版本又一个版本,硬 ... -
Iphone开发
2012-09-17 22:46 12391. NSClassFromString 这个方法 ... -
HowTo: Install iPhone SDK 2.0 – 3.1 for XCode 3.2[转]
2012-09-06 09:00 1249原文链接 So… you’ve installe ... -
Xcode 中设置部分文件ARC支持[转]
2012-08-03 10:57 1755ARC是什么 ARC是iOS 5推出的新功 ... -
xcode4 设置调试错误信息小结【转】
2012-07-19 14:37 1839方案1:NSZombieEnabled 先选中工程, ... -
[Cocoa]XCode的一些调试技巧【转】
2012-07-19 14:35 1236XCode 内置GDB,我们可以在命令行中使用 GDB ... -
[IPhone]如何使用Leak检查内存泄漏[转]
2012-07-19 14:34 1281简介 在IPhone程式开发中,记忆体泄漏(内存泄漏)是 ... -
获得通讯录中联系人的所有属性[转]
2012-06-21 14:04 1651获得通讯录中联系人的所有属性 ,看代码: ABAdd ... -
多个UIViewController使用addSubView,第二个 UIViewController 不响应旋转[转]
2012-06-20 23:51 16465------------------------------- ... -
shouldAutorotateToInterfaceOrientation 不触发或者不执行的问题[转]
2012-06-20 22:58 1483今天遇到一个很郁闷 ... -
UIViewController生命周期-学习笔记[转]
2012-06-20 22:57 1176UIViewController生命周 ...
相关推荐
Finding Memory Leaks Issues with System RAM Issues with Battery Life Power Measurement Options Sources of Power Drain Addressing Application Size Issues Crash Reporting Using ACRA In-App Diagnostics ...
Finding Memory Leaks Issues with System RAM Issues with Battery Life Power Measurement Options Sources of Power Drain Addressing Application Size Issues The Role of Scripting Languages The Scripting ...
05.zip Finding memory leaks 发现内存的泄漏(6KB)<END><br>6,06.zip Convert message ID to a string 将消息标志符转换成字符串(4KB)<END><br>7,07.zip Message Tracer 消息跟踪(5KB)<END><br>8,...
内容概要:本文探讨了利用电动汽车参与电网削峰填谷的多目标优化调度策略。通过MATLAB平台,使用YALMIP工具箱和CPLEX求解器构建了一个精确的数学模型,旨在同时降低用户的综合成本、减少电网峰谷差并稳定负荷波动。文中详细介绍了目标函数的设计思路,包括各子目标的权重分配方法及其背后的考量因素;电池退化成本的计算方式;关键约束条件如SOC连续性和峰谷限制的具体实现;以及最终优化结果带来的显著效益。此外,还分享了一些实用技巧,比如如何选择合理的初始值以提高求解效率。 适合人群:电力系统研究人员、智能电网工程师、从事电动汽车相关工作的技术人员。 使用场景及目标:适用于需要解决电网负荷不均衡问题的研究机构或企业,特别是在推广新能源汽车过程中面临较大挑战的城市和地区。本研究的目标在于提供一种有效的解决方案,既能够保障车主利益最大化,又可以促进能源的有效利用和社会可持续发展。 其他说明:文中提供的完整代码和详细的说明文档有助于读者快速理解和应用该优化策略。未来还可以进一步探索加入更多变量(如天气预报、光伏发电量)的可能性,使模型更加完善。
实训商业源码-游戏扫码登录多功能工具箱集合微信小程序源码-毕业设计.zip
内容概要:本文详细介绍了COMSOL激光烧蚀脉冲激光打孔包及其在工业生产中的应用。首先阐述了激光打孔技术的优势,随后重点讲解了两个核心模块——动网格模块和固体传热模块的功能和作用。动网格模块用于模拟激光束在材料中的传播路径、烧蚀区域的形成及材料表面的热交换过程;固体传热模块则专注于模拟材料在激光烧蚀过程中的热传导特性,帮助优化工艺参数并提高加工效率。文中还列举了具体的案例分析,展示了该技术在金属材料加工中的广泛应用。最后讨论了技术的优势与面临的挑战,并对未来的发展进行了展望。 适合人群:从事材料加工、激光技术研究的专业人士,以及希望深入了解COMSOL软件应用的研究人员和工程师。 使用场景及目标:适用于需要进行高精度、高效率金属材料加工的企业和个人。通过学习和应用COMSOL激光烧蚀脉冲激光打孔包,可以优化工艺流程,提升产品质量和生产效率。 其他说明:尽管COMSOL提供了强大的建模和分析功能,但在实际操作中还需注意模型复杂度的控制和仿真结果的准确性验证等问题。
yudao ruoyi-pro 的 CRM 客户管理模块初始化 SQL 包含客户全生命周期管理核心表结构及基础数据,支持客户信息、商机、联系人及合同管理。资源涵盖客户表(crm_customer)、商机表(crm_business)、联系人表(crm_contact)、合同表(crm_contract),集成客户分群、跟进记录、公海池回收规则、合同审批流程等数据模型。内置示例客户数据、测试商机线索及合同模板,通过 crm_ 前缀表实现模块解耦。适配 Ruoyi 权限体系,提供角色-客户权限绑定、菜单路由初始化,支持客户分配流转、跟进时间线、销售漏斗分析及业绩统计功能,适用于企业销售管理、客户资源维护及跨部门协作场景快速搭建。
cJSON.c开源文件
内容概要:本文档为计算机网络学习提供全面的资源整合指南,涵盖学习资料推荐、题目合集与考点解析、实验项目指导以及工具使用教程。学习资料包括核心教材与课件、题库与解析、慕课资源,重点讲解网络分层模型、IP分段与重组、路由算法等知识点。题目合集部分针对选择题和综合设计题提供了典型题目及其答案参考来源,帮助学生掌握网络拓扑结构、IP地址划分、传输层协议等内容。实验项目指导则详细介绍了验证性实验和设计性实验的操作步骤,如IP地址配置与连通性测试、ARP协议解析、TCP连接分析等。工具使用教程包括Wireshark抓包过滤器语法和高级功能,以及Cisco Packet Tracer的基础操作和进阶应用。最后,文档还给出了从入门到深化阶段的学习路径建议。; 适合人群:计算机网络初学者、有一定基础的学生或技术人员。; 使用场景及目标:①作为计算机网络课程的辅助学习材料;②用于备考相关认证考试;③为实际网络工程设计和故障排查提供实践指导。; 阅读建议:建议按学习路径逐步深入,先掌握基础知识和基本命令,再进行实验操作,最后利用工具深入分析协议和网络行为。
012204124_王韵淇_仿真报告.docx
内容概要:本文详细介绍了基于Matlab/Simulink 2021b的三相PWM逆变器电压电流双闭环控制系统的设计与仿真。主要内容涵盖主电路设计(采用IGBT)、坐标变换(Clarke变换和Park变换)、电压电流双环PI控制器以及SVPWM控制。特别关注了在0.2秒时突加负载情况下系统的抗扰性能,展示了系统在突加载情况下的快速恢复能力和稳定性。文中还分享了一些调试经验和优化技巧,如在Park变换后加入低通滤波器以减少高频噪声干扰,调整仿真步长以提高仿真的精度和效率。 适合人群:电力电子工程师、自动化控制领域的研究人员和技术人员,尤其是对逆变器控制和仿真感兴趣的读者。 使用场景及目标:适用于需要深入了解三相PWM逆变器控制原理和仿真实现的研究人员和技术人员。目标是掌握电压电流双闭环控制的具体实现方法,了解SVPWM的工作机制,并能进行有效的抗扰性测试。 其他说明:本文不仅提供了详细的理论背景,还包含了具体的实现步骤和调试经验,有助于读者更好地理解和应用相关技术。
内容概要:本文详细介绍了利用IEEE73节点三区输电网进行暂态仿真和调频调压的研究方法。首先,通过Python加载IEEE73节点的数据并解析关键节点信息,特别是第15号节点作为区域间联络枢纽的作用。接着,通过PSCAD设置故障条件,如三相短路故障,来模拟暂态过程,并分析发电机转速波动情况。对于调压部分,采用Python进行灵敏度分析,识别对系统电压最敏感的机组。最后,使用MATLAB计算系统的动态响应,探讨不同区域之间的耦合效应及其对频率恢复的影响。文中还强调了模型校验的重要性以及参数设置的精确性。 适合人群:电力系统研究人员、电网工程师、高校相关专业师生。 使用场景及目标:适用于需要深入了解输电网暂态特性和调频调压机制的研究人员和技术人员。目标是掌握如何通过仿真工具分析复杂电网行为,优化电网运行性能。 其他说明:建议初学者从简单的单机无穷大系统入手,逐步深入到完整的三区输电网模型,确保理解和应用的准确性。
OneView - RAW和YUV看图工具,支持Tiny ISP处理和格式转换
内容概要:文章详细介绍了渤海大学首胜队参加第九届“飞思卡尔”杯全国大学生智能汽车竞赛的情况。该竞赛涵盖控制、模式识别、传感技术、电子、电气、计算机、机械等多个学科,旨在推动汽车智能化技术的发展。文中首先阐述了智能汽车的研究背景及其重要性,强调智能化是未来汽车发展的趋势。接着介绍了基于MK60DN512VLQ10微处理器的智能车设计方案,包括机械设计(悬挂、轮胎、舵机等)、电路设计(电源管理、电机驱动、红外检测、摄像头、编码器等)和动作设计(腾空飞跃、漂移过弯、走双边桥、过转盘、侧边行车、漂移入位)。文章还展示了系统框图,详细列出了各模块的功能和连接方式。最后总结了团队在整个项目中的收获,包括遇到的问题、解决方案以及团队协作的经验。 适合人群:对智能汽车竞赛感兴趣的学生和技术爱好者,尤其是参与类似竞赛或研究项目的人员。 使用场景及目标:①帮助读者了解智能汽车竞赛的具体流程和技术要求;②为准备参加此类竞赛的团队提供参考案例和技术支持;③展示如何将多学科知识融合应用于实际工程项目中。 其他说明:本文不仅提供了详细的硬件设计和软件编程指导,还分享了团队在比赛过程中积累的经验教训,强调了团队合作的重要性。此外,文章附有详细的参考文献列表,方便读者进一步查阅相关资料。
基于SpringBoot+Vue前后端分离的Java快速开发框架。移动端采用Vue、Uniapp、Uview。PC端采用Vue、Element UI。后端采用Spring Boot、Mybatis、Spring Security、Redis & Jwt。推荐使用Camunda实现工作流。推荐使用Websocket实现即时通讯。推荐使用OSS、COS实现对象存储。推荐使用Luckysheet实现Excel拖拽赋值的Web数据录入。推荐使用Vxe-table实现单行编辑,即时保存效果。推荐使用ECharts,UCharts实现数据可视化图表。推荐使用DataV展示可视化大屏数据。推荐使用IReport实现复杂报表导出Pdf、Execl,Word。推荐使用UReport实现自定义报表设计。推荐使用Hiprint实现自定义报表打印设计。推荐使用kkFileView实现在线预览,支持doc,docx,Excel,pdf,txt,zip,rar,图片等。使用OAuth2实现三方应用授权。支持多种登录方式(微信扫码登录,微信授权登录,验证码登录,密码登录)。支持微信、支付宝等第三方支付。支持加载动态权限菜单,控制菜单权限,按钮权限,数据权限。高效率开发,使用代码生成器可以一键生成前后端代码。
实训商业源码-青苹果-毕业设计.zip
实训商业源码-漫画小程序-毕业设计.zip
内容概要:本文详细介绍了全新的直流无刷电机控制器硬件设计方案,涵盖智能调速、正反转控制、多级保护(如限流、启动力矩设置、电压保护、电流保护、温度保护)以及简易调试等功能。硬件设计采用模块化的H桥驱动和信号调理,选用耐用的MOSFET IRFS7530和DRV8301驱动芯片,确保高精度控制和稳定性。调速算法采用分段式PID,启动阶段使用模糊控制,高速阶段则使用经典PID,确保启动平稳和运行稳定。保护机制方面,采用了快速响应的电流采样和温度预测算法,能够在极端情况下迅速切断输出,保障安全。此外,提供了详细的调试手册,帮助用户轻松完成调试。 适用人群:适用于从事电机控制系统设计的技术人员、电子工程师及相关领域的研究人员。 使用场景及目标:本方案旨在提供一种高性能、易调试的直流无刷电机控制器解决方案,适用于需要精确控制和多重保护的应用场景,如工业自动化设备、机器人等领域。 其他说明:调试手册配有生动的RAP口诀,便于初学者快速掌握调试技巧,使整个调试过程更加直观和高效。
实训商业源码-可乐个人发卡源码2.0v修复版-毕业设计.zip