游戏邦在:
杂志专栏:
gamerboom.com订阅到鲜果订阅到抓虾google reader订阅到有道订阅到QQ邮箱订阅到帮看

游戏开发者需要注意的4个内存使用问题

发布时间:2015-09-29 15:28:23 Tags:,,,,

作者:Aditya Kulkarni

从销售角度来看,你的全新手机游戏总是应该适用于今天人们所使用的大多数设备。“大多数设备”可能会让开发者感到惊讶。在2015年1月,世界各地的手机用户数已经达到36亿人。所以开发者的工作便是让游戏/应用能够适应这些设备的种种硬件约束条件。开发者们在开发像游戏等需要耗费内存的软件时需要进行更多思考。通过编写这篇文章,我希望能够帮助一些不了解技术的人进一步理解一些内存问题,而这也是他们在致力于2D游戏项目时需要考虑的内容。

1)内存泄露:内存泄露指的是未被释放的分配内存块。

类比:让我们想象你在一个拥有3个方形储物柜的房间寻找一个储物柜。

lockers(from gamecareerguide)

lockers(from gamecareerguide)

1.John和Danny“需要”储存自己的装备,而一个储物柜便足以满足他们每个人。所以现在我们拥有2个已被使用的储物柜,以及1个闲置的储物柜。

2.这时候John需要储存他的头盔,并且不再需要他之前储存的装备。所以现在最理想的情况是他应该拿出之前的装备并将头盔装进去。但是他却向管理者请求另一个储物柜并希望将头盔放在全新储物柜中。从逻辑上看,在这之后所有的储物柜便都满了。而现在的John会忘记自己储存在第一个储物柜中的装备。

3.现在Danny需要储存2个头盔并需要2个储物柜,所以他决定拿掉自己的装备并空出1个储物柜。然后他向管理者申请了一个新的储物柜,但是因为所有储物柜都满了所以遭到了管理者的拒绝。

在这里,即使John从储物柜中拿掉无用的装备,Danny也不能再使用多出来的储存空间。

2)相同纹理的多个例子:一个纹理将占用宽度*高度*4(游戏邦注:红色,绿色,蓝色和透明度各占据1个字节)的储存空间。所以一张512×512规格的图像将占据1.05兆的内存。而同样的纹理将占据其3倍的储存空间,也就是3.15兆。所以如果我们想要使用这些图像作为纹理的话我们便需要准备多出3倍的内存。

类比:让我们以一张呈现出你最喜欢的角色“buttman”并且不能无限次使用的方形贴纸为例。

sticker(from gamecareerguide)

sticker(from gamecareerguide)

你需要将这个新角色呈现给3个不同的人。所以你能够:

1.获得2张一样的贴纸,获得3个木质框架能够在上面贴贴纸,并且你将把这些木制框架递交给那3个人。

2.让那3个人去获得属于自己的木制框架并让他们将贴纸贴到框架上。

第2个例子适合我们,因为我们可以反复使用同样的贴纸。这也适用于软件程序中,即所有人可以同时想到buttman。

3)不必要的迭代:比起多次调用,在资源中调用for/while循环更简单。你可以在一个循环中处理多个任务。

类比:你是一个间谍,并需要向3个遥远的不同场所运输包裹。并且你需要从这3个不同场所的不同boss那收集到重要的秘密信息。

因为你是从不同boss那接到这些任务,所以你可以:

1.根据不同boss去分配你的任务。例如你可以驾车前往所有的这3个场所并先完成boss A所分配的任务,然后再驾车前往同样的3个场所完成Boss B所分配的任务。反之亦然。

2.根据不同场所去分配你的任务。例如你可以先驾车前往第一个场所,递交包裹并获得信息,然后再驾车前往下个场所。

如果你所制定的任务计划是像第1种情况那样的话你便是一个愚蠢的间谍,因为你需要花费更多时间和资源去完成同样的任务。

4)在内存中保留未使用的对象:任何时候都只有一定的对象能够被使用或出现在屏幕上。因为我们拥有优先的内存空间,所以任何被留在内存里的额外对象都是在耗费资源。

类比:你需要将一辆马车从A点推到B点。让我们假设如果马车未负载任何东西或者载着最轻的东西,这便是一个较简单的任务。但是因为你喜欢啤酒,所以你选择用马车去载一桶啤酒,从而加重了自己任务的难度。

所以在这里的最佳解决方法便是放弃你所深爱的啤酒。

