Is it possible to change Garbage Collection behavi

2019-07-15 10:32发布

I was wondering if it is possible to tweak the way garbage collector works on JavaMe in order to improve performance somehow(may reducing the number of passages)? I´ve seen some articles about it, but mostly directed Java SE and most of them says GC is highly dependant on the maker. How much would that be.

3条回答
啃猪蹄的小仙女
2楼-- · 2019-07-15 10:53

Thanks for all the input people. I´ve seen it´s not possible to tweak the GC for Java Me capable devices. At least not in any easy or usefull way. I´ll leave this question open anyway, in case this scenario changes somehow in the time being. :)

查看更多
祖国的老花朵
3楼-- · 2019-07-15 11:09

I don't see any possible way to tweak the garbage collection behavior in J2ME.

If you have problems with pauses induced by garbage collections, you have to do two things:

  • Reduce the garbage that your program produces, by re-using objects, and avoid costly things like string concatenations (use StringBuffer instead)
  • Use System.gc() to force garbage collection when a little freeze doesn't matter (for example, during the "loading" screen of a game), to reduce the odds that the collector process is triggered when it's annoying.
查看更多
时光不老,我们不散
4楼-- · 2019-07-15 11:10

When the garbage collector is triggered is largely a mistery unless you have first hand knowledge of how the particular VM your application is running on was implemented and exactly how it was configured and customized for the specific phone you are using.

Calling java.lang.System.gc() is no guarantee that the garbage collector is triggered. It usually merely increases the probablility of the VM launching garbage collection very soon.

I have found that calling System.gc() 3 times consecutively in the same Thread but from 3 different methods tends to work reasonably well.

There are many ways to work around ineficiencies in the JavaME standard API implementations in order to reduce the amount of garbage you generate:

  • Extend ByteArrayOutputStream so you don't create a copy of the byte array when you want to access the data.

  • avoid calling StringBuffer.getChars() and StringBuffer.toString(). make your code work with StringBuffer offsets and lengths instead.

  • turn local buffers (byte[], StringBuffer...) into instance or static variables (and protect them with synchronized). Obviously, there is an overhead to doing this but it can prevent your application from freezing due to garbage collection too often.

  • Extend StringBuffer to avoid switching back and forth between String and StringBuffer : implement append(String, offset, length), parseInt(int), indexOf(String, index), replace(offset, StringBuffer, offset, length)...

...

查看更多
登录 后发表回答