[英]how to create an object in a specific memory and with specific size in Java?
I don't how to ask this question but I want to create an object in a specific address (memory) and with specific size. 我不问这个问题,但是我想在特定的地址(内存)中创建一个具有特定大小的对象。 For example,
class A {int, string, class B, List<array>}
shouldn't be more than 30 MB otherwise throws an error NoMoreMemoryException
例如,
class A {int, string, class B, List<array>}
不应超过30 MB,否则会引发错误NoMoreMemoryException
I know there is no pointer in Java (even if C++ hackers insist) but how to point to a specific address? 我知道Java中没有指针(即使C ++黑客坚持),但如何指向特定地址呢?
Write a JNI code and call thru java code.Please look at the source code of the Object class written in c,specifically clone method, which does the bitwise copy in the memory to create clone object. 编写一个JNI代码并通过Java代码进行调用。请查看用c编写的Object类的源代码,特别是clone方法,该方法在内存中按位复制以创建克隆对象。 from there you can logic and use it in ur own class to know the size of the each member field attributes.
从那里,您可以进行逻辑处理,并在您自己的类中使用它来了解每个成员字段属性的大小。
This should get you half-way: http://robaustin.wikidot.com/how-to-write-to-direct-memory-locations-in-java 这应该使您半途而废: http : //robaustin.wikidot.com/how-to-write-to-direct-memory-locations-in-java
Like the article says though, the JVM needs to control the memory to get access. 就像文章所说的那样,JVM需要控制内存以获取访问权限。
Another more powerful option would be to use JNI and do what you need to do in C or C++. 另一个更强大的选择是使用JNI并执行您在C或C ++中需要做的事情。
There's no way you can do that in pure Java. 您不可能在纯Java中做到这一点。 If, for some reason you have a requirement for doing exactly that, you should use JNI/C.
如果出于某种原因而需要执行此操作,则应使用JNI / C。
As a note, the toString()
method (depending in the implementation) sometimes can give you the initial address where an object is created, but it can be moved by the JVM in any time. 注意,
toString()
方法(取决于实现)有时可以为您提供创建对象的初始地址,但JVM可以随时将其移动。
I assume you are getting an OutOfMemoryError, perhaps because your -mx or -Xmx setting it too low. 我假设您收到OutOfMemoryError异常,可能是因为-mx或-Xmx设置得太低。
Its not very clear what your data structure is or why you believe it shouldn't be 30 MB, however 30 MB is a fairly trivial amount of memory in a computer these days. 尚不清楚您的数据结构是什么,或者为什么您认为它不应为30 MB,但是,如今30 MB是计算机中相当少的内存量。 A server with 8 GB costs about £850.
具有8 GB的服务器的价格约为850英镑。 Are you programming for a mobile device?
您是否正在为移动设备编程?
The best way to see how much a structure is using and why is to use a memory profiler. 查看结构使用量以及原因的最佳方法是使用内存分析器。 VisualVM is a profiler which comes with Java.
VisualVM是Java附带的探查器。 YourKit is a commercial profiler which I find better.
YourKit是我发现更好的商业分析器。
It is quite likely you are not using the most memory efficient structure, but then again it quite likely it shouldn't make much difference. 您很有可能没有使用内存效率最高的结构,但是同样很可能不会有太大的不同。
Perhaps if you could clarify what you are doing when could suggest a better solution. 也许如果您可以澄清自己在做什么,那么可能会建议一个更好的解决方案。
In Sun/Oracle's JVM you can point to a specific location in memory and allocate/free memory like in C, but it is highly unlikely to be a good idea. 在太阳/ Oracle的JVM可以指向特定的内存位置和分配/释放内存就像在C,但它是极不可能是一个好主意。 ;)
;)
I know this post is really old but I thought I comment it anyway :-) 我知道这篇文章真的很老,但我还是想发表评论:-)
You can actually access the Memory as long as it is managed by the JVM you could use the sun.misc.Unsafe which brings some special methods which allow the user accessing the memory directly. 您实际上可以访问内存,只要它由JVM管理,就可以使用sun.misc.Unsafe,它带来了一些特殊的方法,这些方法允许用户直接访问内存。
Be advised, this is a really dangers Class that´s why it is not only not documented, the constructors are also private, which means you have to use Reflection. 请注意,这是一个真正的危险类,这就是为什么不仅没有记录它,而且构造函数也是私有的,这意味着您必须使用Reflection。
You can do some nice things with it. 您可以用它做一些不错的事情。 enjoy....
请享用....
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.