Movatterモバイル変換


[0]ホーム

URL:


Wikidot.com
.wikidot.com
Share ontwitterFacebookDeliciousDiggRedditReddit
EditHistoryTagsSource
Explore »
 

Java Wiki

How To Write Directly to a Memory Locations In Java
chip_ao_01.jpg

If anyone has ever told you, you cannot write directly to memory locations in java, then they are wrong. Well, to be precise, they are half-wrong, you can write to memory locations as long as the memory is control by the JVM.

Although this is possible, I strongly recommend that you don’t do it. Failing to get your code 100% correct will cause the JVM to crash. There maybe cases where you wish to optimize your code and write to memory directly but I would only do this as a last resort.

On the Hotspot JVM, you are able to write and read directly to memory. One of the advantages of this technique is that is very fast, however it comes with no safe guards usually provided by the Java APIs. Its also not documented by SUN.

Use the java class sun.misc.Unsafe, some of the methods you may be interested in are :

publicnativelonggetByte(longaddress);publicnativevoidputByte(longaddress,bytevalue);publicnativelonggetLong(longaddress);publicnativevoidputLong(longaddress,longvalue);publicnativelongallocateMemory(longsize);publicnativelongreallocateMemory(longl,longl1);publicnativevoidsetMemory(longl,longl1,byteb);publicnativevoidcopyMemory(longl,longl1,longl2);

You can't instantiate the class directly as it has a private constructor, so you will have to create an instance like this :

Unsafeunsafe =null;try{Fieldfield =sun.misc.Unsafe.class.getDeclaredField("theUnsafe");field.setAccessible(true);unsafe =(sun.misc.Unsafe)field.get(null);}catch(Exceptione){thrownewAssertionError(e);}

you can then call

importjava.lang.reflect.Field;importsun.misc.Unsafe;publicclassDirect{publicstaticvoidmain(String...args){Unsafeunsafe =null;try{Fieldfield =sun.misc.Unsafe.class.getDeclaredField("theUnsafe");field.setAccessible(true);unsafe =(sun.misc.Unsafe)field.get(null);}catch(Exceptione){thrownewAssertionError(e);}longvalue =12345;bytesize =8;// a long is 64 bits (http://docs.oracle.com/javase/tutorial/java/nutsandbolts/datatypes.html)longallocateMemory =unsafe.allocateMemory(size);unsafe.putLong(allocateMemory,value);longreadValue =unsafe.getLong(allocateMemory);System.out.println("read value :" +readValue);}}

this will output :

read value : 12345
Powered byWikidot.com
Unless otherwise stated, the content of this page is licensed underCreative Commons Attribution-ShareAlike 3.0 License

Other interesting sites

Energy Club

University of Alberta

The Backrooms Lose

You'ot been here before,because it has gone up in smoke ...

THIS WIKI CLOSED

Current Wiki @ multiversemush.com
Click here to edit contents of this page.
Click here to toggle editing of individual sections of the page (if possible). Watch headings for an "edit" link when available.
Append content without editing the whole page source.
Check out how this page has evolved in the past.
If you want to discuss contents of this page - this is the easiest way to do it.
View and manage file attachments for this page.
A few useful tools to manage this Site.
See pages that link to and include this page.
Change the name (also URL address, possibly the category) of the page.
View wiki source for this page without editing.
View/set parent page (used for creating breadcrumbs and structured layout).
Notify administrators if there is objectionable content in this page.
Something does not work as expected? Find out what you can do.
General Wikidot.com documentation and help section.
Wikidot.com Terms of Service - what you can, what you should not etc.
Wikidot.com Privacy Policy.

[8]ページ先頭

©2009-2025 Movatter.jp