Skip to content
Skip to breadcrumbs
Skip to header menu
Skip to action menu
Skip to quick search
Quick Search
Browse
Pages
Blog
Labels
Attachments
Mail
Advanced
What’s New
Space Directory
Feed Builder
Keyboard Shortcuts
Confluence Gadgets
Log In
Sign Up
Dashboard
Groovy
Copy Page
You are not logged in. Any changes you make will be marked as
anonymous
. You may want to
Log In
if you already have an account. You can also
Sign Up
for a new account.
This page is being edited by
.
Paragraph
Paragraph
Heading 1
Heading 2
Heading 3
Heading 4
Heading 5
Heading 6
Preformatted
Quote
Bold
Italic
Underline
More colours
Strikethrough
Subscript
Superscript
Monospace
Clear Formatting
Bullet list
Numbered list
Outdent
Indent
Align left
Align center
Align right
Link
Table
Insert
Insert Content
Image
Link
Attachment
Symbol
Emoticon
Wiki Markup
Horizontal rule
tinymce.confluence.insert_menu.macro_desc
Info
JIRA Issue
Status
Gallery
Tasklist
Table of Contents
Other Macros
Page Layout
No Layout
Two column (simple)
Two column (simple, left sidebar)
Two column (simple, right sidebar)
Three column (simple)
Two column
Two column (left sidebar)
Two column (right sidebar)
Three column
Three column (left and right sidebars)
Undo
Redo
Find/Replace
Keyboard Shortcuts Help
<p><table class="wysiwyg-macro" data-macro-name="excerpt" data-macro-parameters="atlassian-macro-output-type=BLOCK|hidden=true" style="background-image: url(/plugins/servlet/confluence/placeholder/macro-heading?definition=e2V4Y2VycHQ6aGlkZGVuPXRydWV8YXRsYXNzaWFuLW1hY3JvLW91dHB1dC10eXBlPUJMT0NLfQ&locale=en_GB&version=2); background-repeat: no-repeat;" data-macro-body-type="RICH_TEXT"><tr><td class="wysiwyg-macro-body"><p>how not to crash the JVM or lock your process </p></td></tr></table><br /> The <a href="http://en.wikipedia.org/wiki/Component_Object_Model"><span style="color: rgb(0,0,255);">wiki on COM</span></a> explains, among many other things, that <strong>COM (Component Object Model)</strong> is an older Microsoft techonology that encompasses <strong>OLE</strong>, <strong>OLE Automation</strong>, <strong>ActiveX</strong>, <strong>COM+</strong> and <strong>DCOM</strong>. <strong>Microsoft.NET</strong> is not based on <strong>COM</strong>, but for the most part it supports it seamlessly. <strong>Scriptom</strong> lets you easily integrate these technologies into your <strong>Java/Groovy</strong> project.</p> <p>Despite the fact that <strong>Visual Basic</strong> versions up through 6 were abstractions on top of <strong>COM</strong>, it's not at all simple. I won't go into the details here (the <a href="http://en.wikipedia.org/wiki/Component_Object_Model"><span style="color: rgb(0,0,255);">wiki</span></a> does a better job than I could at explaining it all). The important point to remember is that <strong>COM</strong> is fundamentally a <strong>C/C++</strong> based technology that was developed using pre-Java ideas of object orientation. To be specific, <strong>C/C++</strong> apps are generally responsible for cleaning up allocated memory, and the <strong>COM</strong> threading models differ somewhat from <strong>Java</strong>.</p> <p>Because <strong>C++/COM</strong> and <strong>Java</strong> aren't quite compatible, it is possible to get <strong>Scriptom</strong> to randomly crash your JVM (core-dump), use up all available memory, peg the CPU at 100% for hours on end, or prevent your Java process from exiting. If you use it correctly, though, you'll find that <strong>Scriptom</strong> is rock-solid stable.</p> <p>This article is going to explain, in very simple terms, some of the differences between <strong>COM</strong> and <strong>Java</strong>, and how to deal with the issues they present in the simplest way possible. </p> <h1>Apartment Threads for Absolute Beginners</h1> <p>Sorry about this, but you have to manage <strong>COM</strong> thread apartment contexts. The good news is that this is not nearly as onerous as it sounds. You can do this using <strong>Jacob</strong> directly (giving you a lot of flexibility). Of course, this article is about the simple, safe ways to use <strong>Scriptom</strong>, so we'll leave the advanced <strong>Jacob</strong> topics for another.</p> <p><strong>Scriptom</strong> provides an even simpler and safer way that handles 95% of the cases you are likely to run into. It also tends to produce the most stable applications, making it ideal for use on the server side. All you have to do is wrap your code in a <strong>Groovy</strong> closure, like this:</p> <table class="wysiwyg-macro" data-macro-name="code" style="background-image: url(/plugins/servlet/confluence/placeholder/macro-heading?definition=e2NvZGV9&locale=en_GB&version=2); background-repeat: no-repeat;" data-macro-body-type="PLAIN_TEXT"><tr><td class="wysiwyg-macro-body"><pre> Scriptom.inApartment { ... } </pre></td></tr></table> <p>When you wrap your Scriptom code using <strong>.inApartment</strong>, the thread apartment is initialized if needed, and all <strong>COM</strong> resources are released when you are done. You can do this over and over again in a thread, and you can also nest calls to <strong>.inApartment</strong>, allowing you to use it safely with other code (this addresses an issue with <strong>Jacob</strong>). You'll see this pattern time and time again in our examples and test code.</p> <p>The main side-effect of this is that you have to define all your <strong>ActiveXObject</strong>s within the apartment scope (the closure). In case that doesn't work for your purposes, you can freely use <strong>Jacob</strong> to manage your <strong>COM</strong> thread contexts. However, you can't safely mix and match the <strong>Scriptom</strong> thread management with <strong>Jacob</strong> calls in the same thread. </p> <h1>It's NOT a Memory Leak!</h1> <p>It should come as no surprise that <strong>COM</strong> strings, numbers, and objects are not interchangable with <strong>Java</strong> strings, numbers, and objects (though <strong>Scriptom</strong> does a pretty good job of making them look as if they are). Every time we exchange data with a <strong>COM</strong> library, the <strong>Java</strong> data-type needs to be converted to and from a form that is suitable for <strong>COM</strong>. That form just happens to be Microsoft's (pseudo) universal data type: the <strong>Variant</strong>. Without going too deeply into the murky details, a <strong>Variant</strong> can store various forms of numbers, strings, and objects. You don't normally need to worry about this because <strong>Scriptom</strong> handles all the conversion details for you.</p> <p>What you need to know about <strong>Variants</strong> is that they can't be completely reclaimed by standard <strong>Java</strong> garbage collection. There is a little "residue" that <strong>Jacob</strong> has to hold on to even after you are finished using the <strong>Variant</strong>. This is necessary to prevent random JVM crashes (it's a <strong>C/C++/JNI</strong> thing). Over a large number of calls, this residue can fill all the memory on your server.</p> <p><em>Don't panic!</em> It isn't a memory leak. When you exit the <strong>.inApartment</strong> closure, the <strong>COM</strong> context is released and all the memory is reclaimed. </p> <p>There is one more thing you should be aware of. The clean-up process gets much slower if you make a very large number (millions) of <strong>COM</strong> calls before cleaning up. So for a long running thread, you are better off breaking up the apartment contexts rather than having one context for the whole thread.</p> <p>In short:</p> <ul> <li>Use an <strong>.inApartment</strong> closure around your <strong>COM</strong> code (more specifically, any code that uses <strong>ActiveXObject</strong>).</li> <li>Break <strong>.inApartment</strong> closures into reasonably-sized chunks.</li> </ul>
Please type the word appearing in the picture.
Attachments
Labels
Location
Watch this page
< Edit
Preview >
Loading…
Save
Cancel
Next hint
search
attachments
weblink
advanced