why jvm crashes if I invoke GC every 20000 allocations
I am doing a project related to object's lifetime. I modified JVM code to force garbage collection every 20000 allocations (i.e., every 20000 objects have been allocated). In my code, I used a global variable total_objs to track how many objects have been allocated so far.
I added the following code at the allocation point ( in function jvmpi_object_alloc() of universe.cpp)
if( total_objs% GCFreq == 0) {
//GCFreq=20000 here
Universe::heap()->collect(GCCause::_java_lang_system_gc);
}
But JVM always failed. Even I enlarge GCFreq, VM still crashed. Why?
Feng
[614 byte] By [
terryxiana] at [2007-10-2 17:09:54]

I can't tell you why "JVM always failed" without an exception.
I can tell you that you are not in any way, shape, or form "forcing"
garbage collection. You're asking the JVM to please, maybe,
if it feels like it, possibly garbage collect sometime in the near or
far future.
>
> But JVM always failed. Even I enlarge GCFreq, VM
> still crashed. Why?
The code you posted is not java.
And so the answer is the same for any C/C++ application problem - you have a memory/pointer problem. Could be almost anything but one possibility is that you are holding references in the C/C++ code to java objects incorrectly.
> I can't tell you why "JVM always failed" without an exception.
> I can tell you that you are not in any way, shape, or form "forcing"
> garbage collection. You're asking the JVM to please, maybe,
> if it feels like it, possibly garbage collect
> sometime in the near or far future.
The Sun VM, at least in 1.4 and 1.5, will do a collection.
Given the Sun VM can not support hotloading with shared libraries without allowing that I suspect it will continue.
Other VMs might have a different solution to that problem though.
> if( total_objs% GCFreq == 0)
first off, this returns true far more often than the totas_objs reaches 2,000. it in fact returns true every time the total_objs is mearly 2. you might want to check this out.
> But JVM always failed. Even I enlarge GCFreq, VM
> still crashed. Why?
>
the objects pointed to by your native code could be gced by the jvm, if you are careless with your native code, when this happens, the jvm could crash.
if you could post some detailed code, we might be able to get to the point.
> > if( total_objs% GCFreq == 0)
>
> first off, this returns true far more often than the
> totas_objs reaches 2,000. it in fact returns true
> every time the total_objs is mearly 2. you might want
> to check this out.
No.
That will be the case only when GCFreq is 2.
If GCFreq is 2000, then the expression will be true at 2000, 4000, 6000, etc.
jverda at 2007-7-13 18:24:50 >

> > > if( total_objs% GCFreq == 0)
> >
> > first off, this returns true far more often than the
> > totas_objs reaches 2,000. it in fact returns true
> > every time the total_objs is mearly 2. you might want
> > to check this out.
>
> No.
>
> That will be the case only when GCFreq is 2.
>
> If GCFreq is 2000, then the expression will be true
> at 2000, 4000, 6000, etc.
2/2000 != 0;
2%2000 = 0;
4%2000 = 0;
...
jverda at 2007-7-13 18:24:50 >

> That will be the case only when GCFreq is 2.> > If GCFreq is 2000, then the expression will be true> at 2000, 4000, 6000, etc.oh well, you dont know what i am drinking. the stuff is truely good:)
jverda at 2007-7-13 18:24:50 >

> > > > if( total_objs% GCFreq == 0)
> > >
> > > first off, this returns true far more often than
> the
> > > totas_objs reaches 2,000. it in fact returns
> true
> > > every time the total_objs is mearly 2. you might
> want
> > > to check this out.
> >
> > No.
> >
> > That will be the case only when GCFreq is 2.
> >
> > If GCFreq is 2000, then the expression will be
> true
> > at 2000, 4000, 6000, etc.
>
> 2/2000 != 0;
Wrong.
> 2%2000 = 0;
Wrong.
> 4%2000 = 0;
Wrong.
You seem to be confusing division ( / ) with modulus (% ).
jverda at 2007-7-13 18:24:50 >

> > That will be the case only when GCFreq is 2.
> >
> > If GCFreq is 2000, then the expression will be
> true
> > at 2000, 4000, 6000, etc.
>
> oh well, you dont know what i am drinking. the stuff
> is truely good:)
Paint thinner?
I would guess that heap() -> collect(...) was not meant to be called willy-nilly in any old line of code you feel like.I've got co-odeI've got bu-ugsI've got my debugger, who could ask for anything more?
> I would guess that heap() -> collect(...) was not> meant to be called willy-nilly in any old line of> code you feel like.> I missed that.
> I am doing a project related to object's lifetime. I
> modified JVM code to force garbage collection every
> 20000 allocations (i.e., every 20000 objects have
> been allocated). In my code, I used a global variable
> total_objs to track how many objects have been
> allocated so far.
> I added the following code at the allocation point (
> in function jvmpi_object_alloc() of universe.cpp)
>
To be clear...
You have modified the VM source code itself, and it now blowing up?
Then the explanation is that you do not understand how the VM works and you need to look into that more completely. There are a variety of commercial tools which you can use to help you to determine where a memory /pointer problem is occurring. Those would probably help you to determine the problem but wouldn't necessarily help in understanding why it is failing.
Note that I am sure you can also get a support contract from Sun that will allow them to specificaly help you to modify the VM.
Please note that the the primary reason that this site exists is to help in the usage of the java language and not in the usage of C/C++ which is what the code you posted is written in. So you are really unlikely to get help on that.
Moreover most people don't modify the VM for various reasons. Including that you can't distribute the modified source (so one can't sell a solution that requires that.)
Finally there are any number of command line options on the current Sun VM and I suspect investigating what those are and how those work would probably lead to a less fragile solution.
