简体   繁体   English

如何在Java中的特定内存和特定大小的对象中创建对象?

[英]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 ++黑客坚持),但如何指向特定地址呢?

an important aspect of the JVM is that memory is abstracted. JVM的一个重要方面是内存是抽象的。 You really can't point to a specific address in memory. 您确实无法指向内存中的特定地址。

You can get the size of an object in memory. 您可以获取内存中对象的大小。 Look here for details 在这里查看详细信息

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.

相关问题 Java VisualVM内存采样器 - 如何获取特定类的大小 - Java VisualVM memory sampler - how to get the size of a specific class JVM内存中特定对象的子树的大小 - Size of the specific object's subtree in the JVM memory 如何在特定大小的Java中生成二进制Blob对象? - How to generate a binary blob object in java of a specific size? 如何在Java中给定键String的情况下创建特定类的对象? - How to create an object of an specific class given a key String in Java? 如何用Java创建对象? 具体来说,创建对象时内存中的处理过程是什么? - How is an object created in Java? To be specific, What is the process going on in the memory when an object is created? 如何创建具有特定大小的元素的列表 - How to create a list with specific size of elements 创建特定大小(MB)的Java变量(字符串) - Create A Java Variable (String) of a specific size (MB's) 如何根据JSON对象的字段值创建特定Java类的对象? - How to create an object of specific Java class from JSON object depending on it's fields values? 如何创建 JSON 文件,提取特定的 object 字段,基于 object 与 @JsonProperty 注释的所有字段 - ZD6C71E8DFF178D1 - How to create JSON file extracting specific object fields, basing on object with all fields annotated with @JsonProperty - JAVA 如何创建特定对象变量的子列表? - How to create s sublist of specific object variables?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM