AndEngine内存泄漏
我在我的应用程序中发现了一个问题: a 拥有场景 S1 和 S2 以及经理 M;从S1我可以构建并切换到S2,从S2可以得到S1(全部通过管理器)。
每次当我在场景之间切换时,我想删除其中一个。
但我发现,如果我在场景之间切换,两个场景都会存储在内存中,并且在多次切换后应用程序会崩溃。
一些伪代码:
Manager:
void build(Scene s){
getEngine().setScene(s);
}
onLoadScene{
M.build(S1);
}
S1:
onButtonClick{
M.build(S2);
}
S2:
onButtonClick{
M.build(S1);
}
正如我所想,当我构建 S1 时,到 S2 的链接消失了并且 当构建S2时,S1消失,如果内存不够,GC必须清理它。
如何避免应用程序崩溃?
非常感谢。
i found a problem in my app:
a have to scenes S1 and S2 and manager M; from S1 i can build and switch to S2 and from S2 can get S1 (all through manager).
Each time when i switch between scenes i want to delete one of it.
But i found that if i switch between scenes both scenes store in memory and after many switches app is crashed.
some pseudocode:
Manager:
void build(Scene s){
getEngine().setScene(s);
}
onLoadScene{
M.build(S1);
}
S1:
onButtonClick{
M.build(S2);
}
S2:
onButtonClick{
M.build(S1);
}
As i think when i build S1 link to S2 is dissapeared and
when build S2, S1 dissapeard and GC have to clean it if memory is not enough.
How can i avoid crashes of application?
Thx a lot.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
这些代码不足以帮助您找到内存泄漏。但我的猜测是你的纹理正在被重新创建,并且你的精灵仍然附着在场景上。您想要集中并重新使用您的资源。每次调用“新”任何内容或创建纹理图集时都要检查。在这些情况下,创建某种类来存储旧实例并重新初始化它们。 Andengine 可能对资源非常粘,除非您可以专门删除每个资源及其引用和依赖项。
然而,在大多数游戏中,您需要的子弹、敌人、背景图块等的数量都有限制。因此可以为每种对象类型设置池并回收它们。您不想做的是每次调用 build() 时创建新资产。
That's not enough code to help you find a memory leak. But my guess is your textures are getting recreated, and your sprites remain attached to the scenes. You want to pool and re-use your resources. Check for every time you are calling "new" anything, or creating a textureAtlas. In those cases make some sort of class that stores the old instances and reinitializes them. Andengine can be very sticky with resources unless you can specifically remove each one and all fo its references and dependencies.
However in most games there is a limit on the number of bullets, enemies, bg tiles, etc that you need. So its possible to set up pools for each object type and recycle them. Waht you DONT want to do is make new assets each time you cal build().