问题描述
是否可以使某些线程子集(例如来自特定ThreadPool的线程)从自己的堆中分配内存?例如.大多数线程是从常规共享堆中分配的,而很少有工作线程是从单个堆中分配的(每个线程1:1).
Is it possible to make some sub-set of threads (e.g. from specific ThreadPool) allocate memory from own heap? E.g. most of the threads are allocating from regular shared heap, and few worker threads are allocating from individual heaps (1:1 per thread).
目的是确保在共享环境中安全地执行代码-典型的工作程序是无状态的,并且在单独的线程上运行,处理一个请求不应占用超过4MB的堆.
The intent is to ensure safe execution of the code in shared environment - typical worker is stateless and is running on separate thread, processing of one request should not consume more than 4MB of heap.
更新#1 Re:但是,您为什么担心安全执行"和堆消耗的不可预测的增长?
重点是在我的进程中安全托管任意第三方的Java代码.唯一的要点是,由于第三方代码中的错误,整个过程都不要让内存不足.
The point is about safe hosting of arbitrary 3rd party java code within my process. Once of the points is to not get "Out of Memory" for my entire process because of bugs in the 3rd party code.
更新#2 Re:关于限制每个线程的内存使用情况,在Java语言中是不可能的
根据我在发布此问题之前所做的调查,我的看法是相同的,我只是希望自己缺少一些东西.
According to my investigation before I've posted this question my opinion is the same, I'm just hoping I'm missing something.
我现在看到的用例唯一可能的替代解决方案是...
The only possible alternative solutions for my use-case as I see right now are ...
1)我的Java线程占用多少内存?-跟踪某些调控器线程中的线程内存使用情况并终止不良线程
1) How much memory does my java thread take? - track thread memory usage in some governor thread and terminate bad threads
2)在我自己的JVM上运行Java代码-是有可能的.您可以下载JVM开源实现,对其进行修改...:)
2) Run Java code on my own JVM - Yes it is possible. You can download a JVM open source implementation, modify it ... :)
推荐答案
查看 Java非阻塞内存分配-线程通常已经从它们自己的分配块中分配了内存.因此,如果您担心速度,Sun会为您完成.
Check out Java nonblocking memory allocation — threads are usually allocating memory from their own allocation blocks already. So if the speed is of concern, Sun has done it for you.
关于限制每个线程的内存使用情况,用Java语言是不可能的.该平台在JVM和Java中是否可能(或有意义)是一个有趣的问题.您当然可以与任何内存分析器相同的方法,但恐怕管理系统很快就会淘汰应用程序本身.
As of limiting memory usage per thread, in Java the language it's impossible. Whether it is possible (or makes sense) in JVM and Java the platform is an interesting question. You can of course do it the same way as any memory profiler does, but I'm afraid the management system will outgrow the application itself pretty soon.
这篇关于特定于线程的堆分配的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!