Java-Gaming.org    
Featured games (81)
games approved by the League of Dukes
Games in Showcase (497)
Games in Android Showcase (114)
games submitted by our members
Games in WIP (563)
games currently in development
News: Read the Java Gaming Resources, or peek at the official Java tutorials
 
    Home     Help   Search   Login   Register   
Pages: [1]
  ignore  |  Print  
  JNI memory allocation  (Read 2866 times)
0 Members and 1 Guest are viewing this topic.
Offline Mojomonkey

Senior Member




ooh ooh eee eeee


« Posted 2003-10-30 16:53:59 »

When you call native methods via JNI and those methods allocate memory (say loads a sound stream using OpenAL) do they expend the JVM's memory heap, or the underlying OS memory heap?

Don't send a man to do a monkey's work.
Offline swpalmer

JGO Coder


Exp: 12 years


Where's the Kaboom?


« Reply #1 - Posted 2003-10-30 17:47:45 »

OS Heap.  Only java objects (not even the memory blocks used by NativeByteBuffers) are on the Java heap as far as I know.

Offline rreyelts

Junior Member




There is nothing Nu under the sun


« Reply #2 - Posted 2003-10-30 17:54:55 »

Quote
OS Heap.  Only java objects (not even the memory blocks used by NativeByteBuffers) are on the Java heap as far as I know.

That's right. NativeByteBuffers are often used to allocate memory specifically because its outside of the JVMs own heap space.

God bless,
-Toby Reyelts

About me: http://jroller.com/page/rreyelts
Jace - Easier JNI: http://jace.reyelts.com/jace
Retroweaver - Compile on JDK1.5, and deploy on 1.4: http://retroweaver.sf.net.
Games published by our own members! Check 'em out!
Legends of Yore - The Casual Retro Roguelike
Offline swpalmer

JGO Coder


Exp: 12 years


Where's the Kaboom?


« Reply #3 - Posted 2003-10-30 22:08:13 »

Hmmm.. this brings up an interesting security concern.  Can an Applet allocate massive NativeByteBuffers and thus steal all the memory in a system - likely leading to a crash?
Things that make you go  hmmmm...

Offline rreyelts

Junior Member




There is nothing Nu under the sun


« Reply #4 - Posted 2003-10-30 22:45:43 »

Quote
Hmmm.. this brings up an interesting security concern.  Can an Applet allocate massive NativeByteBuffers and thus steal all the memory in a system - likely leading to a crash?
Things that make you go  hmmmm...

I'm not sure how that is any different than setting the max heap size parameter in your JNLP file to something huge and then eating up that memory.

God bless,
-Toby Reyelts

About me: http://jroller.com/page/rreyelts
Jace - Easier JNI: http://jace.reyelts.com/jace
Retroweaver - Compile on JDK1.5, and deploy on 1.4: http://retroweaver.sf.net.
Offline swpalmer

JGO Coder


Exp: 12 years


Where's the Kaboom?


« Reply #5 - Posted 2003-10-31 02:41:47 »

I suppose, but Web Start seems to be less restricted in general.  The user is more aware that they are launching a program.  Applets just happen.  I guess ultimately it isn't that much different.

Offline Jeff

JGO Coder




Got any cats?


« Reply #6 - Posted 2003-10-31 04:31:16 »

Quote
Hmmm.. this brings up an interesting security concern.  Can an Applet allocate massive NativeByteBuffers and thus steal all the memory in a system -.


Nope, because only native (called through JNI) code can create a Native Byte Buffer.

And native code may only be called by trusted Java code.

If you could create arbitrary native byte buffers from the java side that would constitute a COMPLETE break of security as you could touch any memory in the system you wanted to.

Got a question about Java and game programming?  Just new to the Java Game Development Community?  Try my FAQ.  Its likely you'll learn something!

http://wiki.java.net/bin/view/Games/JeffFAQ
Offline princec

JGO Kernel


Medals: 378
Projects: 3
Exp: 16 years


Eh? Who? What? ... Me?


« Reply #7 - Posted 2003-10-31 08:16:26 »

I think he's referring to a perfectly easy DOS attack:
1  
2  
3  
4  
ArrayList a = new ArrayList(100000);
for (;;) {
a.add(ByteBuffer.allocateDirect(1000000000));
}