这里所列出的问题绝对超过我在一篇文章中能够写下的内容。作为一名开发者,你应该尽快识别这些问题并使用有效的解决方法。先克服这些挑战再发行产品是真正让人兴奋的游戏开发过程。每个人每一天只拥有24个小时。对于我们而言,人们能够花费这些宝贵的时间去尝试我们所创造的内容便是推动着我们去开发游戏的最佳动力。

本文为游戏邦/gamerboom.com编译,拒绝任何不保留版权的转发,如需转载请联系:游戏邦

Four Memory Problems to Watch Out for in Game Development
[08.20.15]

- Aditya Kulkarni

From a sales point of view, your cool new mobile game should work on most of the devices used by people today. The term ‘most of the devices’ evokes a ‘yikes’ by a developer. As of Jan 2015, the number of unique mobile users was 3.6 Billion. It’s the job of the developer to make the game/application work within the hardware constraints of a lot of those devices. Developers need to put a lot of thought while developing memory intensive software like games. By writing this article, I hope to help a non-tech person understand some of the memory problems that should be taken into account when developing 2D game projects.

1) Memory Leak: A memory leak can be defined as an allocated memory block which has not been released.

Analogy: Let’s imagine a cabinet that you find in a locker room having a total of 3 perfectly square lockers.

1.John and Danny ‘need’ to store their equipment for which a single locker suffices for each of them. So we now have 2 lockers that have been used and 1 unused locker which has free space for storage.

2.John needs to store his helmet at this point and no longer needs the old equipment that he stored before. Now ideally, he should be taking out the older equipment and putting his helmet in it’s place. But he asks for another locker from the manager and decides to put the helmet in the new locker. Logically, all the lockers are used up after this. Having been gifted with the memory of a warthog, John forgets about the equipment that he had stored in the first locker.

3.Danny needs to store 2 helmets now and needs 2 lockers, so he decides to remove his older equipment and frees up 1 locker. He now asks for an extra locker from the manager, but is refused since all the lockers have been used up.

Here, Danny would not be able to utilize the storage space that would have actually been available if John had removed the unused equipment from the locker.

2) Multiple instances of the same texture: A texture takes up width*height*4 (1 byte each for red, green, blue and alpha) of memory space. So an image of size 512×512 will take up 1.05 MB of memory. The same texture used 3 times would then take 3.15 MB of memory. So we would be using 3 times more memory if we used those many instances of the image as a texture.

Analogy: Consider a square sticker which shows your favourite character, ‘buttman ‘and can be reused for an infinite number of times.

You need to show this cool new character to 3 different people. So you can either:

1.Get 2 more stickers which are exactly the same, get 3 wooden frames on which you plan to stick the stickers and send the wooden frames to those 3 people.

2.Ask the 3 people to get their own wooden frames and just let them put the sticker on their wooden frames to see how it looks.

Case 2 works for us since we can just reuse the same sticker multiple times. It actually works in parallel in case of a software program, so all of them can look at buttman at the exact same time. Magic, yeah.

3) Unnecessary Iterations: Calling a for/while loop once is easier on the resources than calling it multiple times. Batch multiple tasks in one loop if you can.

Analogy: You are a spy assigned to deliver packages to 3 different, far off locations in tropical heat. You have also been asked to get top secret info by a different boss from the same 3 locations above.

Since both the tasks have been assigned to you by different bosses, you can either:

1.Group your mission according to the boss i.e. You would drive over to all 3 locations to complete the task given by Boss A first, then drive over again to the same 3 locations to complete the task given by Boss B. Or vice versa.

2.Group your mission according to the locations. i.e. You would drive to the first location, deliver the package and get information, then move on to the next location.

You would be a damn stupid spy if your mission plan was Case 1 due to the simple fact that it takes a longer time and more resources to complete the same set of tasks.

4) Keeping unused objects in memory: At any given point in time, only a definite number of objects will be used and / or shown on the screen. Since we have limitations on the memory that can be used, any additional objects still in memory are a drain on resources and should be released.

Analogy: You need to push a cart from point A to point B. Let’s assume that the task at hand would be easier if the load on the cart is non-existent or lighter at the very least. Since you love beer, you load up the cart with a barrel of beer anyway, which just makes the task at hand harder, you alcoholic nitwit.

So the best solution would be to not load up the barrel even if your emotions say otherwise.

The list of problems is definitely more than I could write about in a single article. As a developer, you should be able to identify these problems quickly and apply the needed solution. Shipping the product after overcoming these challenges is what makes game development exciting. Everyone has 24 hours in a day. For us, it’s the idea of other human beings spending and enjoying some of those precious hours of their lives on something that we create, that pushes us to develop games for the populace to consume. (source:gamecareerguide

 


上一篇:

下一篇: