Is there a way to change object memory address? [closed] - java

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 1 year ago.
Improve this question
I am need to write an obfuscation mechanism and think that the cracker can just read desired address in memory and access my object. All that I have so far come up with for the role of protection against this is the frequent "shuffling" of addresses in the heap. As far as I know, in garbage collection, the collector can do this.
My question is: is it possible in Java 8 to implement such a "shuffle" or at least change the address of a particular object, if so, how?

I am need to write an obfuscation mechanism and think that the cracker can just read desired address in memory and access my object.
You are probably trying to solve a problem that doesn't really exist. For a non-trivial Java program, it is extremely unlikely that a specific object will have the same memory address across multiple runs of your application. So checking the content of fixed memory addresses to find objects would not offer any "leverage" for the hacker. And ... it follows that moving objects won't improve security.
Having said that, the Java language and the Java Virtual Machine provide no way to move objects in memory. The closest you will get is to create a new object ... which will have a different address to the old one ... copy its state, and then update any variables containing references to it.
Finally ...
If the hackers (or customers who you don't trust) control the hardware or operating system on which your application is running, then there is nothing you can do to physically prevent them from hacking your code ... if they have enough time, skill and motivation.
Anything you can conceivably do can be subverted. Trying to come up with clever ways to make hacking more difficult is ultimately futile.

Java itself does not provide you with any API to manipulate the address of an object instance on the heap.
The JVM as such knows how to manipulate object addresses, and there are several ways to access this features, but not from the Java programming language, nor from any other JVM language I am aware of.

Related

Design on how to store large objects in a list [closed]

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 4 years ago.
Improve this question
I need a hint on an Interview question that I came across. I tried to find a solution but I need advice from experts over here. What are the different strategies you would employ had you came across this particular situation? The question and my thoughts are as follows:
Q. You want to store a huge number of objects in a list in java. The number of objects is very huge and gradually increasing, but you have very limited memory available. How would you do that?
A. I answered by saying that, once the number of elements in the list
get over a certain threshold, I would dump them to a file. I would typically then build cache-like data-structure that would hold the most-frequently or recently added elements. I gave an analogy of page swapping employed by the OS.
Q. But this would involve disk access and it would be slower and affect the execution.
I did not know the solution for this and could not think properly during the interview. I tried to answer as:
A. In this case, I would think of horizontally scaling the system or
adding more RAM.
Immediately after I answered this, my telephonic interview ended. I felt that the interviewer was not happy with the answer. But then, what should have been the answer.
Moreover, I am not curious only about the answer, I would like to learn about different ways with which this can be handled.
Maybe I am not sure but It indicates that somewhat Flyweight Pattern. This is the same pattern which is been used in String pool and its efficient implementation is must Apart from that, we need focus on database related tasks in order to persist the data if the threshold limit is surpassed. Another technique is to serialize it but as you said, the interviewer was not satisfied and wanted some other explanation.

Java list expand strategy [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 6 years ago.
Improve this question
for example, in an ArrayList, each item is very big, and the size of the list may be large enough to exceed the size of memory. What is the strategy to expand a list in this situation?
Thanks for all the replies. I have encountered such a problem that receiving a list of object by remote calling, and each object in the list may be quite large while the size of the list may be 10000 or more. I wonder how to store this list into memory during the execution.
List<BigItem> list = queryService.queryForList(params...);
Your question is very generic, but I think it is possible to give a certain "fact based" answer nonetheless:
If your setup is as such that memory becomes a bottleneck; then your application needs to be aware about that fact. In other words: you need to implement measurements within your application.
You have to enable your application to make the decision if "growing" a list (and "loading" those expensive objects for example) is possible, or not.
A simple starting point is described here; but of course, this is really a complicated undertaking. Your code has to constantly monitor its memory usage; and take appropriate steps if you get closer to your limits.
Alternatively, you should to profiling to really understand the memory consumption "behavior" of your application. There is no point in a putting effort into "self-controlling" ... if your application happens to have various memory leaks for example. Or if your code is generating "garbage" on a rate that makes the garbage collector spin constantly.
You see, a lot of aspects come into play here. You should focus on them one by one. Start with understanding your application; then decide if you have to improve its "garbage collection" behavior; or if you have go down the full nine yards and make your application manage itself!

Why did Sun/Oracle not implement an object pool for java.util.regex.Pattern? [closed]

Closed. This question is opinion-based. It is not currently accepting answers.
Want to improve this question? Update the question so it can be answered with facts and citations by editing this post.
Closed 7 years ago.
Improve this question
Background
On every large, commercial Java project I've worked on, I come across numerous usages of Pattern.compile(...) even in code segments which are re-used many times, e.g.
public String rewriteUrlWhichIsDoneABajillionTimes(final String requestedUrl) {
Matcher m = Pattern.compile("^/([^/]+)\\.html$").matcher(requestedUrl);
if (!m.matches()) {
return null;
}
// Do processing here
...
}
For every project on which I found things like this, I told at least one person whom I was working with that Pattern.compile(...) is very slow and is not cached but that the java.util.regex.Pattern class is thread-safe and so it can be safely re-used, and each time they informed me that they did not know these things.
Potential solutions
Correct future usage of the API
One "solution" could be to (try to) force people to read the Java standard library documentation and to use the standard library "correctly", but prescriptive methods often to not work so well.
Correct past usage of the API
Alternatively (or complementarily), it would be possible to "clean up" any bad usages of Pattern.compile(...) wherever they are found, but this is likely to be a never-ending task, since (according to my experience) people will continue to use Pattern.compile(...) incorrectly over and over again...
Correct the API
So why not then simply change the Pattern.compile(...) method so that it pools objects and returns the same instance for equivalent input?-- this would instantaneously apply a fix to possibly billions of lines of code around the world (as long as the respective code is run using a JRE which includes the change). The only possible downside I can imagine is that the software would have a larger memory footprint... but given how much memory most computers have these days, I doubt that this will cause problems anywhere other than in edge cases. On the other hand, a huge number of programs will likely run much faster. So why didn't/doesn't Oracle implement an object pool for Pattern similarly to how they did for strings or for primitives?

How should I properly FULLY delete an object? (C#, Objective-C, Java) [closed]

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 8 years ago.
Improve this question
I haven't found or discovered a clear answer of this yet. I am not asking about a specific language but am talking about all OOP languages in general.
If I have an object for example:
Object obj = new Instance();
Lets say I no longer need this object. Of course I could simply do as follows:
obj = null;
However, correct me if I am wrong, this simply means the obj variable is no longer assigned as "new Instance();" but does not mean that "new Instance();" no longer exists. So my question is: How can I make sure the instance object is COMPLETELY removed from the system and no longer exists. How can I be completely sure the instance no longer exists?
I program in Objective-C, Java, and C#, so an answer relative to those languages would be great as I am aware garbage cleaners work differently in different languages and in some languages don't even exist.
I know you cannot completely clear an instance in some languages however what would be the best way to free up memory with the object?
Thanks so much!
In java and C#, your garbage collector takes care of that and there is nothing you can or should do to reclaim that memory.
As others have said, the removal of the object from memory itself is out of your control with Java. Your only requirement is to unassign all references to this object so that it becomes eligible for garbage collection, but when GC actually runs is neither determinable nor reliably consistent.
There is a System.gc() call which you can read into, but it should probably just be avoided entirely.
If you're programming with C# or Java they can do it for you. I develop with C# so I can talk about it, if you want to force Garbage Collector to delete unused objects for you you can call System.GC.Collect() method can help you.

Java - multithreaded copy-on-write [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 9 years ago.
Improve this question
Is it possible to add software enforced copy-on-write for multithreaded applications in Java? By this I mean threads having a reference to the same object, but when one thread attempts to modify it, the object pointed to is copied and the reference is adjusted to point to that copy.
The only implementation I know is the
java.util.concurrent.CopyOnWriteArrayList
see
http://docs.oracle.com/javase/6/docs/api/java/util/concurrent/CopyOnWriteArrayList.html
and the related Set class
java.util.concurrent.CopyOnWriteArraySet
and finally
org.apache.mina.util.CopyOnWriteMap
but it depends from your need.
If your question is,
is it possible to enforce copy-on-write behavior across the board for an entire Java runtime
then the answer is,
No, there is no such general capability in Java.
Actually, I think the closest you can possibly get to that goal is using Clojure. All its default data structures are copy-on-write internally, and on the outside they are simply immutable objects.
The references you talk about are called, surprisingly, refs and they support full in-memory transactions. A simpler kind of a reference is atom, which fits your description 100%.
The whole Core API is devoted to elegant and epressive manipulation of these structures in a thread-safe, lock-free manner.
Yes. Lazy copying is easy to implement, but you would generally have to do it yourself.

Categories

Resources