That'll just chew into swapspace into milliseconds and grind the system to a halt, before finally running out of memory. You could even catch the OutOfMemoryError and then sit in an infinite busy loop writing bytes into the memory as well just to make sure it kept swapping like crazy.

Of course that's no more sophisticated than:
1  
2  
3  
for (;;) {
new Thread() { public void run() { for (;;); } }.start();
}

which will rapidly consume all the available OS resources if it doesn't run out of thread stack memory.

There is possibly a requirement highlighted here that the JVM has a security mechanism in place to prevent such DOS attacks by limiting the availability of some resources by default.

Cas Smiley

Offline swpalmer

JGO Coder


Exp: 12 years


Where's the Kaboom?


« Reply #8 - Posted 2003-10-31 19:24:18 »

Quote
Nope, because only native (called through JNI) code can create a Native Byte Buffer.


allocateDirect doesn't work from an Applet??  Or does it just give you a non-direct buffer?

Offline Jeff

JGO Coder




Got any cats?


« Reply #9 - Posted 2003-10-31 19:46:53 »

Quote


allocateDirect doesn't work from an Applet??  Or does it just give you a non-direct buffer?

Sorry I was thinking of Native Direct  Byte buffers, wher you control the memory mapping.

I dopn't know if allocateDirect works from applets or not, actually.  Its also unclear whether a DirectByte Buffer is native memory or not on any given platform.  (See the "Direct v. Non-Direct" buffers part of the docs.)

In any event allocation is up to the VM.  Its free to deny you the allocation if it thinks doing so will harm the system.



Got a question about Java and game programming?  Just new to the Java Game Development Community?  Try my FAQ.  Its likely you'll learn something!

http://wiki.java.net/bin/view/Games/JeffFAQ
Games published by our own members! Check 'em out!
Legends of Yore - The Casual Retro Roguelike
Offline Jeff

JGO Coder




Got any cats?


« Reply #10 - Posted 2003-10-31 19:48:14 »

Quote

I though you were talking about  *Native* Direct  Byte buffers, wher you control the memory mapping.

I dopn't know if allocateDirect works from applets or not, actually.  Its also unclear whether a DirectByte Buffer is native memory or not on any given platform.  (See the "Direct v. Non-Direct" buffers part of the docs.)

In any event allocation is up to the VM.  Its free to deny you the allocation if it thinks doing so will harm the system.



Got a question about Java and game programming?  Just new to the Java Game Development Community?  Try my FAQ.  Its likely you'll learn something!

http://wiki.java.net/bin/view/Games/JeffFAQ
Pages: [1]
  ignore  |  Print  
 
 
You cannot reply to this message, because it is very, very old.

 

Add your game by posting it in the WIP section,
or publish it in Showcase.

The first screenshot will be displayed as a thumbnail.

UprightPath (21 views)
2014-09-20 20:14:06

BurntPizza (27 views)
2014-09-19 03:14:18

Dwinin (40 views)
2014-09-12 09:08:26

Norakomi (70 views)
2014-09-10 13:57:51

TehJavaDev (96 views)
2014-09-10 06:39:09

Tekkerue (49 views)
2014-09-09 02:24:56

mitcheeb (70 views)
2014-09-08 06:06:29

BurntPizza (52 views)
2014-09-07 01:13:42

Longarmx (39 views)
2014-09-07 01:12:14

Longarmx (45 views)
2014-09-07 01:11:22
List of Learning Resources
by Longor1996
2014-08-16 10:40:00

List of Learning Resources
by SilverTiger
2014-08-05 19:33:27

Resources for WIP games
by CogWheelz
2014-08-01 16:20:17

Resources for WIP games
by CogWheelz
2014-08-01 16:19:50

List of Learning Resources
by SilverTiger
2014-07-31 16:29:50

List of Learning Resources
by SilverTiger
2014-07-31 16:26:06

List of Learning Resources
by SilverTiger
2014-07-31 11:54:12

HotSpot Options
by dleskov
2014-07-08 01:59:08
java-gaming.org is not responsible for the content posted by its members, including references to external websites, and other references that may or may not have a relation with our primarily gaming and game production oriented community. inquiries and complaints can be sent via email to the info‑account of the company managing the website of java‑gaming.org
Powered by MySQL Powered by PHP Powered by SMF 1.1.18 | SMF © 2013, Simple Machines | Managed by Enhanced Four Valid XHTML 1.0! Valid CSS!