都是原始类型的垃圾收集在Android的? [英] Are primitive types garbage collected in Android?

查看:176
本文介绍了都是原始类型的垃圾收集在Android的?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我知道这可能是一个愚蠢的问题,但我的背景是更多的C ++和管理自己的记忆。

I know this may be a dumb question, but my background is more in c++ and managing my own memory.

我目前砍伐的每一个配置,我可以从我的游戏,试图减少垃圾收集的频率和感知的滞后,所以对于每个变量,我创建的是一个对象(字符串和矩形一例如)我要确保我的手之前创建它在我的构造函数,而不是建立在简单的10个临时变量 行功能...(我希望是有道理的)

I am currently cutting down every single allocation that I can from one of my games to try and reduce the frequency of garbage collection and perceived "lag", so for every variable that I create that is an Object (String and Rect for example) I am making sure that I create it before hand in my constructor and not create temporary variables in simple 10 line functions... (I hope that makes sense)

反正我是工作,虽然今晚多一些,我意识到,我可能是完全错误的关于我的垃圾收集和原始类型(int,布尔型,浮点型)是我在10号线函数来创建这些原始类型变量的假设被调用20 次,第二次增加垃圾收集我的问题?

Anyways I was working though it some more tonight and I realized that I may be completely wrong about my assumption on garbage collection and primitive types (int, boolean, float) are these primitive type variables that I create in a 10 line function that gets called 20 times a second adding to my problem of garbage collection?

所以,一年前每几秒钟我就看到logcat的消息像

So a year ago every few seconds I would see a message in logcat like

GC释放4010对象/在484064字节   101ms

GC freed 4010 objects / 484064 bytes in 101ms

现在我看到消息每隔15-90秒左右...

Now I see that message every 15-90 seconds or so...

因此​​,要改写我的问题:是否原始类型(整型,浮点,布尔,等等)看到这条消息时,包括

So to rephrase my question: Are primitive types (int, float, boolean, etc) included when seeing this message?

推荐答案

基本类型不是对象,所以他们不会造成任何垃圾收集。但是,你必须要非常小心,因为由于拳击基本类型很容易成为一个对象没有你明确地这样做。

Primitive types are not objects, so they do not cause any garbage collection. However, you do have to be very careful because due to boxing a primitive type can easily become an object without you explicitly doing so.

例如,如果你想要一个HashMap<>整数键,您将使用HashMap中。注意,因为整数是不是一个对象,它不能在一个容器中使用。 Integer是一种原始的一个int对象版本。当你写code这样的,一个Integer对象将自动为你创建:

For example, if you want a HashMap<> of integer keys, you would use HashMap. Note that because "int" is not an object, it can't be used in a container. Integer is an object version of a primitive int. When you write code like this, an Integer object will automatically be created for you:

HashMap<Integer, Object> map = new HashMap<Integer, Object>();
int someNum = 12345;    // no object created.
map.put(someNum, null); // Integer object created.

需要注意的是同样的事情会发生,如果你不使用泛型,但更隐蔽:

Note that the exact same thing will happen if you don't use generics, but even more hidden:

HashMap map = new HashMap();
int someNum = 12345;    // no object created.
map.put(someNum, null); // Integer object created.

有关这种特殊情况下,可以使用Android的SparseArray类,这是基本整数键的容器。

For this particular situation, you can use Android's SparseArray class, which is a container of primitive integer keys.

这篇关于都是原始类型的垃圾收集在Android的?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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