LWUIT-JavaME 中的内存不足异常
我正在使用 Netbeans IDE 在 Java ME 中开发一个应用程序,其中我创建了 3 个表单,其中包含 Button
、Command
(确定并返回)和 Image表单
中的code>。
使用 OK Command
我可以将控制转移到第二种形式。这
在第二个 Form
中有效,我有 2 个 Command
OK 和 Back。单击“确定”后,它应该转到第三个表单
。单击返回后,它应该返回到第一个表单
。
当我处于第二个Form
时,我既无法转移到第三个Form
,也无法转移到第一个Form
。
我收到以下异常........................
java.lang.OutOfMemoryError
(堆栈跟踪不完整) 过渡绘制期间发生异常,如果在过渡中间调整大小,这可能有效,
请帮助我。 提前致谢
I am developing an app in Java ME using Netbeans IDE, where I'm creating 3 Forms which contains Button
, Command
(OK and back) and Image
in the 1st Form
.
Using OK Command
I can transfer the control to the 2nd form. This works
In 2nd Form
I have 2 Command
OK and Back. On click of OK it should go to the 3rd Form
. On click of back it should return to the 1st Form
.
When I am in the 2nd Form
, I'm neither able to transfer to the 3rd nor to the 1st Form
.
I am getting the following exception.............
java.lang.OutOfMemoryError
(stack trace incomplete)
An exception occurred during transition paint this might be valid in case of a resize in the middle of a transition
Please help me out.
Thanks in advance
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
Sanjay 的答案是正确的,但是如果它不能解决问题,我猜你的代码中某个地方有递归,导致堆栈溢出。许多 J2ME VM 无法触发堆栈溢出异常,而是抛出内存不足错误。
只需使用调试器并浏览代码即可查看递归。
Sanjay's answer is correct however if it doesn't solve the problem I would guess you have a recursion somewhere in your code leading to a stack overflow. Many J2ME VM's fail in triggering the stack overflow exception and instead throw an out of memory error.
Just use a debugger and walk over the code to see the recursion.
根据您提供的信息,可能的问题是第一种形式的图像,您需要跟踪您正在使用的图像的大小。加载图像时,数据被解压缩为像素阵列。每个像素可能需要 2 到 4 个字节,具体取决于设备。假设 800x600 图像有 480,000 个像素,因此需要至少 1Mb 的堆(可能高达 2Mb)来加载它。这将解释为什么你的内存不足。在转换期间,LWUIT 可能会制作图像的本地副本来“转换”(重绘)它(尽管我不确定),因此如果您在转换时需要更多内存,这并不奇怪。
这是一篇可能对您有所帮助的好读物: LWUIT 中的内存泄漏和 Java ME 中的跟踪内存
With the informations you provided, The possible issue is with the image in the first form, you need to keep track of the size of the image you are using. When an image is loaded, the data is decompressed to a pixel array. Pixels can need anything from 2 to 4 bytes each, depending on the device.Say an 800x600 image has 480,000 pixels, so will need at least 1Mb of heap (possible as much as 2Mb) to load it. This will explain why you are running out of memory. During a transition it may be LWUIT makes a local copy of the Image to "transform" (redraw) it(though I'm not sure) so its not a surprise if you need more memory while transition.
Here is a good read which might help you : Memory Leaks In LWUIT And Tracking Memory In Java ME
您可以扩展模拟器的内存。您必须单击项目的属性/平台/管理模拟器/工具和工具。扩展/打开首选项/存储
在此窗口中,您将看到堆大小,在此处写入 1000 fe,然后重试。
You can extend the memory of the emulator. You have to click on Properties of the project / Platform / Manage emulators / Tools & extensions / Open preferences / Storage
In this window you will see heap size, write here 1000 f.e. and try it again.
当您切换到该类的第二或第三遍对象时,使用下一个表单构造函数显示对象(来自 mdilet),当您回调返回按钮时,只需编写一行代码,如
display.setCurrent(previous_form_object);
&当您单击“确定”命令时,您知道如何进入下一个表单。桑杰提到的一件事。谢谢
When you swiching to 2nd or 3rd pass object of that class & display object(which is from mdilet) with next form constructor, and when you calling back button just write one line of code like
display.setCurrent(previous_form_object);
& you know how to go next form when you clicking ok command. And one thing which is mentioned by Sanjay.Thanks