Hi !
Featured games (84)
games approved by the League of Dukes
Games in Showcase (549)
Games in Android Showcase (139)
games submitted by our members
Games in WIP (593)
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  
  Java's Unsafe class  (Read 5721 times)
0 Members and 1 Guest are viewing this topic.
Offline xfactor973

Senior Newbie

Java games rock!

« Posted 2003-08-14 00:02:51 »

Anyone have experience using sun.misc.Unsafe ?  I tried using it today and it's giving an error when i run it.  

import sun.misc.*;

public class UnsafeTester {
  private static final Unsafe unsafe = Unsafe.getUnsafe();
  public static void main(String[] args) {
    long ptr = unsafe.allocateMemory(512);
      System.out.println("Unsafe: " + unsafe.toString());

Caused by: java.lang.SecurityException: Unsafe
      at sun.misc.Unsafe.getUnsafe(
      at garbage.UnsafeTester.<clinit>(
Exception in thread "main"

I'm not sure exactly how I should go about tackling this one so any help would be great.   Wink
Offline AndersDahlberg

Junior Devvie

« Reply #1 - Posted 2003-08-14 00:05:33 »

Well, don't use it Grin

There was a bug once upon a time Wink when you could actually use it to wrec havoc (first beta1.4.0 or something like that) - they fixed that quite fast Smiley
Offline GergisKhan

Junior Devvie

&quot;C8 H10 N4 O2&quot;

« Reply #2 - Posted 2003-08-14 00:35:38 »

Ummmm.... what is this class?

why does it curiously remind me of the structs discussion that was occurring in another thread somewhere?

And for future reference, where can I get information about the sun.misc.* classes?


"Go.  Teach them not to mess with us."
          -- Cao Cao, Dynasty Warriors 3
Games published by our own members! Check 'em out!
Legends of Yore - The Casual Retro Roguelike
Offline tortoise

Junior Devvie

<3 Shmups

« Reply #3 - Posted 2003-08-14 00:49:23 »

Whatever it is, I suspect it is unsafe to use Smiley

You're really not supposed to use any of the sun.* classes for real use. They are subject to change at any moment, and are only available on Sun JVMs.
Offline socketrider

Innocent Bystander

Java games rock!

« Reply #4 - Posted 2003-08-14 10:25:03 »

Check this page:

Offline xfactor973

Senior Newbie

Java games rock!

« Reply #5 - Posted 2003-08-14 11:12:54 »

Thanks for your help.  The only reason I was curious about that Unsafe class was my friend wanted to make a stringBuffer class using JNI and it turned out being alot slower than even your normal stringbuffer.  I'm guessing the JNI calls are expensive.  so we went hunting in the nio classes and found no native methods and that lead to the Unsafe class.  Only problem is that security exception that i'm getting.  Guess it's unusable at this point.   :-/
Offline princec

« JGO Spiffy Duke »

Medals: 457
Projects: 3
Exp: 16 years

Eh? Who? What? ... Me?

« Reply #6 - Posted 2003-08-14 11:32:56 »

You should have no need whatsoever to use Unsafe.
Got performance problems? You've written the code wrong. Time to run with the -Xprof flag methinks!

Cas Smiley

Offline xfactor973

Senior Newbie

Java games rock!

« Reply #7 - Posted 2003-08-14 11:39:16 »

yeah this is true.  It was just something i was toying around with.   Grin
Offline xfactor973

Senior Newbie

Java games rock!

« Reply #8 - Posted 2003-08-22 11:57:26 »

When I allocate memory with the Unsafe class what exactly is it doing?  I'm really just experimenting with the class.  And the other thing is that when i call unsafe.addressSize() it always returns 4 for some odd reason.  Anyone know how this class works? Huh
Offline abies

Senior Devvie

« Reply #9 - Posted 2003-08-22 12:52:14 »

 And the other thing is that when i call unsafe.addressSize() it always returns 4 for some odd reason.

I suppose that it returns size of native platform pointer in bytes. So on 64-bit system you will probably get 8 as a return.

Artur Biesiadowski
Games published by our own members! Check 'em out!
Legends of Yore - The Casual Retro Roguelike
Offline xfactor973

Senior Newbie

Java games rock!

« Reply #10 - Posted 2003-08-23 13:46:46 »

Good thinking.  Thanks for the help
Offline Jeff

JGO Coder

Got any cats?

« Reply #11 - Posted 2003-09-05 22:23:22 »

Unsafe is the underpinning to Native Byte Buffers

Thats really where you want to hit this.


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!
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.

Archive (25 views)
2015-01-29 04:26:08

theagentd (22 views)
2015-01-28 15:33:52

GamerIDGoesHere (32 views)
2015-01-27 01:23:23

GamerIDGoesHere (33 views)
2015-01-27 01:22:15

CopyableCougar4 (40 views)
2015-01-27 00:34:41

CopyableCougar4 (28 views)
2015-01-26 04:47:56

Olo (19 views)
2015-01-25 21:26:00

Olo (26 views)
2015-01-25 18:44:22

Robo11 (43 views)
2015-01-25 06:14:26

basil_ (34 views)
2015-01-17 22:29:32
2D Dynamic Lighting
by ThePixelPony
2015-01-01 20:25:42

How do I start Java Game Development?
by gouessej
2014-12-27 19:41:21

Resources for WIP games
by kpars
2014-12-18 10:26:14

Understanding relations between setOrigin, setScale and setPosition in libGdx
by mbabuskov
2014-10-09 22:35:00

Definite guide to supporting multiple device resolutions on Android (2014)
by mbabuskov
2014-10-02 22:36:02

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 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‑
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!