简体   繁体   English

Java如何在Applet模式下使用这么多的内存?

[英]How does Java manage to use so much memory in Applet mode?

I;ve been writing a Java swing applet that is to be deployed in a browser (which ios pretty normal for applets, granted). 我一直在编写要在浏览器中部署的Java Swing小应用程序(对于小应用程序,这是ios正常的做法)。 It uses Java 2D for 它使用Java 2D

Anyway, I have a test harness for development purposes, which runs it as a desktop app. 无论如何,我有一个用于开发目的的测试工具,可以将其作为桌面应用程序运行。

Based on exactly the same test data, the Test Harness version has a heap total size 18 Meg; 根据完全相同的测试数据,Test Harness版本的堆总大小为18 Meg; this is based on drawing about 7000 objects on a Java 2D canvas with perhaps 30,000 coordinate pairs, plus other bits and pieces, so an 18 Meg heap is big but just about understandable. 这是基于在Java 2D画布上绘制大约7000个对象以及大约30,000个坐标对以及其他点点滴滴的结果,因此18 Meg堆很大,但是几乎可以理解。 Total app size is 40 Meg. 应用程式总大小为40 Meg。

Now I run exactly the same code as an Applet, via IBM Websphere. 现在,我通过IBM Websphere运行与Applet完全相同的代码。

The delta in memory for the plug-in rises to about 160Meg! 插件的内存增量大约为160Meg! Somehow the same java code is managing to use 10 times the memory. 不知何故,相同的Java代码设法使用10倍的内存。

The old CBM64 programmer in me is not particularly impressed at the first figure - it's an order of magnitude bloated IMO, but the second is amazing - does anyone have a clue what could possibly be using so much memory? 我对旧的CBM64程序员的印象并不特别深刻-它是一个数量级肿的IMO,但第二个令人惊奇-有人知道可能会使用这么多的内存吗? I'm looking with VisualVM, and it's helpfully putting things like Object, char[] and String as the memory hogs, none of my classes come close. 我正在使用VisualVM,它有助于将诸如Object,char []和String之类的内容放入内存中,而我的类都没有关闭。

Interestingly, Float and Double seem to take up exactly the same amount of memory (16 bytes each.). 有趣的是,Float和Double似乎占用了完全相同的内存量(每个内存16个字节)。

My main guess right now is that the data retrieval using SOAP is causing the huge spike in memory use, and for reasons unknown the SOAP XML is being retained instead of GC'd. 我现在的主要猜测是,使用SOAP进行数据检索导致内存使用量激增,并且由于未知的原因,将保留SOAP XML而不是GC。

Does anyone else out there have a clue as to what is going on here? 还有其他人对这里发生的事情有任何线索吗?

Found it - or a bit thereof. 找到了-还是有点。

When you use Axis 1.3 to make a SOAP call - to fetch the data in the first place - it doesn't deallocate the SOAP XML until the next call. 当您使用Axis 1.3进行SOAP调用时-首先要获取数据-在下一次调用之前它不会释放SOAP XML。

Unfortunately, in this case there was only an initial data population call, so Axis was hanging onto ~150Meg of XML. 不幸的是,在这种情况下,只有一个初始数据填充调用,因此Axis挂接到了约150Meg的XML上。 Simple enough workaround, make a second 'empty' call to clear it. 足够简单的解决方法,再进行一次“空”调用以将其清除。 All fine once GC'd. GC一切都很好。

The JVM always allocates a pool of memory that is larger than it needs or is actually likely to be using at any given time. JVM总是分配一个内存池,该内存池大于其所需的内存,或者实际上可能在任何给定时间正在使用。 Any external measure of the memory usage of the JVM is likely to be misleading. JVM的内存使用情况的任何外部度量都可能会引起误解。

The JVM does this for a few fairly sensible reasons: JVM出于一些相当明智的原因执行此操作:

  • It enables the garbage collector to run more efficiently when it has more space to allocate / move objects about 当垃圾收集器有更多空间分配/移动对象时,它可使垃圾收集器更有效地运行。
  • Even if the GC clears some space, it makes sense to keep the space reserved on the assumption that the program is likely to need it again 即使GC清除了一些空间,在程序可能再次需要它的前提下保留保留空间也是有意义的
  • The GC avoids unnecessary work by collecting garbage lazily - unless the GC is forced to perform a collection then old objects can exist on the heap for quite some time. GC通过延迟收集垃圾避免了不必要的工作-除非强制执行GC,否则旧对象可以在堆上存在相当长的时间。 These objects aren't doing any harm - the space can be reclaimed later if needed. 这些对象没有任何危害-以后可以根据需要回收空间。

You are probably just seeing the result of this bahaviour. 您可能只是看到此行为的结果。 Unless you are actually encountering problems (like OutOfMemoryError - which could suggest you have a memory leak) then don't worry about it. 除非您实际上遇到问题(例如OutOfMemoryError-可能表明您存在内存泄漏),否则不必担心。

ps the reason that Float and Double objects both take 16 bytes each is most likely that a) they have an object header and b) they are then padded to ensure cache line / memory addressing alignment. ps浮动对象和双精度对象都占用16个字节的原因很可能是a)它们具有对象标头,b)然后对其进行填充以确保高速缓存行/内存寻址对齐。 If you pack a lot of floats / doubles in an array then you will find they take 4 and 8 bytes respectively. 如果在数组中打包很多浮点数/双精度数,那么您会发现它们分别占用4和8个字节。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM