在ipad / iphone应用程序中使用众所周知的免费内存代码是否合法? [英] Is it legal to use the well-known free memory code in ipad/iphone app?

查看:145
本文介绍了在ipad / iphone应用程序中使用众所周知的免费内存代码是否合法?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

内存是iphone ipad应用程序的一个问题,特别是对于ipad,如果想要做大事。

Memory is an issue for iphone ipad app, especially for ipad, if one wants to do something big.

那么,这两个代码片段可以1.获取当前可用的设备内存; 2.强制释放记忆。

Well, these two snippets of codes can 1. get current available memory of the device; 2. force free memory.

我们可以在应用程序中使用它吗? appstore允许这个吗?

Can we use it in the app? Will appstore allow this?

#import <mach/mach.h>
#import <mach/mach_host.h>

static void print_free_memory () {
mach_port_t host_port;
mach_msg_type_number_t host_size;
vm_size_t pagesize;

host_port = mach_host_self();
host_size = sizeof(vm_statistics_data_t) / sizeof(integer_t);
host_page_size(host_port, &pagesize); 

vm_statistics_data_t vm_stat;

if (host_statistics(host_port, HOST_VM_INFO, (host_info_t)&vm_stat, &host_size) != KERN_SUCCESS)
NSLog(@"Failed to fetch vm statistics");

/* Stats in bytes */ 
natural_t mem_used = (vm_stat.active_count +
vm_stat.inactive_count +
vm_stat.wire_count) * pagesize;
natural_t mem_free = vm_stat.free_count * pagesize;
natural_t mem_total = mem_used + mem_free;
NSLog(@"used: %u free: %u total: %u", mem_used, mem_free, mem_total);
}

可用内存

/* Allocate the remaining amount of free memory, minus 2 megs * /
size_t size = freemem - (2*1024*1024);
void *allocation = malloc(size);
bzero(allocation, size);
free(allocation);


推荐答案

如果你问我,iOS会很好地管理它的内存资源。如果您的应用程序存在内存问题,则需要对这些内存问题进行概要分析并对其进行优化。

iOS manages its memory resources pretty well if you ask me. If you're having memory issues with your app you need to profile and identify what those memory issues are and optimise them.

从用户的角度考虑它。特别是在支持多任务处理的iPhone / iPad(4.2)上。用户在后台拥有他/她希望在一段时间内返回的应用程序。他们打开你的应用程序 - 它为自己腾出空间 - 然后他们试图回到他们的其他应用程序,却发现他们已被杀死,因为你的应用程序杀了他们。

Think of it from a user's perspective. Especially on multi-tasking capable iPhones/iPads (with 4.2). The user has apps in the background that he/she will want to go back to in some time. They open up your app - which nukes everything to make room for itself - and then they try to return to their other apps only to find they've been killed because your app killed them.

我不会太高兴,并且可能会强烈重新考虑将来再次使用你的应用程序。

I wouldn't be too happy and would probably strongly reconsider using your app again in the future.

Apple可能会有同样的看法。他们的工作是确保用户获得最佳体验,这就是他们开始制定规则和指南的原因。

Apple will likely have the same opinion. Their job is make sure the users have the best experience possible, which is why they have rules and guidelines to start with.

这篇关于在ipad / iphone应用程序中使用众所周知的免费内存代码是否合法?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